package cz.moneta.test.dsl.brokerportal.contract;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.BrokerPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

public interface ValuationPage extends WebFlow<ValuationPage>, StoreAccessor {

    //Valuation - property search
    String INPUT_CITY = "//input[@name = 'city']";
    String INPUT_AREA = "//div[@id = 'search-area']//input";

    String INPUT_FLATUNIT = "//input[@id = 'flatUnit']";
    String INPUT_HOUSE = "//input[@id = 'house']";
    String BUTTON_CONTINUE = "//button/span[text() = 'Pokračovat']";
    String CHECKBOX_HOUSECHECK = "//li[@id = 'estate_value-house']";
    String BUTTON_CONFIRM = "//button/span[text() = 'Potvrdit nemovitost']";

    //Valuation - Adress
    String INPUT_SEARCHADDRESS = "//div[@id = 'searchAddress']//input";

    //Valuation - Flat
    String INPUT_FLATTYPE = "//div[@id = 'type']//input";
    String INPUT_FLATPLACING = "//div[@id = 'placing']//input";
    String INPUT_FLATDISPOSITION = "//div[@id = 'disposition']//input";
    String INPUT_FLATSTATUS = "//div[@id = 'technicalStatus']//input";

    String INPUT_FLATFLOORAREA = "//input[@id = 'floorArea']";
    String INPUT_FLATFLOORAREAOTHER = "//input[@id = 'floorAreaOther']";
    String INPUT_FLATPARKINGOUTSIDE = "//input[@id = 'parkingPlaceOutside']";
    String INPUT_FLATPARKINGINSIDE = "//input[@id = 'parkingPlaceInside']";
    String INPUT_FLATGARAGE = "//input[@id = 'garage']";

    // Price
    String INPUT_PURCHASEPRICE = "//input[@id = 'purchasePrice']";

    //Valuation - Flat house
    String INPUT_FLATHOUSETYPE = "//div[@id = 'buildingConstructionMaterial']//input";
    String INPUT_FLATHOUSEYEAR = "//input[@id = 'constructionYearBuilding']";
    String INPUT_FLATHOUSEFLOORS = "//input[@id = 'upstairsFloors']";
    String INPUT_FLATHOUSESTATUS = "//div[@id = 'buildingTechnicalStatus']//input";

    //Valuation - House
    String INPUT_HOUSEMATERIAL = "//div[@id = 'constructionMaterial']//input";
    String INPUT_HOUSEYEAR = "//input[@id = 'constructionYearBuilding']";
    String INPUT_HOUSESTATUS = "//div[@id = 'technicalStatus']//input";
    String INPUT_HOUSELANDAREA = "//input[@id = 'landArea']";
    String INPUT_GARAGEBASEMENT = "//input[@id = 'garageBasementCount']";
    String INPUT_GARAGEGROUND = "//input[@id = 'garageGroundFloorCount']";
    String INPUT_GARAGESEPARATE = "//input[@id = 'garageSeparateCount']";
    String INPUT_NEEDCALCULATEFALSE = "//li[@id = 'needCalculate_value-false']";
    String INPUT_FLOORAREA = "//input[@name = 'floorArea']";

    //Valuation - User
    String INPUT_ROLE = "//div[@id = 'role']//input";
    String INPUT_FIRSTNAME = "//input[@id = 'firstName']";
    String INPUT_LASTNAME = "//input[@id = 'lastName']";
    String INPUT_PHONE = "//input[@name = 'phone']";

