#!perl -T
use 5.006;
use strict;
use warnings FATAL => 'all';
use Test::More;
use Test::LWP::UserAgent;
use Test::Exception;
use HTTP::Response;
use Finance::Nadex;
use Finance::Nadex::Contract;
use Finance::Nadex::Order;
use Finance::Nadex::Position;

my $gooduseragent = Test::LWP::UserAgent->new;

$gooduseragent->cookie_jar( { autosave => 1, ignore_discard => 1 } );
$gooduseragent->map_response(
    qr{iDeal/login}, 
    HTTP::Response->new('200', 'OK', ['Content-Type' => 'application/json;charset=UTF-8', 'X-SECURITY-TOKEN' => 123456, 'Set-Cookie' => 'JSESSIONID=12345'], ''));
   
$gooduseragent->map_response(
    qr{iDeal/markets/details/position}, 
    HTTP::Response->new('200', 'OK', ['Content-Type' => 'application/json;charset=UTF-8', 'X-SECURITY-TOKEN' => 123456, 'Set-Cookie' => 'JSESSIONID=12345'], '{ "market" : { "instrumentName": "GBP/USD >1.5160 (3PM)", "displayOffer": "85.00", "displayBid": "75.00" }, "position" : { "direction": "buy", "openLevel": "45.00", "dealId": "NZ123DF56GFSZA2ZX", "epic": "NT.D.GBP-USD-12-100-1-12Jan15", "dealSize": "2"  } }'));
 
my $baduseragent = Test::LWP::UserAgent->new;

$baduseragent->cookie_jar( { autosave => 1, ignore_discard => 1 } );
$baduseragent->map_response(
    qr{iDeal/login}, 
    HTTP::Response->new('200', 'OK', ['Content-Type' => 'application/json;charset=UTF-8'], ''));

BAD_LOGIN: {
   my $client = Finance::Nadex->new();
   $client->{user_agent} = $baduseragent;
   dies_ok { $client->retrieve_position( position_id => 'NZ123XCOO982XZ' ) } 'must be logged in to retrieve a position';
}

my $client = Finance::Nadex->new();
$client->{user_agent} = $gooduseragent;

$client->login( username => 'someusername', password => 'somepassword');

dies_ok {  $client->retrieve_position() } 'must supply all required arguments to retrieve a position';

my $position;

ok ( $position = $client->retrieve_position( id => 'NZ123XCOO982X' ), 'position retrieved when all arguments provided');
ok ( $position->id(), "retrieved position contains an id" );
ok ( $position->direction(), "retrieved position contains a direction" );
ok ( $position->bid(), "retrieved position contains a bid" );
ok ( $position->offer(), "retrieved position contains an offer");
ok ( $position->size(), "retrieved position contains a size");
ok ( $position->price(), "retrieved position contains a price");
ok ( $position->epic(), "retrieved position contains an epic");
ok ( $position->contract(), "retrieved position contains a contract");
done_testing();
