#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 7;
use Tvh::Htsp::Client;

# we test without client connection to the HTSP server and set parameter 'no_client' to 1
# we do not want to require a HTSP server connection during module installation
my $htsp = Tvh::Htsp::Client->new( { no_client => 1, debug_info => 0 } );
can_ok('Tvh::Htsp::Client', qw(new getChanUuidId getChanNamId htsp_send_recv htsp_send htsp_recv htsmsg_deserialise htsmsg_serialise DESTROY));
isa_ok( $htsp, 'Tvh::Htsp::Client', "\$htsp" );

# without connection to the HTSP server we can test serialise and deserialise methods
my $msg = { method => 'hello', htspversion => 43, clientname => 'Tvh::Htsp::Client', clientversion => "v$Tvh::Htsp::Client::VERSION" };
my $htspmsg = $htsp->htsmsg_serialise($msg);    # serialise
my $msg_got = $htsp->htsmsg_deserialise(\$htspmsg);    # deserialise
ok( defined $htspmsg, "method 'htsmsg_serialise' returned defined" );
ok( defined $msg_got, "method 'htsmsg_deserialise' returned defined");
is_deeply($msg_got, $msg, "method 'htsmsg_deserialise' returned equal to simple message");
$msg = {
  method => 'api', path => 'channel/grid',
  args => {
    start => 0, limit => 99999, sort => 'number', dir => 'desc',
    filter => [ { field => 'number', type => 'numeric', value => 10, comparison => 'lt' },
              [ 10, 11, 12, {a => 'z', b => 'y'}, 'c' ] ] } };
$htspmsg = $htsp->htsmsg_serialise($msg);    # serialise
$msg_got = $htsp->htsmsg_deserialise(\$htspmsg);    # deserialise
is_deeply($msg_got, $msg, "method 'htsmsg_deserialise' returned equal to complex message");

# take two serialised messages to / from my HSTP server
$htspmsg = join '', map { chr $_ } (
  0x00, 0x00, 0x00, 0x5a, 0x03, 0x06, 0x00, 0x00, 0x00, 0x05, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
  0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x03, 0x0a, 0x00, 0x00, 0x00, 0x0f, 0x63, 0x6c, 0x69, 0x65, 0x6e,
  0x74, 0x6e, 0x61, 0x6d, 0x65, 0x48, 0x54, 0x53, 0x50, 0x20, 0x50, 0x65, 0x72, 0x6c, 0x43, 0x6c,
  0x69, 0x65, 0x6e, 0x74, 0x03, 0x0d, 0x00, 0x00, 0x00, 0x05, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74,
  0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x76, 0x30, 0x2e, 0x30, 0x33, 0x02, 0x0b, 0x00, 0x00,
  0x00, 0x01, 0x68, 0x74, 0x73, 0x70, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x2b,       0x00,
  0x00, 0x01, 0x28, 0x02, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x68, 0x74, 0x73, 0x70, 0x76, 0x65, 0x72,
  0x73, 0x69, 0x6f, 0x6e, 0x2b, 0x03, 0x0a, 0x00, 0x00, 0x00, 0x09, 0x73, 0x65, 0x72, 0x76, 0x65,
  0x72, 0x6e, 0x61, 0x6d, 0x65, 0x54, 0x76, 0x68, 0x65, 0x61, 0x64, 0x65, 0x6e, 0x64, 0x03, 0x0d,
  0x00, 0x00, 0x00, 0x13, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f,
  0x6e, 0x34, 0x2e, 0x33, 0x2d, 0x32, 0x34, 0x32, 0x34, 0x7e, 0x67, 0x62, 0x66, 0x63, 0x63, 0x30,
  0x30, 0x31, 0x34, 0x32, 0x04, 0x09, 0x00, 0x00, 0x00, 0x20, 0x63, 0x68, 0x61, 0x6c, 0x6c, 0x65,
  0x6e, 0x67, 0x65, 0x2b, 0xb7, 0x65, 0x14, 0xa7, 0x2a, 0xe0, 0x87, 0x79, 0xe2, 0x6b, 0xb7, 0x3f,
  0x99, 0xbe, 0xcd, 0x37, 0xb1, 0x57, 0x63, 0xea, 0x56, 0xf1, 0x78, 0xcc, 0x20, 0xfb, 0x6f, 0xb0,
  0x3f, 0x7d, 0x19, 0x03, 0x08, 0x00, 0x00, 0x00, 0x0b, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67,
  0x65, 0x67, 0x65, 0x72, 0x2c, 0x65, 0x6e, 0x67, 0x2c, 0x66, 0x72, 0x65, 0x05, 0x10, 0x00, 0x00,
  0x00, 0x67, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x63, 0x61, 0x70, 0x61, 0x62, 0x69, 0x6c, 0x69,
  0x74, 0x79, 0x03, 0x00, 0x00, 0x00, 0x00, 0x08, 0x63, 0x61, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x74, 0x76, 0x61, 0x64, 0x61, 0x70, 0x74, 0x65, 0x72, 0x73,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x61, 0x74, 0x69, 0x70, 0x5f, 0x63, 0x6c, 0x69, 0x65,
  0x6e, 0x74, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x61, 0x74, 0x69, 0x70, 0x5f, 0x73, 0x65,
  0x72, 0x76, 0x65, 0x72, 0x03, 0x00, 0x00, 0x00, 0x00, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x68,
  0x69, 0x66, 0x74, 0x03, 0x00, 0x00, 0x00, 0x00, 0x05, 0x74, 0x72, 0x61, 0x63, 0x65, 0x03, 0x00,
  0x00, 0x00, 0x00, 0x05, 0x6c, 0x69, 0x62, 0x61, 0x76, 0x02, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x61,
  0x70, 0x69, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x13
);
# deserialise the messages
$msg_got=[];
while (length $htspmsg) {
  my $bd = $htsp->htsmsg_deserialise(\$htspmsg);
  push ($msg_got->@*, $bd);
}
# equivalent perl data structure
my $msg_expected = [
  { clientversion => "v0.03", method => "hello", htspversion => 43, clientname => "HTSP PerlClient" },
  { api_version => 19, serverversion => "4.3-2424~gbfcc00142", language => "ger,eng,fre", servername => "Tvheadend",
    servercapability => [ "caclient", "tvadapters", "satip_client", "satip_server", "timeshift", "trace", "libav" ], htspversion => 43,
    challenge => "2bb76514a72ae08779e26bb73f99becd37b15763ea56f178cc20fb6fb03f7d19" }
];
is_deeply($msg_got, $msg_expected, "method 'htsmsg_deserialise' returned equal to '\$msg_expected'");    # compare the two data structures
