package cz.moneta.test.dsl.ufo.banka.pages.income;

import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
import cz.moneta.test.dsl.util.data.client.NonClient;
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.banka.pages.income.PIM001_S001_IncomeEditPage.PIM001_S001_PAGE_LABEL_XPATH;

@Wait(value = PIM001_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH)
public interface PIM001_S001_IncomeEditPage extends WebFlow<PIM001_S001_IncomeEditPage>, StoreAccessor {

    String PIM001_S001_PAGE_LABEL_XPATH = "//div[text()='PIM001_S001']";
    String SEARCH_EMPLOYER_BUTTON = "PIM001_S001_B002";
    String COMPANY_NAME_ICO_SEARCH_INPUT = "PIM001_S001_E005";
    String SEARCH_COMPANY_BUTTON = "PIM001_S001_B018";
    String FIRST_COMPANY_XPATH = "//table[@id='PIM001_S001_G001_UfoTable']//input[1]";
    String COMPANY_NAME_ROW_XPATH = "//td[text()='%s']";
    String COMPANY_NAME_FIRST_ROW_CHECKBOX = "PIM001_S001_G001-0";
    String SELECT_EMPLOYER_BUTTON = "PIM001_S001_B019";
    String INCOME_CASH_NO_RADIO = "PIM001_S001_R006_0";
    String EMPLOYMENT_START_DATE_INPUT = "PIM001_S001_E008";
    String EMPLOYMENT_END_DATE_INPUT = "PIM001_S001_E003";
    String PHONE_COUNTRY_CODE_INPUT = "PIM001_S001_E001";
    String PHONE_NUMBER_INPUT = "PIM001_S001_E002";
    String NET_INCOME_INPUT = "PIM001_S001_E009";
    String SAVE_INCOME_BUTTON = "PIM001_S001_B015";
    String BACK_BUTTON = "PIM001_S001_B014";
    String NOTICE_PERIOD_NO_RADIO = "PIM001_S001_R001_0";
    String TRIAL_PERIOD_NO_RADIO = "PIM001_S001_R002_0";
    String TRIAL_PERIOD_YES_RADIO ="PIM001_S001_R002_1";
    String CONTINUE_BUTTON = "PIM001_S001_B025";
    String ADD_INCOME_BUTTON = "PIM001_S001_B022";
    String INCOME_TYPE_SELECT = "PIM001_S001_C003";
    String INCOME_SUBTYPE_SELECT = "PIM001_S001_C013";
    String OCCUPATION_SELECT = "PIM001_S001_C002";
    String ACCEPT_INCOME = "PIM001_S001_H002";

    @Click(ACCEPT_INCOME)
    PIM001_S001_IncomeEditPage clickAcceptIncome();

    @Click(SEARCH_EMPLOYER_BUTTON)
    PIM001_S001_IncomeEditPage openSearchEmployerWindow();

    @TypeInto(COMPANY_NAME_ICO_SEARCH_INPUT)
    PIM001_S001_IncomeEditPage fillCompanyNameOrIco(String companyName);

    @Click(value = SEARCH_COMPANY_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PIM001_S001_IncomeEditPage clickSearchCompanyButton();

    @Click(value = FIRST_COMPANY_XPATH, by = Lookup.XPATH)
    PIM001_S001_IncomeEditPage clickFirstCompany();

    @CustomAction
    default PIM001_S001_IncomeEditPage selectCompanyNameRow(String companyName) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String rowXpath = String.format(COMPANY_NAME_ROW_XPATH, companyName);
        endpoint.scrollIntoView(rowXpath, Lookup.XPATH);
        endpoint.click(() -> rowXpath, Lookup.XPATH);
        return null;
    }

    @Click(COMPANY_NAME_FIRST_ROW_CHECKBOX)
    PIM001_S001_IncomeEditPage clickFirstResult();

    @Click(value = SELECT_EMPLOYER_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PIM001_S001_IncomeEditPage clickSelectEmployerButton();

    @Click(value = INCOME_CASH_NO_RADIO, jsClick = true)
    PIM001_S001_IncomeEditPage clickIncomeCashNoRadio();

    @Click(value = NOTICE_PERIOD_NO_RADIO, jsClick = true)
    PIM001_S001_IncomeEditPage clickNoticePeriodNoRadio();

    @Click(value = TRIAL_PERIOD_NO_RADIO, jsClick = true)
    PIM001_S001_IncomeEditPage clickTrialPeriodNoRadio();

    @Click(value = TRIAL_PERIOD_YES_RADIO, jsClick = true)
    PIM001_S001_IncomeEditPage clickTrialPeriodYesRadio();

    @CustomAction
    default PIM001_S001_IncomeEditPage clickTrialPeriod(NonClient nonClient){
        if (nonClient.occupationTrialPeriod()) clickTrialPeriodYesRadio(); else clickTrialPeriodNoRadio();
        return null;
    }

    @TypeInto(value = EMPLOYMENT_START_DATE_INPUT, clear = true)
    PIM001_S001_IncomeEditPage fillEmploymentStartDate(String date);

    @TypeInto(value = EMPLOYMENT_END_DATE_INPUT, clear = true)
    PIM001_S001_IncomeEditPage fillEmploymentEndDate(String date);

    @TypeInto(value = PHONE_COUNTRY_CODE_INPUT, clear = true)
    PIM001_S001_IncomeEditPage fillPhoneCountryCode(String countryCode);

    @TypeInto(value = PHONE_NUMBER_INPUT, clear = true)
    PIM001_S001_IncomeEditPage fillPhoneNumber(String phoneNumber);

    @TypeInto(value = NET_INCOME_INPUT, clear = true)
    PIM001_S001_IncomeEditPage fillNetIncome(int netIncome);

    @Click(value = SAVE_INCOME_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 40))
    PIM001_S001_IncomeEditPage clickAcceptIncomeButton();

    @Click(value = ADD_INCOME_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PIM001_S001_IncomeEditPage clickAddIncomeButton();

    @Select(value = INCOME_TYPE_SELECT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PIM001_S001_IncomeEditPage selectIncomeType(String incomeType);

    @Select(value = INCOME_SUBTYPE_SELECT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PIM001_S001_IncomeEditPage selectIncomeSubtype(String incomeSubtype);

    @Select(value = OCCUPATION_SELECT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PIM001_S001_IncomeEditPage selectOccupation(String occupation);

    @Click(BACK_BUTTON)
    PIM001_S001_IncomeEditPageTransitions clickBackButton();

    @CustomAction
    default PIM001_S001_IncomeEditPage clickAcceptIncomeButtonAndDisableAlertIfVisible() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        this.clickAcceptIncomeButton();
        if ((endpoint.isAlertPresent(3)) &&
                (endpoint.getAlertText(1).contains("Příjem není možné uložit"))) {
            endpoint.dismissAlert(1);
            this.selectIncomeType("Příjem ze závislé činnosti")
                    .selectIncomeSubtype("Příjem ze závislé činnosti na dobu neurčitou")
                    .selectOccupation("it programátor/software developer")
                    .clickAcceptIncomeButton();
        }
        return null;
    }
}