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

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

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

# go to projects
$d->find_element('//li[@id="ctl00_ProjectsMenuItem"]/a')->click();
$d->pause( 1 * $sec );

# go to the Metformin study
$d->find_element('//span[@id="ctl00_MC_ProjectList_ListView"]//a[@title="Metformin"]')->click();
$d->pause( 1 * $sec );

# go to the variable groups panel
$d->find_element('//a[@id="ctl00_MC_VariableGroupsButton"]')->click();
$d->pause( 1 * $sec );

# go to the variable group
$d->find_element('//a[@variablegroupname="Serum Chemistry (CHM)"]')->click();
$d->pause( 1 * $sec );

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

while (<DATA>) {
    chomp;
    my ( $type, $var ) = split /,/;

    # add a new variable
    $d->find_element('//a[@id="ctl00_MC_VariableList_NewVariableButton"]')->click();
    $d->pause( 1 * $sec );

    # set attributes
    $d->find_element('//input[@id="ctl00_MC_VariableUpdate_Attributes_NameText"]')->send_keys( $type . ' unavailable' );

    my $input = $d->find_element('//textarea[@id="ctl00_MC_VariableUpdate_Attributes_DataEntryPromptText"]');
    $input->clear();
    $input->send_keys('<i>not available</i>');

    my $code = $d->find_element('//input[@id="ctl00_MC_VariableUpdate_Attributes_Code"]');
    $code->clear();
    $code->send_keys($var);

    $d->find_element('//select[@id="ctl00_MC_VariableUpdate_Attributes_VariableTypeDrop"]/option[@value="11"]')->click();
    $d->pause( 0.5 * $sec );

    $d->find_element('//a[@href="#LayoutTab"]')->click();
    $d->pause( 0.5 * $sec );

    $d->find_element('//select[@id="ctl00_MC_VariableUpdate_Layout_LayoutTemplateDrop"]/option[@value="2"]')->click();

    $d->find_element('//input[@id="ctl00_MC_VariableUpdate_Layout_PromptWidthText"]')->send_keys("150");

    $d->find_element('//div[@aria-labelledby="ui-dialog-title-VariableUpdateDialog"]//button[text()="Continue"]')->click();
    $d->pause( 1 * $sec );
}

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

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

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

__END__
Sodium,CHMNA_na
Potassium,CHMPO_na
Chloride,CHMCHL_na
CO2,CHMCO_na
Fasting glucose,CHMGLU_na
BUN,CHMBUN_na
Creatinine,CHMCR_na
AST/SGOT,CHMAST_na
ALT/SGPT,CHMALT_na
Hemoglobin A1C,CHMHGB_na
Vitamin B12,CHMB12_na
Total cholesterol,CHMCHO_na
LDL,CHMLDL_na
HDL,CHMHDL_na
Triglycerides,CHMTRI_na
Serum lactate,CHMLAC_na
Insulin,CHMINS_na