#!/usr/bin/env perl
use Modern::Perl '2012';
use Selenium::Remote::Driver;
use Try::Tiny;

my $uname        = 'XXX';
my $pword        = 'XXX';
my $study        = 'XXX';
my $base_subject = 'XXX';
my $sec          = 1000;

# get a driver object, already logged in and at the base subject
sub get_driver {

    # connect to the Selenium server
    my $d = Selenium::Remote::Driver->new(
        'browser_name'   => 'firefox',
        'default_finder' => 'xpath',
    );

    # log in
    $d->get('https://studytrax.partners.org/StudyTrax/Account/Login.aspx');
    $d->find_element('//input[@id="ctl00_MC_Username"]')->send_keys($uname);
    $d->find_element('//input[@id="ctl00_MC_Password"]')->send_keys($pword);
    $d->find_element('//input[@id="ctl00_MC_LoginButton"]')->click();

    # make sure we are at the home item
    $d->find_element('//li[@id="ctl00_HomeMenuItem"]/a')->click();
    $d->pause( 1 * $sec );

    # select the study
    $d->find_element( '//select[@name="ctl00$ToolbarContent$ProjectDrop$ctl01"]/option[.="' . $study . '"]' )->click();
    $d->pause( 1 * $sec );

    # get to a first subject
    $d->find_element('//input[@id="ctl00_MC_ReferenceIdText"]')->send_keys($base_subject);
    $d->find_element('//input[@id="ctl00_MC_ReferenceIdSearchButton"]')->click();
    $d->pause( 2 * $sec );

    return $d;
}

my $d = get_driver();

while (<DATA>) {
    chomp;
    next unless m/\S/;
    next if m/^#/;
    my $id = $_;

    print "$id ..";

    try {

        # go to the subject
        $d->find_element('//input[@id="ctl00_ToolbarContent_SearchParameter"]')->send_keys($id);
        $d->find_element('//input[@id="ctl00_ToolbarContent_GoButton"]')->click();
        $d->pause( 0.5 * $sec );

        # get a list of the unknown intervals
        my @unknowns = $d->find_elements('//span[@id="ctl00_MC_Dashboard_Timeline_ListView"]/a[./div[contains(@class, "Unknown")]]');

        # while we still have unknowns
        while ( scalar(@unknowns) ) {

            # get the first unknown interval
            my $interval = $d->find_element('//span[@id="ctl00_MC_Dashboard_Timeline_ListView"]/a[./div[contains(@class, "Unknown")]]');
            my $name = $d->find_child_element( $interval, './/div[@class="Name"]' )->get_text();
            print " '$name'";

            # go to it
            $interval->click();
            $d->pause( 0.5 * $sec );

            # recalculate completion status
            $d->find_element('//a[@id="ctl00_ToolbarContent_RecalculateVariableGroupStatusesButton"]')->click();
            $d->pause( 1 * $sec );

            # save and return
            $d->find_element('//a[@id="ctl00_ToolbarContent_SaveAndReturnButton"]')->click();
            $d->pause( 0.5 * $sec );

            print " ok";

            # get a new list of unknowns
            @unknowns = $d->find_elements('//span[@id="ctl00_MC_Dashboard_Timeline_ListView"]/a[./div[contains(@class, "Unknown")]]');
        }

        say " done";
    }
    catch {

        say " failed !";

        # get a new browser session and continue
        $d->quit();
        $d = get_driver();
    };
}

# end by going home
$d->find_element('//li[@id="ctl00_HomeMenuItem"]/a')->click();
$d->pause( 1 * $sec );

# and log out
$d->find_element('//a[@class="LogoutLink"]')->click();

# close the Selenium server connection
$d->quit();

__END__