#!/usr/bin/perl -w
#
#  rhrdlibs
#
#  Copyright (C) 2015 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhrdlibs.
#
#  rhrdlibs is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  rhrdlibs is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with rhrdlibs. If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use RHRD::rddb;

sub print_usage
{
  print STDERR "Usage: rd-group list\n" .
               "       rd-group (check|remove|get-members|get-carts|get-reports) <groupname>\n" .
               "       rd-group add <groupname> [ <description> ]\n" .
               "       rd-group (add-member|remove-member|is-member) <groupname> <user>\n" .
               "       rd-group set-carts <groupname> <low> <high> [ <type> [ <enforce range> ]]\n" .
               "       rd-group set-reports <groupname> <nownext> <traffic> <music>\n";
}

sub list
{
  my ($ctx) = @_;

  my @groups = RHRD::rddb::list_groups($ctx);
  if(!defined $groups[0] && defined $groups[1]) {
    print STDERR "$groups[1]: $groups[2]";
    return 1;
  }
  for my $group (@groups) {
    print $group . "\n";
  }
  return 0;
}

sub check
{
  my ($ctx, $groupname) = @_;

  my ($result, $status, $errorstring) = RHRD::rddb::check_group($ctx, $groupname);
  print STDERR "$result, $status: $errorstring\n";
  return $result;
}

sub add
{
  my ($ctx, $groupname, $description) = @_;

  my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($ctx, $groupname, $description);
  unless(defined $cnt) {
    print STDERR "$errorstring\n";
    return 1;
  }
  print int($cnt) . " rows affected\n";
  return 0;
}

sub remove
{
  my ($ctx, $groupname) = @_;

  my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $groupname);
  if(!defined $carts[0] && defined $carts[1]) {
    print STDERR $carts[1] . ": " . $carts[2] . "\n";
    return 1;
  }
  unless(scalar @carts == 0) {
    print "won't remove group with active shows\n use 'rhrd-show list " . $groupname . "' to find out which show(s) use this group\n";
    return 1;
  }

  my @results = RHRD::rddb::remove_group($ctx, $groupname);
  if(!defined $results[0] && defined $results[2]) {
    print STDERR "$results[2]\n";
    return 1;
  }
  for my $href (@results) {
    print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
  }
  return 0;
}

sub get_members
{
  my ($ctx, $groupname) = @_;

  my @users = RHRD::rddb::get_group_members($ctx, $groupname);
  if(!defined $users[0] && defined $users[1]) {
    print STDERR "$users[2]\n";
    return 1;
  }
  for my $user (sort @users) {
    print "$user\n";
  }
  return 0;
}

sub add_member
{
  my ($ctx, $groupname, $username) = @_;

  my ($cnt, undef, $errorstring) = RHRD::rddb::add_group_member($ctx, $groupname, $username);
  unless(defined $cnt) {
    print STDERR "$errorstring\n";
    return 1;
  }
  print int($cnt) . " rows affected\n";
  return 0;
}

sub remove_member
{
  my ($ctx, $groupname, $username) = @_;

  my ($cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($ctx, $groupname, $username);
  unless(defined $cnt) {
    print STDERR "$errorstring\n";
    return 1;
  }
  print int($cnt) . " rows affected\n";
  return 0;
}

sub is_member
{
  my ($ctx, $groupname, $username) = @_;

  my ($cnt, undef, $errorstring) = RHRD::rddb::is_group_member($ctx, $groupname, $username);
  unless(defined $cnt) {
    print STDERR "$errorstring\n";
    return 1;
  }
  print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n";
  return (($cnt) ? 0 : 1);
}

sub get_carts
{
  my ($ctx, $groupname) = @_;

  my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_cart_range($ctx, $groupname);
  unless(defined $low) {
    print STDERR "$type\n";
    return 1;
  }
  print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n";
  return 0;
}

sub set_carts
{
  my ($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;

  $low_cart = RHRD::rddb::RD_INVALID_CART unless($low_cart >= RHRD::rddb::RD_MIN_CART && $low_cart <= RHRD::rddb::RD_MAX_CART);
  $high_cart = RHRD::rddb::RD_INVALID_CART unless($high_cart >= RHRD::rddb::RD_MIN_CART && $high_cart <= RHRD::rddb::RD_MAX_CART);
  $cart_type = 1 unless(defined($cart_type) && ($cart_type == 1 || $cart_type == 2));
  $enforce_cart_range = 'Y' if(defined($enforce_cart_range) && ($enforce_cart_range eq 'Y' || $enforce_cart_range eq 'y' || $enforce_cart_range eq '1'));
  $enforce_cart_range = 'N' unless(defined($enforce_cart_range) &&$enforce_cart_range eq 'Y');

  my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $groupname);
  if(!defined $carts[0] && defined $carts[1]) {
    print STDERR $carts[1] . ": " . $carts[2] . "\n";
    return 1;
  }
  unless(scalar @carts == 0) {
    for my $cart (@carts) {
      if($cart < $low_cart || $cart > $high_cart) {
        print "this group has active shows with carts outside of the new range\n use 'rhrd-show list " . $groupname . "' to find out which show(s) use this group\n";
        return 1;
      }
    }
  }

  my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_cart_range($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range);
  unless(defined $cnt) {
    print STDERR "$errorstring\n";
    return 1;
  }
  print int($cnt) . " rows affected\n";
  return 0;
}

sub get_reports
{
  my ($ctx, $groupname) = @_;

  my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $groupname);
  unless(defined $nownext) {
    print STDERR "$music\n";
    return 1;
  }
  print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n";
  return 0;
}

