use strict;
use warnings;
BEGIN { eval q{ use EV } }
use AnyEvent::WebSocket::Client;
use Test::More;
use FindBin ();
use lib $FindBin::Bin;
use testlib::Server;


eval "use Net::SSLeay 1.33 (); 1" or plan skip_all => "test requires Net::SSLeay 1.33";
eval "use AnyEvent::TLS (); 1"    or plan skip_all => "test requires AnyEvent::TLS";

plan skip_all => 'user requested skip of SSL tests via environment'
  if $ENV{ANYEVENT_WEBSOCKET_TEST_SKIP_SSL};

plan tests => 3;

testlib::Server->set_timeout;

my $counter;
my $max;

my $uri = testlib::Server->start_server(
  tls => AnyEvent::TLS->new(cert => do { local $/; <DATA> }),
  handshake => sub {  # handshake
    my $opt = { @_ };
    $counter = 1;
    $max = 15;
    note "max = $max";
    note "resource = " . $opt->{handshake}->req->resource_name;
    if($opt->{handshake}->req->resource_name =~ /\/count\/(\d+)/)
    { $max = $1 }
    note "max = $max";
  },
  message => sub {  # message
    my $opt = { @_ };
    eval q{
      note "send $counter";
      $opt->{hdl}->push_write($opt->{frame}->new($counter++)->to_bytes);
      if($counter >= $max)
      {
        $opt->{hdl}->push_write($opt->{frame}->new(type => 'close')->to_bytes);
        $opt->{hdl}->push_shutdown;
      }
    };
  },
);

$uri->path('/count/10');
note $uri;

my $connection = eval { AnyEvent::WebSocket::Client->new( ssl_no_verify => 1 )->connect($uri)->recv };

if(my $error = $@)
{
  diag "";
  diag "";
  diag " == NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE ==";
  diag "";
  diag "  Recent versions of AnyEvent, in combination of Net::SSLeay";
  diag "  broke the SSL test for AnyEvent::WebSocket::Client.";
  diag "  Please see the GitHub issue tracker for up to date information:";
  diag "";
  diag "  https://github.com/plicease/AnyEvent-WebSocket-Client/issues/22";
  diag "";
  diag "  If SSL is not important for your use case, you may consider";
  diag "  installing AnyEvent::WebSocket::Client anyway.  It should work";
  diag "  fine over non-encrypted channels.  You can set the environment";
  diag "  variable ANYEVENT_WEBSOCKET_TEST_SKIP_SSL to skip the SSL tests.";
  diag "";
  diag "  Patches to fix this will be gladly accepted.";
  diag "";
  diag " == NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE ==";
  diag "";
  diag "";
  die $error;
}

isa_ok $connection, 'AnyEvent::WebSocket::Connection';

my $done = AnyEvent->condvar;

$connection->send('ping');

my $last;

$connection->on(each_message => sub {
  my $message = pop->body;
  note "recv $message";
  $connection->send('ping');
  $last = $message;
});

$connection->on(finish => sub {
  $done->send(1);
});

is $done->recv, '1', 'friendly disconnect';

is $last, 9, 'last = 9';

__DATA__
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA02VwAqlQzCrPenkxUjawHcXzJreJ9LDhX7Bkg3E/RB6Ilm4D
LBeilCmzkY7avp57+WCiVw2qkg+kH4Ef2sd+r10UCGPh/1diLehRAzp3Ho1bixyg
w+zkDm79OnN3uHxuKigkAxx3GGz9HhQA83U+RUns+39/OnFh0RY6/f5rV2ziA9jD
6HK3Mnsuxocd46YbVdiqlQK430CgiGj8dV44JG6+R6x3r5qXDbbRtGubC29kQOUq
kYslbpTo7ml8ShyqAP6qa8BpeSIaNG1CQQ/7JkAdxSWyFHqMQ0HR3BUiaEfUElZt
DFgXcCkKB5F8jx+wYoLzlPHHZaUvfP2nueYjcwIDAQABAoIBAQCtRDMuu0ByV5R/
Od5nGFP500mcrkrwuBnBqH56DdRhLPWe9sS62xRyhEuePoykOJo8qCvnVlg8J33K
JLfLRkBb09qbleKiuyjJn+Tm1IDWFd62gtxyOjQicG41/nZeS/6vpv79XdNvvcUp
ZhPxeGN1v0XyTWomqNAX5DSuAl5Q5HxkaRYNeuLZaPYkqmEVTgYqNSes/wRLKUb6
MaVrZ9AA/oHJMmmV4evf06s7l7ICjxAWeas7CI6UGkEz8ZFoVRJsLk5xtTsnZLgf
f24/pqHz1vApPs7CsJhK2HsLZcxMPD+hmTNI/Njl51WoH8zGhkv+p88vDzybpNSF
Hpkl+ZlBAoGBAO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