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

my $uname         = 'XXX';
my $pword         = 'XXX';
my $study         = 'XXX';
my $download_type = 'CSV';
my $sec           = 1000;

# create the profile to let things download
my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
$profile->set_preference( 'browser.download.useDownloadDir'        => 1 );
$profile->set_preference( 'browser.download.dir'                   => '/Users/rem16/Desktop/dls' );
$profile->set_preference( 'browser.download.folderList'            => 2 );
$profile->set_preference( 'browser.helperApps.alwaysAsk.force'     => 0 );
$profile->set_preference( 'browser.helperApps.neverAsk.saveToDisk' => 'text/csv, application/octet-stream, */*' );

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

# 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();

# go to datasets
$d->find_element('//li[@id="ctl00_DataSetsMenuItem"]/a')->click();

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

# get the list of datasets
my $export_links = [ map { $_->get_attribute('title'); } $d->find_elements('//span[@id="ctl00_MC_DataSetList_ListView"]//a[starts-with(@title, "Export ")]') ];

# download each data set
foreach my $link (@$export_links) {

    # reset to a known location (go to datasets, select the study)
    $d->find_element('//li[@id="ctl00_DataSetsMenuItem"]/a')->click();
    $d->find_element( '//select[@name="ctl00$ToolbarContent$ProjectDrop$ctl01"]/option[.="' . $study . '"]' )->click();

    # click the download link
    $d->find_element( '//a[@title="' . $link . '"]' )->click();
    $d->pause( 1 * $sec );

    # go into the download pane
    $d->find_element('//a[@id="ctl00_MC_DownloadButton"]')->click();
    $d->pause( 1 * $sec );

    # find the correct download link and click it
    $d->find_element( '//table[@class="DownloadTypes"]/tbody/tr[.//h1/span[text()="' . $download_type . '"]]//a[text()="Download"]' )->click();
    $d->pause( 1 * $sec );

    # click the "Back" toolbar button to get back to a known state
    $d->find_element('//a[@id="ctl00_ToolbarContent_BackButton"]')->click();

    # pause briefly so we don't overload the network
    $d->pause( 1 * $sec );

}

# wait 10 seconds for any downloads to finish
$d->pause( 10 * $sec );

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