package cz.moneta.test.dsl.broadcom;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.broadcom.BroadcomEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.TypeInto;
import cz.moneta.test.harness.support.web.WebFlow;

public interface BroadcomHomePage extends WebFlow<BroadcomHomePage>, StoreAccessor {

    String LEFT_MENU_FRAME = "//frame[@name='leftFrame']";
    String MAIN_MENU_FRAME = "//frame[@name='mainFrame']";
    String ALL_TRANSTACTIONS_LINK = "//a[text()='All Transactions']";
    String INPUT_FROM_YEAR = "//input[starts-with(@id, 'reportForm:j_id_1') and preceding-sibling::text()[1][contains(., 'Year')]]";
    String INPUT_FROM_MONTH = "//input[starts-with(@id, 'reportForm:j_id_1') and preceding-sibling::text()[1][contains(., 'Month')]]";
    String INPUT_FROM_DAY = "//input[starts-with(@id, 'reportForm:j_id_1') and preceding-sibling::text()[1][contains(., 'Day')]]";
    String INPUT_FROM_HOUR = "//input[starts-with(@id, 'reportForm:j_id_1') and preceding-sibling::text()[1][contains(., 'Hour')]]";
    String INPUT_FROM_MINUTE = "//input[starts-with(@id, 'reportForm:j_id_1') and preceding-sibling::text()[1][contains(., 'Min')]]";
    String INPUT_TO_YEAR = "//input[starts-with(@id, 'reportForm:j_id_2') and preceding-sibling::text()[1][contains(., 'Year')]]";
    String INPUT_TO_MONTH = "//input[starts-with(@id, 'reportForm:j_id_2') and preceding-sibling::text()[1][contains(., 'Month')]]";
    String INPUT_TO_DAY = "//input[starts-with(@id, 'reportForm:j_id_2') and preceding-sibling::text()[1][contains(., 'Day')]]";
    String INPUT_TO_HOUR = "//input[starts-with(@id, 'reportForm:j_id_2') and preceding-sibling::text()[1][contains(., 'Hour')]]";
    String INPUT_TO_MINUTE = "//input[starts-with(@id, 'reportForm:j_id_2') and preceding-sibling::text()[1][contains(., 'Min')]]";
    String EXPORT_BUTTON = "//input[@id='reportForm:btnExport']";

    @CustomAction
    default BroadcomHomePage switchFrameAndGoToAllTransactions() {
        BroadcomEndpoint endpoint = getEndpoint(BroadcomEndpoint.class);
        endpoint.switchToFrame(LEFT_MENU_FRAME);
        endpoint.click(() -> ALL_TRANSTACTIONS_LINK);
        endpoint.switchToDefaultContent();
        endpoint.switchToFrame(MAIN_MENU_FRAME);
        endpoint.waitForElementsToLoad(10, EXPORT_BUTTON);
        return null;
    }

    @TypeInto(value = INPUT_FROM_YEAR, clear = true)
    @TypeInto(value = INPUT_FROM_MONTH, clear = true)
    @TypeInto(value = INPUT_FROM_DAY, clear = true)
    @TypeInto(INPUT_FROM_HOUR)
    @TypeInto(INPUT_FROM_MINUTE)
    BroadcomHomePage fillFromDateTime(String year, String month, String day, String hour, String min);

    @TypeInto(value = INPUT_TO_YEAR, clear = true)
    @TypeInto(value = INPUT_TO_MONTH, clear = true)
    @TypeInto(value = INPUT_TO_DAY, clear = true)
    @TypeInto(INPUT_TO_HOUR)
    @TypeInto(INPUT_TO_MINUTE)
    BroadcomHomePage fillToDateTime(String year, String month, String day, String hour, String min);

    @CustomAction
    default BroadcomHomePage clickToExportAndDownloadFile(String dates) {
        BroadcomEndpoint endpoint = getEndpoint(BroadcomEndpoint.class);
        String expectedFileName = "AllTransactionsExtended_" + dates + ".csv";
        log("Starting download of file: {}", expectedFileName);
        endpoint.click(() -> EXPORT_BUTTON);
        endpoint.checkIfFileExists(expectedFileName, 180, null, false, true);
        return null;
    }
}