sub set_reports
{
  my ($ctx, $groupname, $now_next, $traffic, $music) = @_;

  $now_next = 'Y' if ($now_next eq 'Y' || $now_next eq 'y' || $now_next eq '1');
  $now_next = 'N' unless ($now_next eq 'Y');
  $traffic = 'Y' if ($traffic eq 'Y' || $traffic eq 'y' || $traffic eq '1');
  $traffic = 'N' unless ($traffic eq 'Y');
  $music = 'Y' if ($music eq 'Y' || $music eq 'y' || $music eq '1');
  $music = 'N' unless ($music eq 'Y');

  my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, $now_next, $traffic, $music);
  unless(defined $cnt) {
    print STDERR "$errorstring\n";
    return 1;
  }
  print int($cnt) . " rows affected\n";

  return 0;
}


my $num_args = $#ARGV + 1;
if($num_args < 1) {
  print_usage();
  exit(1);
}
my $cmd = $ARGV[0];
my $groupname = $ARGV[1];
my $ret = 0;

my ($ctx, $status, $errorstring) = RHRD::rddb::init();
if(defined $ctx) {
  if($cmd eq "list") {
    if($num_args != 1) {
      print_usage();
      $ret = 1;
    } else {
      $ret = list($ctx);
    }
  }
  elsif($cmd eq "check") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = check($ctx, $groupname);
    }
  }
  elsif($cmd eq "add") {
    if($num_args < 2 || $num_args > 3) {
      print_usage();
      $ret = 1;
    } else {
      $ret = add($ctx, $groupname, $ARGV[2]);
    }
  }
  elsif($cmd eq "remove") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = remove($ctx, $groupname);
    }
  }
  elsif($cmd eq "get-members") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = get_members($ctx, $groupname);
    }
  }
  elsif($cmd eq "add-member") {
    if($num_args != 3) {
      print_usage();
      $ret = 1;
    } else {
      $ret = add_member($ctx, $groupname, $ARGV[2]);
    }
  }
  elsif($cmd eq "remove-member") {
    if($num_args != 3) {
      print_usage();
      $ret = 1;
    } else {
      $ret = remove_member($ctx, $groupname, $ARGV[2]);
    }
  }
  elsif($cmd eq "is-member") {
    if($num_args != 3) {
      print_usage();
      $ret = 1;
    } else {
      $ret = is_member($ctx, $groupname, $ARGV[2]);
    }
  }
  elsif($cmd eq "get-carts") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = get_carts($ctx, $groupname);
    }
  }
  elsif($cmd eq "set-carts") {
    if($num_args < 4 || $num_args > 6) {
      print_usage();
      $ret = 1;
    } else {
      $ret = set_carts($ctx, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]);
    }
  }
  elsif($cmd eq "get-reports") {
    if($num_args != 2) {
      print_usage();
      $ret = 1;
    } else {
      $ret = get_reports($ctx, $groupname);
    }
  }
  elsif($cmd eq "set-reports") {
    if($num_args != 5) {
      print_usage();
      $ret = 1;
    } else {
      $ret = set_reports($ctx, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]);
    }
  }
  else {
    print_usage();
    $ret = 1;
  }

  RHRD::rddb::destroy($ctx);
} else {
  print STDERR "$errorstring\n";
  $ret = 1;
}

exit $ret;