package SMS::Send::VoIP::MS;
use strict;
use warnings;
use URI;
use JSON::XS qw{decode_json};
use base qw{SMS::Send::Driver::WebService};

our $VERSION = '0.03';

=head1 NAME

SMS::Send::VoIP::MS - SMS::Send driver for Web Services


  Configure /etc/SMS-Send.ini
  use SMS::Send;
  my $sms     = SMS::Send->new('VoIP::MS');
  my $success = $sms->send_sms(text=> 'Hello World!', to =>'+17035551212');
  use SMS::Send::VoIP::MS;
  my $sms     = SMS::Send::VoIP::MS->new;
  my $success = $sms->send_sms(text=> 'Hello World!', to =>'+17035551212');
  my $json    = $sms->{__content};
  my $href    = $sms->{__data};


SMS::Send driver for Web Services.

=head1 METHODS

=head2 send_sms

  my $success = $sms->send_sms(text=> 'Hello World!', to =>'+17035551212');


sub send_sms {
  my $self             = shift;
  my %argv             = @_;
  my $to               = $argv{'to'} or die('Error: to propoerty required');
  my $text             = defined($argv{'text'}) ? $argv{'text'} : '';
  my $url              = $self->url; #isa URI
  $url                 = URI->new($url) unless (ref($url) and $url->can('query_form'));
  my @form             = (
                          method       => 'sendSMS',
                          api_username => $self->username,
                          api_password => $self->password, 
                          did          => $self->did,
                          dst          => $argv{'to'},
                          message      => $argv{'text'},

  my $response         = $self->uat->get($url); #isa HASH from HTTP::Tiny
  die(sprintf('HTTP Error: %s %s', $response->{'status'}, $response->{'reason'})) unless $response->{'success'};
  $self->{'__content'} = $response->{'content'};
  my $data             = decode_json($response->{'content'});
  $self->{'__data'}    = $data;
  return $data->{'status'} eq 'success' ? 1 : 0;


=head2 username

Sets and returns the username string value which is passed to the web service as "api_username"



#see SMS::Send::Driver::WebService->username

=head2 password

Sets and returns the password string value which is passed to the web service as "api_password"



#see SMS::Send::Driver::WebService->password

=head2 did

Sets and returns the "did" string value (Direct Inward Dialing Number aka the From Phone Number) which is passed to the web service as "did".


sub did {
  my $self       = shift;
  $self->{'did'} = shift if @_;
  $self->{'did'} = $self->cfg_property('did') unless defined $self->{'did'};
  die('Error: property did required (Direct Inward Dialing Phone Number)') unless defined $self->{'did'};
  return $self->{'did'};

=head2 url
Sets and returns the url for the web service.
#see SMS::Send::Driver::WebService->url
sub _url_default {''};
sub _protocol_default {'https'};
sub _host_default {''};
sub _port_default {443};
sub _script_name_default {'/api/v1/rest.php'};

=head1 SEE ALSO

L<VoIPms>, L<>, L<>

=head1 AUTHOR

Michael R. Davis, mrdvt92


Copyright (C) 2020 by Michael R. Davis

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.16.3 or,
at your option, any later version of Perl 5 you may have available.