package WSZakim;

use strict;

# TODO
#
# HTTP GET

use ZakimInterface;
use Data::Dumper;

my $debuglevel = 1;
my $debug = $debuglevel . ' ' . __PACKAGE__;

# soapFault
sub soapFault() {
    my $a = shift;
    &main::debug($debug, 1, "Faulting: $a");
    die SOAP::Fault->faultcode('Server')
	->faultstring($a);
}

# API

sub callMe {
    my ($self, $conf, $caller) = @_;
    my $a;
    &main::debug($debug, 1, 'Dialing somebody into a conference');
    $a = &ZakimInterface::dialinByWeb($conf, $caller);
    if (! $a) {
	&soapFault('Could not contact Zakim bridge');
    }
    if ($a =~ m/is not in the dialout directory$/) {
	&soapFault($a);
    }
    # Fault
    return $a;
}

sub attendees {
    my ($self, $conf) = @_;
    &main::debug($debug, 1, 'Getting list of attendees');
    my ($a, $error) = &ZakimInterface::getAttendeesListByWeb($conf);
    if ($error) {
	&soapFault($error);
    }
    if (! $a) {
	&soapFault("This conference isn't active for the moment");
    }
    return $a;
}

sub listConferences {
    my ($self) = @_;
    &main::debug($debug, 1, 'Getting list of ongoing teleconferences');
    my ($a, $error) = &ZakimInterface::getConferenceListByWeb();
    if ($error) {
	&soapFault($error);
    }
    return $a;
}

sub createConference {
    my ($self, $number) = @_;
    if (! $main::startIrcModule) {
	&soapFault("Unavailable: WS-Zakim needs to be restarted with the IRC module");
    }
    my $a;
    &main::debug($debug, 1, 'Trying to create a conference');
    $a = &ZakimInterface::createConferenceByIrc($number);
    if ($a->{error}) {
	&soapFault($a->{error});
    }
    return($a);
}

1;
