#!/usr/bin/perl

use Test::WWW::Mechanize::CGIApp;
use Test::Builder::Tester;

use strict;
use warnings;

use DateTime;
use POSIX;

use Test::More tests => 47; # use Test::More 'no_plan';

=begin # tests:
0) check expected tests allocated to screen term
1) load 1st unscreened new request
2) follow 'screen' link
3) submit new screen data
4) check 'screen' link disappeared
5) check 'screened as' text displayed
6) check expected tests requested
7) check direct link to screen generates error
8) create & load 2nd unscreened new request
9) change initial_screen & check tests
=cut

BEGIN {
    require 't/test-lib.pl';

    use DateTime;
    DateTime->DefaultLocale('en_GB');
}

my $mech = get_mech();

my $dbh;

eval {
    $dbh = get_dbh() or die 'no database handle recieved from get_dbh';
};

warn $@ if $@;

do_login();

# check tests allocated to screen term:
$mech->get_ok('/admin/screen_test/list/1');                   # print_and_exit();
{
    $mech->content_contains(
        q!<option value="1" selected>AML</option>!,
        'OK: expected screen term loaded'
    );
    
    $mech->has_tag_like(
        strong => qr(Flow \[1\]),
        'OK: expected number of tests allocated (1)'
    );
    $mech->has_tag_like(
        strong => qr(Molecular \[1\]),
        'OK: expected number of tests allocated (2)'
    );
    
    # AML, APML:
    foreach (2,5) {
        $mech->content_like(
            qr(value="$_" checked),
            'OK: expected test allocated'
        );
    }
    
    foreach (1,3,4) {
        $mech->content_lacks(
            qr(value="$_" checked),
            'OK: expected test not allocated'
        );
    }
    
    $mech->content_contains(
        'AML partial panel',
        'OK: flow details text loaded'
    );
}

# load main summary page for record #1:
$mech->get_ok('/search/=/1');                                 # print_and_exit();
{
    $mech->content_contains(
        '&raquo; Record Search',
        'OK: initial summary page loaded',
    );
    
    # follow 'Screen' link:
    $mech->follow_link( text => 'Screen', n => 1 );           # print_and_exit();
    
    $mech->content_contains(
        '&raquo; Initial Screen',
        'OK: initial screen page loaded',
    );
    
    $mech->submit_form(
        fields => {
            screen_id => 1, # AML
            option_id => 1,
        }
    );                                                         # print_and_exit();

    $mech->content_lacks(
        q!<a href="http://localhost/screen/=/1">Screen</a>!,
        'OK: link to initial-screen function disabled',
    );
    
    $mech->has_tag_like(
        p => qr(Screened as AML),
        'OK: initial_screen function succeeded',
    );                                                        # print_and_exit();
}

# check expected tests requested:
{
    foreach(qw/AML APML/, ) { # 'Flow details' - not displayed on 'view' page anymore
        $mech->has_tag_like(
            span => qr($_\:),
            'OK: expected test requested',
        );
    }

    foreach (qw/CML PNH HIV/) {
        test_out( 'not ok 1 - foo' );
        test_fail( +1 );
        $mech->has_tag_like( span => qr($_\:), 'foo' );
        test_test( 'OK: expected lab test not requested' );
    }                                                          
    
    $mech->has_tag(
        span => '[pending]',
        'OK: expected test status detected',
    );                                                        # print_and_exit();
    
    # get history:
    $mech->get_ok('/history/=/1');                            # print_and_exit();    
    $mech->has_tag(
        td => 'screened',
        'OK: expected history found',
    );    
}

# try to load initial-screen function again:
$mech->get_ok('/screen/load/1');                              # print_and_exit();

$mech->content_contains(
    q!Record already screened as "AML"!,
    'OK: repeat initial_screen function blocked',
);                                                            # print_and_exit();

# register another request direct:
my $dbix = get_dbix();
{
    my %request = (
        request_number => 3,
        year => DateTime->now->year,
        patient_case_id => 1,
        referrer_department_id => 1,
        created_at => DateTime->now,
    );
    $dbix->insert('requests', \%request);
}
$mech->get_ok('/screen/=/2');                                 # print_and_exit();
{
    $mech->submit_form(
        fields => {
            screen_id => 2, # PNH
            option_id => 1,
        }
    );                                                       #  print_and_exit();
    
    # check expected tests requested:
    $mech->has_tag_like(
        span => qr(PNH\:),
        'OK: expected test requested',
    );

    foreach(qw/AML HIV APML CML/, ) { # 'Flow details' - not displayed anymore
        test_out( 'not ok 1 - foo' );
        test_fail( +1 );
        $mech->has_tag_like( span => qr($_\:), 'foo' );
        test_test( 'OK: expected lab test not requested' );
    }
}

# change initial_screen:
$mech->get_ok('/request/edit/2');                             # print_and_exit();
{
    $mech->follow_link_ok( 
        { url_regex => qr(screen/edit/2) },
        'OK: followed edit screen link',
    );                                                        # print_and_exit();
    
    $mech->has_tag(
        h3 => 'Change initial screen:',
        'OK: expected title found',
    );
    
    $mech->field( screen_id => 1 ); # AML
    $mech->submit();                                          # print_and_exit();
    
    # expect new AML tests (AML, AMPL, Flow details) + original PNH & not CML or HIV:
    foreach(qw/AML APML PNH/, ) { # 'Flow details' - not displayed anymore
        $mech->has_tag_like(
            span => qr($_\:),
            'OK: expected test requested',
        );
    }
    foreach(qw/HIV CML/) {
        test_out( 'not ok 1 - foo' );
        test_fail( +1 );
        $mech->has_tag_like( span => qr($_\:), 'foo' );
        test_test( 'OK: expected lab test not requested' );
    }                                                         # print_and_exit();
    
    $mech->get_ok('/history/=/2');                            # print_and_exit();
    $mech->has_tag(
        td => 'updated initial screen from PNH',
        'OK: expected history found',
    );
}

do_logout();
