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

import cz.moneta.test.dsl.hypos.components.Menu;
import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.dsl.hypos.pages.contract.NewContractPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.TypeInto;
import cz.moneta.test.harness.support.web.Wait;

import static cz.moneta.test.dsl.hypos.pages.hypokom.HypokomSearchPage.CONTACTS_TABLE_XPATH;

@Wait(value = CONTACTS_TABLE_XPATH, waitSecondsForElement = 120)
public interface HypokomSearchPage extends HyposPageFlow<HypokomSearchPage>, Menu, StoreAccessor {
    String CONTACTS_TABLE_XPATH = "//div[@id='contacts']//table[@class='content']";
    String FILTER_CONTACT_ID_INPUT_XPATH = "//input[@id='filter-id']";
    String FILTER_HYPOS_ID_INPUT_XPATH = "//input[@id='filter-code']";
    String FILTER_SINCE_INPUT_XPATH = "//input[@id='filter-since']";
    String FILTER_STATE_ID_CHECKBOX_XPATH = "//input[@id='%s']";
    String ELABORATE_CONTACT_BUTTON_XPATH = "//a[contains(@class, 'icon-elaborate')]";
    String FILTER_BUTTON_XPATH_XPATH = "//form[@id='contacts-filter-form']//input[@value='Zobrazit']";
    String CREATE_CONTRACT_BUTTON_XPATH = "//a[contains(@class, 'icon-createcontract')]";

    @TypeInto(FILTER_CONTACT_ID_INPUT_XPATH)
    HypokomSearchPage fillContactId(String id);

    @TypeInto(FILTER_HYPOS_ID_INPUT_XPATH)
    HypokomSearchPage fillHyposId(String id);

    @TypeInto(FILTER_SINCE_INPUT_XPATH)
    HypokomSearchPage fillSince(String since);

    @Click(value = FILTER_STATE_ID_CHECKBOX_XPATH, isStringDynamicXpath = true)
    HypokomSearchPage clickState(String stateId);

    @Click(FILTER_BUTTON_XPATH_XPATH)
    HypokomSearchPage clickFilterButton();

    @Wait(CONTACTS_TABLE_XPATH)
    @Click(ELABORATE_CONTACT_BUTTON_XPATH)
    HypokomContactDetailPage clickElaborateContactButton();

    @Wait(CONTACTS_TABLE_XPATH)
    @CustomAction
    default NewContractPage clickCreateContractButtonAndSwitch() {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        endpoint.click(() -> CREATE_CONTRACT_BUTTON_XPATH);
        endpoint.sleepSeconds(15);
        endpoint.closeCurrentWindow();
        endpoint.switchToWindowByTitle("Založení nového hypotečního obchodu");
        endpoint.switchToFrame(MAIN_FRAME_XPATH);
        endpoint.maximizeBrowserWindow();
        return null;
    }
}
