# $Id: ActiveConferenceViewSession.pm,v 1.7 2003/03/23 05:22:13 swick Exp $

package ActiveConferenceViewSession;
use Session;
use Party;
use strict;
use Carp;

@ActiveConferenceViewSession::ISA = qw( Session );

sub version			# .VER
{
    # Version
    my ($self, $version) = @_;
    $self->trace( "ActiveConferenceViewSession ", $self->{_id}, " version $version" );
}

sub attributes			# .SA
{
    # 1~BRIDGEID~CONFID~ASYNCHMSGSENABLED~PARTYDTMFENABLED~OPERATORUPDATESENABLED
    my ($self, $msgNum, $bridge, $confID) = @_;
    $self->{bridge} = $bridge;
    $self->{conf} = Conference->find($bridge, $confID);
    $self->{conf}->{viewSession} = $self;
}

sub confAttributes		# .A
{
    # 1~CONFNAME~BILLINGCODE~CONFIRMCODE~SPECINSTR~STARTTIME~ESTENDTIME~
    #    CONFTYPE~CONFINFO~MAXPARTIES~MAXHOSTS~PNRENABLED~BOHDENABLED~
    #    AUTOBREAKDOWNENABLED~ENTRYEXITCONFIG~REQFEATURES~DNIS1~DNIS2~
    #    USERFLAG1~USERFLAG2~USERFIELD1~USERFIELD2
    # 2~STATE~SECURE~IVRFEATURESTATE~ACTIVESESSIONTYPE~SESSIONOWNERID~
    #    SESSIONSTATE~UNATTSTARTED

    my ($self, $msgNum) = @_;
    if ($msgNum == 1) {
	my ($self, $msgNum, $confName, $schedID, $confirmCode, $instr,
	    $start, $end, $type, $info, $maxParties, $maxhosts, $pnr,
	    $bohd, $autobreakdown, $entryexit, $features) = @_;
	$self->{conf}->{_schedID} = $schedID;
	$self->{conf}->{_maxParties} = $maxParties;
	$self->{conf}->{_features} = $features;
    }
}

sub partyAttributes		# .P.A
{
    # PARTYID~1~INITIALIZED~PARTYNAME~PHONE~OPERATORID~USERDEFINED~
    #    PIN~DNIS~ANI~BILLINGFIELD~BILLINGTYPE~ISMODERATOR~HOSTCTRLLEVEL
    # PARTYID~2~LOCATION~CONNECTSTATE~DISCONNECTREASON~OPERHELPREQ

    my ($self, $id, $msgNum) = @_;
    my $party = Party->find($self->{bridge}, $id);
    if ($msgNum == 1) {
	my ($self, $id, $msgNum , $initialized, $partyName, $phone, $opID,
	    $user1, $pin, $dnis, $ani) = @_;
	if (not defined $party) {
	    Party->new($self->{bridge}, $self->{conf}, $id, $partyName, $phone, $ani);
	}
	else {
	    my $oldName = $party->name($partyName);
	    my $oldPhone = $party->phone($phone);
	    if ($oldName ne $partyName || $oldPhone ne $phone) {
		$self->{conf}->notifyPartyAttributes($party);
	    }
	}
    } elsif ($msgNum == 2) {
	if (defined $party) {
	    # PARTYID~2~LOCATION~CONNECTSTATE~DISCONNECTREASON~OPERHELPREQ
	    my ($self, $id, $msgNum , $location, $state) = @_;
	    my $wasactive = $party->isactive();
	    my $wasmuted = $party->ismuted();
	    $party->location($location);
	    $party->state($state);
	    $party->where();
	    my $isactive = $party->isactive();
	    if ($wasactive != $isactive) {
		$self->{conf}->notify($party);
	    }
	    elsif ($isactive && ($wasmuted != $party->ismuted())) {
		$self->{conf}->notifyPartyAttributes($party);
	    }
	} else {
	    carp "Error: undefined party";
	}
    }
}

sub partyDeleted		# .P.DEL
{
    # PARTYID
    my ($self, $id) = @_;
    Party->delete( $self->{bridge}, $id );
}

sub new
{
    my ($proto, $conn, $id) = @_;
    my $class = ref($proto) || $proto;

    my $self = $class->SUPER::new($conn, $id);

    $self->{bridge} = undef;
    $self->{conf} = undef;

    $self->trace( "Session $id is a ActiveConferenceViewSession" );

    bless ($self, $class);

    $self->{handler}->{'ACV.VER'} = \&version;
    $self->{handler}->{'ACV.SA'} = \&attributes;
    $self->{handler}->{'ACV.A'} = \&confAttributes;
    $self->{handler}->{'ACV.P.A'} = \&partyAttributes;
    $self->{handler}->{'ACV.P.DEL'} = \&partyDeleted;

    return $self;
}

1;
