package cz.moneta.test.dsl.ufo.banka.pages.fon.newCif;

import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;

import static cz.moneta.test.dsl.ufo.banka.pages.fon.newCif.NewCifIdentificationDocuments.*;
import static cz.moneta.test.dsl.ufo.banka.pages.fon.newCif.NewCifPersonalData.*;
import static cz.moneta.test.dsl.ufo.banka.pages.fon.newCif.NewCifResidency.*;

public interface NewCifRegistryChecks extends CIF111_S001_NewCifDataNavigation {

    @CustomAction
    default NewCifRegistryChecks checkHiddenInputElementValue(String elementName, String expectedValue) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String text = endpoint.getAttribute(elementName, Lookup.NAME, "value");
        if (!text.contains(expectedValue)) {
            throw new AssertionError(String.format("Element \"%1s\" doesn't contains text \"%2s\"", elementName, expectedValue));
        }
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkHiddenInputElementValueIgnoreCase(String elementName, String expectedValue) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String text = endpoint.getAttribute(elementName, Lookup.NAME, "value");
        if (!text.equalsIgnoreCase(expectedValue)) {
            throw new AssertionError(String.format("Element \"%1s\" doesn't contains text \"%2s\"", elementName, expectedValue));
        }
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkSelectedComboElementValue(String elementName, String expectedValue) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String text = endpoint.getSelectedFirstItemText(elementName);
        if (!text.contains(expectedValue)) {
            throw new AssertionError(String.format("Selected value in \"%1s\" doesn't contains text \"%2s\"", elementName, expectedValue));
        }
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkFirstName(String name) {
        checkHiddenInputElementValueIgnoreCase(FIRST_NAME_INPUT, name);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkLastName(String name) {
        checkHiddenInputElementValueIgnoreCase(LAST_NAME_INPUT, name);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkSexMan() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String checked = endpoint.getAttribute(SEX_MAN_RADIO, "checked");
        if(!checked.equalsIgnoreCase("true")) {
            throw new AssertionError(String.format("Radiobutton \"%s\" isn't checked", SEX_MAN_RADIO));
        }
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkBirthNumber(String birthNumber) {
        checkHiddenInputElementValue(BIRTH_NUMBER_INPUT, birthNumber);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkDateOfBirth(String dateOfBirth) {
        checkHiddenInputElementValue(DATE_OF_BIRTH_INPUT, dateOfBirth);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkStreet(String name) {
        checkHiddenInputElementValue(RESIDENCY_STREET_INPUT, name);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkStreetDescriptiveNumber(String number) {
        checkHiddenInputElementValue(RESIDENCY_STREET_DESCRIPTIVE_NUMBER_INPUT, number);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkStreetOrientationNumber(String number) {
        checkHiddenInputElementValue(RESIDENCY_STREET_ORIENTATION_NUMBER_INPUT, number);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkCity(String city) {
        checkHiddenInputElementValue(RESIDENCY_CITY_INPUT, city);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkCityPart(String cityPart) {
        checkHiddenInputElementValue(RESIDENCY_CITY_PART_INPUT, cityPart);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkZip(String zip) {
        checkHiddenInputElementValue(RESIDENCY_ZIP_INPUT, zip);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkPostOffice(String postOffice) {
        checkHiddenInputElementValue(RESIDENCY_POST_OFFICE_INPUT, postOffice);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkPlaceOfBirth(String placeOfBirth) {
        checkHiddenInputElementValue(PLACE_OF_BIRTH_INPUT, placeOfBirth);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkBornCountry(String country) {
        checkSelectedComboElementValue(BORN_COUNTRY_SELECT, country);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkNationality(String country) {
        checkSelectedComboElementValue(NATIONALITY_SELECT, country);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkResidencyCountry(String country) {
        checkSelectedComboElementValue(PERMANENT_RESIDENCY_COUNTRY_SELECT, country);
        return null;
    }

    @CustomAction
    default  NewCifRegistryChecks checkFirstIdType(String expectedType) {
        checkSelectedComboElementValue(FIRST_ID_SELECT, expectedType);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkFirstIdNumber(String number) {
        checkHiddenInputElementValue(FIRST_ID_NUMBER_INPUT, number);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkFirstIdValidity(String date) {
        checkHiddenInputElementValue(FIRST_ID_VALID_TO_INPUT, date);
        return null;
    }

    @CustomAction
    default NewCifRegistryChecks checkControlNumberAndDisabled(String controlNumber) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementEnabled(CONTROL_NUMBER_INPUT, Lookup.NAME)){
            throw new AssertionError(String.format("Element \"%1s\" is enabled", CONTROL_NUMBER_INPUT));
        }
        checkHiddenInputElementValue(CONTROL_NUMBER_INPUT, controlNumber);
        return null;
    }
}