    /**
     * Compare given victory screen id with shown on BP.
     *
     * @param victoryScreenId - String Xpath of victoryScreenId.
     * @return
     */
    @CustomAction
    default ValuationPage checkVictoryId(String victoryScreenId) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        if (endpoint.isAlertPresent(10)) {
            endpoint.acceptAlert(5);
        }
        String DIV_VSCREEN = String.format("//h4[@id = '%s']", victoryScreenId);
        endpoint.waitForAnyElementToLoad(300, Lookup.XPATH, DIV_VSCREEN);
        return null;
    }

    @CustomAction
    default ValuationPage reportResults(String valuationType, String contractId) {
        String outputLog = String.format("Broker portal valuation completed with %s result. Contract ID is: %s", valuationType, contractId);
        log(outputLog);
        return null;
    }

    @TypeInto(INPUT_HOUSEMATERIAL)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseMaterial(String material);

    @TypeInto(INPUT_HOUSEYEAR)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseYear(int year);

    @TypeInto(INPUT_HOUSESTATUS)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseStatus(String status);

    @TypeInto(INPUT_HOUSELANDAREA)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseLandArea(int status);

    @TypeInto(INPUT_GARAGEBASEMENT)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseGarageBasement(int garageBasement);

    @TypeInto(INPUT_GARAGEGROUND)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseGarageGround(String garageGround);

    @TypeInto(INPUT_GARAGESEPARATE)
    @KeyPress(Key.ENTER)
    ValuationPage selectHouseGarageSeparated(String garageSeparated);

    @Click(INPUT_NEEDCALCULATEFALSE)
    ValuationPage clickNeedCalculateFalse();

    @TypeInto(INPUT_FLOORAREA)
    ValuationPage insertHouseFloorArea(int floorArea);

    /**
     * Insert area and pick it from list.
     * Reason for this complicated xpath is, because there are always two corresponding elements - one of them is hidden.
     *
     * @param area - String of Area (On tests enviroments work only Prague and Brno areas.)
     * @return
     */
    @CustomAction
    default ValuationPage insertArea(String area) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> INPUT_AREA, area, false);
        String DIV_MENUITEM = String.format("//*/div[text() = '%s' and not(ancestor::div[contains(@style,'display: inline-block;')])]", area);
        endpoint.waitForAnyElementToLoad(5, DIV_MENUITEM);
        endpoint.click(() -> DIV_MENUITEM);
        return null;
    }

    /**
     * Waiting till element is visible, then scrolling to it and then clicking on it.
     *
     * @return
     */
    @CustomAction
    default ValuationPage clickConfirm() {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.scrollIntoView(BUTTON_CONFIRM);
        endpoint.waitForAnyElementToLoad(5, String.valueOf(Until.VISIBLE), BUTTON_CONFIRM);
        endpoint.click(() -> BUTTON_CONFIRM);
        return null;
    }

    /**
     * Waiting till list of addresses is loaded and then picking one of them.
     * Reason for this complicated xpath is, because there are always two corresponding elements - one of them is hidden.
     *
     * @param address
     * @return
     */
    @CustomAction
    default ValuationPage insertAddress(String address) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        String DIV_MENUITEM = String.format("//*/div[text()[contains(., '%s')] and not(ancestor::div[contains(@style,'display: inline-block;')])]", address);
        endpoint.waitForAnyElementToLoad(5, INPUT_SEARCHADDRESS);
        endpoint.type(() -> INPUT_SEARCHADDRESS, address, false);
        endpoint.waitForAnyElementToLoad(5, DIV_MENUITEM);
        endpoint.click(() -> DIV_MENUITEM);
        endpoint.sendKeysAsChord(Key.ENTER);
        return null;
    }

    @TypeInto(INPUT_FLATTYPE)
    @KeyPress(Key.ENTER)
    ValuationPage selectFlatType(String flatType);

    @TypeInto(INPUT_FLATPLACING)
    @KeyPress(Key.ENTER)
    ValuationPage selectFlatPlacing(String flatPlacing);

    @TypeInto(INPUT_FLATDISPOSITION)
    @KeyPress(Key.ENTER)
    ValuationPage selectFlatDisposition(String flatDisposition);

    @TypeInto(INPUT_FLATSTATUS)
    @KeyPress(Key.ENTER)
    ValuationPage selectFlatState(String flatState);

    @TypeInto(INPUT_FLATFLOORAREA)
    ValuationPage insertFlatArea(int flatArea);

    @TypeInto(INPUT_FLATFLOORAREAOTHER)
    ValuationPage insertFlatAreaOther(int flatAreaOther);

    @TypeInto(INPUT_FLATPARKINGOUTSIDE)
    ValuationPage insertFlatParkingOutside(int parkingOutside);

    @TypeInto(INPUT_FLATPARKINGINSIDE)
    ValuationPage insertFlatParkingInside(int parkingOutside);

    @TypeInto(INPUT_FLATGARAGE)
    ValuationPage insertFlatGarage(int parkingGarage);

    @TypeInto(INPUT_PURCHASEPRICE)
    ValuationPage insertPrice(int flatPrice);

    @TypeInto(INPUT_FLATHOUSETYPE)
    @KeyPress(Key.ENTER)
    ValuationPage insertFlatHouseType(String flatHouseType);

    @TypeInto(INPUT_FLATHOUSEFLOORS)
    ValuationPage insertFlatHouseFloors(int flatHouseFloors);

    @TypeInto(INPUT_FLATHOUSESTATUS)
    @KeyPress(Key.ENTER)
    ValuationPage insertFlatHouseStatus(String flatHouseStatus);

    @TypeInto(INPUT_ROLE)
    @KeyPress(Key.ENTER)
    ValuationPage insertUserRole(String userRole);

    @TypeInto(INPUT_FIRSTNAME)
    ValuationPage insertUserFirstName(String userFirstName);

    @TypeInto(INPUT_LASTNAME)
    ValuationPage insertUserLastName(String userLastName);

    @TypeInto(INPUT_PHONE)
    ValuationPage insertUserPhone(String userPhone);

    @TypeInto(INPUT_FLATHOUSEYEAR)
    ValuationPage insertFlatHouseYear(String flatHouseType);

    @TypeInto(INPUT_CITY)
    ValuationPage insertCity(String city);

    @TypeInto(INPUT_HOUSE)
    ValuationPage insertHouse(String unit);

    @TypeInto(INPUT_FLATUNIT)
    ValuationPage insertFlatUnit(String unit);

    @Click(BUTTON_CONTINUE)
    ValuationPage clickContinue();

    @Click(CHECKBOX_HOUSECHECK)
    ValuationPage clickHouse();
}