# JK::Lib:Asterisk.pm
# $Id: Asterisk.pm,v 1.0 2012/09/11 08:34:32 jkister Exp $
# Copyright (c) 2012-2014 Jeremy Kister.
# Released under Perl's Artistic License.

package JK::Lib::Asterisk;

use strict;
use warnings;

our ($VERSION) = q$Revision: 1.0 $ =~ /(\d+\.\d+)/;

sub new {
    my $class = shift;
    my %args = @_;

    my $self = bless(\%args, $class);

    #QQQ a better way to detect swift?
    if( $::agi->exec('Swift') eq '-1' ){
        $self->{swift} = 1;
        $self->{dtmf} = 'SWIFT_DTMF';
    }else{
        $self->{dtmf} = 'DTMF';
    }

    return($self);
}


sub verbose {
    my $self = shift;
    my $msg = join('', @_);

    $::agi->verbose($msg, 1);
}


sub debug {
    my $self = shift;

    $self->verbose(@_) if $::opt{D};
}


sub asr {
    my $self = shift;
    my $text = join('', @_);

    my $utterance;
    for( my $n = 1; $n <= 3; $n++ ){
        $self->debug( "in asr loop - $n" );

        $self->tts( $text );
        $::agi->exec('AGI','speech-recog.agi,en-US,2,any,NOBEEP');
        $utterance = $::agi->get_variable('utterance');
        my $confidence = $::agi->get_variable('confidence');
        $self->verbose( "utterance is: ${utterance} - conf: ${confidence}" );

        if( $confidence < 0.6 ){
            if( $utterance eq '-1' ){
                $self->tts( "i did not hear you." );
                $utterance = undef;
            }else{
                $self->tts( "i did not understand you.  did you say" ); # separate for caching
                $self->tts( "${utterance} ?" );
                $::agi->exec('AGI','speech-recog.agi,en-US,2,any,NOBEEP');
                my $answer = $::agi->get_variable('utterance');
                my $confidence = $::agi->get_variable('confidence');

                if( $confidence < 0.5 || $answer =~ /no/i ){
                    $utterance = undef;
                }elsif( $answer =~ /yes/i ){
                    last;
                }
            }
        }else{
            last;
        }
    }
    unless( $utterance ){
        $self->tts( 'goodbye' );
        sleep 2;
        $::agi->hangup;
        exit;
    }
}


sub tts {
    my $self = shift;
    my $msg = shift;
    my $digits = shift;

    $self->debug( "tts -> msg: $msg - digits: $digits" );

    if( $self->{swift} ){
        $msg .= ",5000,${digits}" if $digits;
    }else{
        $msg .= ",${digits}" if $digits;
    }

    eval {
        if( $self->{swift} ){
            $::agi->exec('Swift',$msg);
        }else{
            $::agi->exec('Macro',"TTS,$msg");
        }
    };

    if( $digits ){
        $self->debug( "tts -> need DTMF" );
        my $dtmf = $::agi->get_variable( $self->{dtmf} );
        my $times = 1;
        until( $dtmf =~ /^[\d#\*]+$/ ){
            if( $times == 3 ){
                $self->tts( 'a response is required.  goodbye' );
                sleep 2;
                $::agi->hangup;
                last;
            }
            eval {
                if( $self->{swift} ){
                    $::agi->exec('Swift',$msg);
                }else{
                    $::agi->exec('Macro',"TTS,$msg");
                }
            };
            $self->debug( "tts -> trying again" );
            $dtmf = $::agi->get_variable( $self->{dtmf} );
            $times++;
        }
        $self->debug( "returning dtmf: $dtmf" );
        return $dtmf;
    }
}


1;
