package Display::Plugins::Asterisk;

use base ("Display::Plugins");

use strict;
use Carp;

use POE qw( Component::Client::Asterisk::Manager );
use Data::Dumper;
use Clutter;
use FindBin qw/$Bin/;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;

  my $self = $class->SUPER::new(@_);

  bless ($self, $class);

  $self->{'logo'} = "$Bin/share/Asterisk.png";

  $self->{'kernel'}->state('asterisk_delayed_start', $self, 'delayed_start');

  POE::Component::Client::Asterisk::Manager->new(
    Alias => 'asterisk',
    RemoteHost => $self->{config}{_config}{plugins}{Asterisk}{RemoteHost},
    RemotePort => $self->{config}{_config}{plugins}{Asterisk}{RemotePort},
    Username   => $self->{config}{_config}{plugins}{Asterisk}{Username},
    Password   => $self->{config}{_config}{plugins}{Asterisk}{Password},

    CallBacks => {
      asterisk_ring  => {
        'Event' => 'Newchannel',
	'State' => 'Ring'
      },
      asterisk_ringing  => {
        'Event' => 'Newstate',
	'State' => 'Ringing',
      },
      asterisk_hangup => {
        'Event' => 'Hangup'
      },
      asterisk_dial => {
        'Event' => 'Dial'
      },
      asterisk_input => ':all',
    },

    inline_states => {
      # When we're connected to Asterisk, post back to the main session for
      # the rest of our states to be setup in asterisk_start.  This is to
      # make sure the states are created in the correct session with a
      # reference to $self.
      _connected => sub { $self->{'kernel'}->post('display' => 'asterisk_delayed_start') },
      asterisk_start => sub {
        my ($kernel, $self) = @_[KERNEL, ARG0];

        $kernel->state('asterisk_input',   $self, 'input');
        $kernel->state('asterisk_ring',    $self, 'ring');
        $kernel->state('asterisk_ringing', $self, 'ringing');
        $kernel->state('asterisk_hangup',  $self, 'hangup');
        $kernel->state('asterisk_dial',    $self, 'dial');
      },
    },
  );

  return $self;
}

sub delayed_start {
  my ($self) = shift;

  $self->{'kernel'}->post('asterisk_client' => 'asterisk_start' => $self);
}

sub ring {
  my ($self, $kernel, $session, $input) = @_[OBJECT, KERNEL, SESSION, ARG0];
  
  print STDERR "RING! $input->{Channel}\n";
  print Dumper($input);
}

sub ringing {
  my ($self, $kernel, $session, $input) = @_[OBJECT, KERNEL, SESSION, ARG0];

  print STDERR "RING, RING! $input->{Channel}, $input->{'CallerID'} , $input->{'CallerIDName'}\n";
  print Dumper($input);
}

sub input {
  my ($self, $kernel, $input) = @_[OBJECT, KERNEL, ARG0];

  print STDERR "Other input - $input->{Event}\n";
  print Dumper($input);

#  $kernel->post('display', 'notifications_add', "Event: " . $input->{Event}, $self->{'logo'});
}

sub hangup {
  my ($self, $kernel, $input) = @_[OBJECT, KERNEL, ARG0];
}

sub dial {
  my ($self, $kernel, $input) = @_[OBJECT, KERNEL, ARG0];

  my $dest = $input->{Destination};
  $dest =~ s/(.*)-.*/$1/;

  my $text = "Call from " . $input->{CallerIDName} . "\n    " . $input->{CallerID} . "\nFor $dest";

  $kernel->post('display', 'notifications_add', $text, $self->{'logo'});
}



1;
