package cz.moneta.test.dsl.hypos.pages.hypokalkulacka;

import cz.moneta.test.dsl.hypos.enums.CorrectionType;
import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.dsl.util.data.hypos.HyposContract;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.hypos.pages.hypokalkulacka.HypokalkulackaClientExpenditurePage.SAVE_EXPENDITURE_BUTTON;

@Wait(value = SAVE_EXPENDITURE_BUTTON, until = Until.VISIBLE)
public interface HypokalkulackaClientExpenditurePage extends HyposPageFlow<HypokalkulackaClientExpenditurePage>, StoreAccessor {
    String EXPENDITURE_INPUT_DIV = "//div[@id='expenditure-input']";
    String EXPENDITURE_ACTIVE_ROW = EXPENDITURE_INPUT_DIV + "//tbody[(contains(@class, 'listing'))]/tr[contains(@class,'pending')]";
    String EXPENDITURE_TYPE_SELECT = EXPENDITURE_ACTIVE_ROW + "/td/select[contains(@class,'expenditure-type')]";
    String EXPENDITURE_CORRECTION_SELECT = EXPENDITURE_ACTIVE_ROW + "/preceding::tr[1]/td/select[contains(@class,'expenditure-correction')]";
    String EXPENDITURE_INSTALMENT_AMOUNT_INPUT = EXPENDITURE_ACTIVE_ROW + "/preceding::tr[1]/td/input[contains(@class,'expenditure-instalment-amount')]";

    String SAVE_EXPENDITURE_BUTTON = EXPENDITURE_INPUT_DIV + "//input[@value='Uložit']";

    @CustomAction
    default HypokalkulackaClientExpenditurePage setExpenditures(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getClients().get(0).getExpenditures().size(); i++) {
            endpoint.selectByVisibleText(EXPENDITURE_TYPE_SELECT, hyposContract.getClients().get(0).getExpenditures().get(i).getType());
            endpoint.type(() -> EXPENDITURE_INSTALMENT_AMOUNT_INPUT, hyposContract.getClients().get(0).getExpenditures().get(i).getAmount(), true);
            if (!hyposContract.getClients().get(0).getExpenditures().get(i).getCorrection().equals(CorrectionType.NO_CORRECTION.getValue())){
                endpoint.selectByVisibleText(EXPENDITURE_CORRECTION_SELECT, hyposContract.getClients().get(0).getExpenditures().get(i).getCorrection());
            }
        }
        return null;
    }

    @Click(SAVE_EXPENDITURE_BUTTON)
    HypokalkulackaPage clickSaveExpenditureButton();
}