#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }

require cfg ;

use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

use LWP::UserAgent;
use HTTP::Request;
use XML::Simple ;
use DBI;
use SOAP::Lite ;

#-------------------------------------------------------------------------------

    @ARGV        = split(/\\*\&/, $ENV{'QUERY_STRING'});
our $debug       = $ARGV[0];
   
# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/pixellot_api.pl?1

#-------------------------------------------------------------------------------

&today;

&send_it ;
&parse_it ;
&read_it ;

exit;

#-------------------------------------------------------------------------------

sub send_it {
	
	my $url   = "https://api.stage.pixellot.tv/v1/login";
	# my $url   = "https://api.pixellot.tv/v1/login";
	my $json  = '{
"username" : "rory@kre8it.co.za",
"password" : "13centreP1x3110T"
}';

	&common_debug("url=$url") ;
	&common_debug("json=$json") ;

	# my $request = HTTP::Request->new(POST => $url) or die "Error: Cannot create GET request \n" ;
	my $request = HTTP::Request->new(POST => $url) or print "Error: Cannot create GET request \n" ;
	   $request -> header('content-length' => length($json)) ;
	   $request -> content($json);
	   $request -> content_type("application/json; charset=UTF-8");

	my $ua = LWP::UserAgent->new;
	my $response = $ua->request($request);

	my $response_code    = $response -> code ;
	my $response_content = $response -> content ;
	
	&common_debug("response_content=$response_content") ;
	
	eval {
		my $decoded_json = decode_json $response_content ;	
		$token = $decoded_json->{'token'} ;
	} or do {
		# print("Something went wrong: $@\n");
	};
	
	&common_debug("token=$token") ;
	
	# # return unless $token ;
	# if (!$token) {
		# &common_cms_send_mail('','','',"[.102 ERROR] post_to_world_to_africa_logistics","NO TOKEN") if $now_mm == 15 || $now_mm == 45 ;
		# return ;
	# }
	
	# my $url   = "http://102.219.138.34:8000/api/kreateit/positions";
	# my $json  = &read_file($fh);

	# &common_debug("url=$url") ;
	# &common_debug("json=$json") ;

	# my $request = HTTP::Request->new(POST => $url) or print "Error: Cannot create GET request \n" ;
	   # $request -> header(Authorization => "Bearer $token");
	   # $request -> header('content-length' => length($json)) ;
	   # $request -> content($json);
	   # $request -> content_type("application/json; charset=UTF-8");

	# my $ua = LWP::UserAgent->new;
	# my $response = $ua->request($request);

	# my $response_code    = $response -> code ;
	# my $response_content = $response -> content ;

	# &common_debug("response_code=$response_code") ;
	# &common_debug("response_content=$response_content") ;

	# &common_cms_time_log("world_to_africa_response.txt","response_code=$response_code\nresponse_content=$response_content",$apipath) ;
	# unlink ($fh) or &common_cms_send_mail('','','',$err_subj,$!)  ;
    
} #-------------------------------------------------------------------------------

sub parse_it {

	# # use Data::Dumper;
	# # print Dumper(\$returnxml);
	# # exit;

	# $returnxml =~ s/[\x80-\xFF]/?/g ;
	# my $xml = new XML::Simple or die "Cant instantiate object XML::Simple $!" ;
	# # my $xmldata = $xml->XMLin($returnxml) or die "Cant open xmldata $!" ;
	# $xmldata = $xml->XMLin($returnxml, forcearray => 1) or die "Cant open xmldata $!";

	# if ($debug) {
		# use Data::Dumper;
		# print Dumper(\$xmldata);
		# # exit ;
    # }

} #-------------------------------------------------------------------------------

sub read_it {

	# foreach my $hashref ( @{$xmldata->{'s:Body'}->[0]->{'PostalCodeResponse'}->[0]->{'PostalCodeResult'}->[0]->{'a:ArrayOfpostalCode'} } ) {
		# # unless (lc ${$hashref}{'Name'}->[0] eq lc $search)     { next ; }
		# # print ${$hashref}{'ContentEntityId'}->[0] ;
		
		# # unless (exists $exists{${$hashref}{'a:postalCode'}->[0]}) {	
		# foreach $address ( @{$hashref}{'a:postalCode'}) {	
			# # print $address . "<br>" ;
			# # print Dumper(\$address);
			# # print Dumper(\$address{'a:city'}->[0]);
			# print 'a:city=' . ${$hashref}{'a:postalCode'}->[0]->{'a:city'}->[0] . "\n" ;
			# print 'a:suburb=' . ${$hashref}{'a:postalCode'}->[0]->{'a:suburb'}->[0] . "\n" ;
			# print 'a:code=' . ${$hashref}{'a:postalCode'}->[0]->{'a:code'}->[0] . "\n" ;
			# print 'a:routeCode=' . ${$hashref}{'a:postalCode'}->[0]->{'a:routeCode'}->[0] . "\n" ;
			# # print ${$address}->[0] . "\n" ;
			# # print 'a:city=' . ${$address}{'a:city'}->[0] . "<br>" ;
			# # print 'a:routeCode=' . ${$address}{'a:routeCode'}->[0] . "<br>" ;
			# # print 'a:suburb=' . ${$address}{'a:suburb'}->[0] . "<br>" ;
			# # print 'a:code=' . ${$address}{'a:code'}->[0] . "<br>" ;
			
			# # $i{dcb_id}    = ${$hashref}{'ContentEntityId'}->[0] ;
			# # $i{latitude}   = ${$hashref}{'Latitude'}->[0] ;
			# # $i{longitude}  = ${$hashref}{'Longitude'}->[0] ;
			# # $i{country}    = ${$hashref}{'Country'}->[0] ;
			 # # $i{area}      = ${$hashref}{'Area'}->[0] ;
			 
			# # # unless (ref(${$hashref}{'Area'}->[0] eq 'HASH'))     { $i{area}      = ${$hashref}{'Area'}->[0] ; }
			# # # unless (ref(${$hashref}{'Location'}->[0] eq 'HASH')) { $i{location}  = ${$hashref}{'Location'}->[0] ; }
			
			# # $i{location}  = ${$hashref}{'Location'}->[0] unless (ref(${$hashref}{'Location'}->[0]) eq 'HASH');
			
			# # $i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
			# # &common_db_insert('dcb_places') ;
		# }
		# # last ;
	# }
    
} #-------------------------------------------------------------------------------

use common ;
use today ;

1;
