package cz.moneta.test.dsl.ufo.common.landing;

import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.ufo.cb.landing.SME451_S001_UfoCbLandingPage;
import cz.moneta.test.dsl.ufo.operations.error.ERR701_S001_LDAPErrorPage;
import cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations;
import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.BOX701_DataBoxes;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.EXT701_S001_ExternalPartnersMainPage;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.businessSupport.ROZ702_S001_BusinessSupportMainPage;
import cz.moneta.test.dsl.ufo.ufoframework.pages.HLP901_S001_MainMenuPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.common.landing.ApplicationSelectPage.CONTINUE_BUTTON;

@Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, waitSecondsForElement = 80, until = Until.GONE)
@Wait(value = CONTINUE_BUTTON, waitSecondsForElement = 80)
public interface ApplicationSelectPage extends WebFlow<ApplicationSelectPage>, StoreAccessor {

    String UFO_COMMERCIAL_BANKING_XPATH = "//input[@value='UFO_Commercial_Banking']";
    String UFO_BANKA_RADIO_XPATH = "//input[@value='UFO_Banka']";
    String UFO_OPERATIONS_RADIO_XPATH = "//input[@value='UFO_Operations']";
    String UFO_EXTERNAL_PARTNERS_RADIO_XPATH = "//input[@value='UFO_Externi_Partneri']";
    String UFO_COMMERCIAL_RADIO_XPATH = "//input[@value='UFO_Commercial_Banking']";
    String UFO_G3_FUNCTIONAL_TESTS_RADIO_XPATH = "//input[@value='UFO_G3_Test_Application_I___Funcional__tests']";
    String BUSINESS_SUPPORT_RADIO_XPATH = "//input[@value='BusinessSupport']";
    String DATABOXES_RADIO_XPATH = "//input[@value='DataBoxes']";
    String BRANCH_UFOHQ_RADIO_XPATH = "//input[@value='86001340']";
    String CONTINUE_BUTTON = "ADM062_S001_B001";
    String FIRST_OM_RADIO = "ADM062_S001_G001-0";

    /*
      According to business rules it is impossible to create customer from HQ. Source:

      <OM>02122120</OM> je nepovolené obchodní místo pro založení a správu produktů klienta,
       přenastavte si obchodní místo na standarní pobočku a nikoliv na centrálu HQ.
     */
    String BRANCH_RADIO_XPATH = "//table[@id='ADM062_S001_G001_UfoTable']//tr[%s]//input[@type='radio']";

    @Wait(value = UFO_BANKA_RADIO_XPATH, by = Lookup.XPATH, until = Until.CLICKABLE)
    @Click(value = UFO_BANKA_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, explicitWaitSeconds = 1))
    @Click(CONTINUE_BUTTON)
    HLO001_S001_ClientSearchPage selectUfoBankaAndContinue();

    @Click(CONTINUE_BUTTON)
    HLO001_S001_ClientSearchPage clickContinue();

    @Wait(value = UFO_OPERATIONS_RADIO_XPATH, by = Lookup.XPATH, until = Until.CLICKABLE)
    @Click(value = UFO_OPERATIONS_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, explicitWaitSeconds = 1))
    @Click(CONTINUE_BUTTON)
    OPS001_S001_ClientSearchPageUfoOperations selectUfoOperationsAndContinueWithoutOmSelect();

    @Click(value = BUSINESS_SUPPORT_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    ROZ702_S001_BusinessSupportMainPage selectBusinessSupportAndContinueWithoutOmSelect();

    @Click(value = DATABOXES_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    BOX701_DataBoxes selectDataboxesAndContinueWithoutOmSelect();

    @Click(value = UFO_OPERATIONS_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(value = BRANCH_UFOHQ_RADIO_XPATH, by = Lookup.XPATH)
    @Click(CONTINUE_BUTTON)
    OPS001_S001_ClientSearchPageUfoOperations selectUfoOperationsAndHQContinue();

    @Click(value = UFO_COMMERCIAL_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    SME451_S001_UfoCbLandingPage selectUfoCommercialAndContinue();

    @Click(value = UFO_G3_FUNCTIONAL_TESTS_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    HLP901_S001_MainMenuPage selectUfoFunctionalTestsAndContinue();

    @Click(value = UFO_EXTERNAL_PARTNERS_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    EXT701_S001_ExternalPartnersMainPage selectExternalPartnersAndContinueWithoutOMSelect();

    @Click(value = UFO_OPERATIONS_RADIO_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    ERR701_S001_LDAPErrorPage selectUfoOperationsAndContinueToLDAPErrorPage();

    @Click(value = UFO_COMMERCIAL_BANKING_XPATH, by = Lookup.XPATH, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(CONTINUE_BUTTON)
    SME451_S001_UfoCbLandingPage selectUfoCommercialBankingAndContinue();

    @Click(FIRST_OM_RADIO)
    ApplicationSelectPage clickFirstOM();

    @CustomAction
    default ApplicationSelectPage selectOm(int order) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String xpath = String.format(BRANCH_RADIO_XPATH, order);
        endpoint.jsClick(() -> xpath, Lookup.XPATH);
        return null;
    }

    @CustomAction
    default ApplicationSelectPage selectFirstOmIfPresent() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String xpath = String.format(BRANCH_RADIO_XPATH, "1");
        if (endpoint.isElementVisible(2, xpath, Lookup.XPATH)) {
            endpoint.click(() -> xpath, Lookup.XPATH);
        }
        return null;
    }
}
