package cz.moneta.test.dsl.mobile.smartbanking.landing;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.smartbanka.MobilePlatform;
import cz.moneta.test.harness.endpoints.smartbanka.SbEndpoint;
import cz.moneta.test.harness.support.mobile.MobileLookup;
import cz.moneta.test.harness.support.mobile.android.AndroidClick;
import cz.moneta.test.harness.support.mobile.android.AndroidTypeInto;
import cz.moneta.test.harness.support.mobile.android.AndroidWait;
import cz.moneta.test.harness.support.mobile.ios.IosClick;
import cz.moneta.test.harness.support.mobile.ios.IosTypeInto;
import cz.moneta.test.harness.support.mobile.ios.IosWait;
import cz.moneta.test.harness.support.mobile.share.MobileCustomAction;
import cz.moneta.test.harness.support.mobile.share.MobileFlow;

import java.util.Arrays;

import static cz.moneta.test.dsl.mobile.smartbanking.landing.PinView.IOS_PIN_INPUT;
import static cz.moneta.test.dsl.mobile.smartbanking.landing.PinView.PIN_INPUT;

@AndroidWait(value = PIN_INPUT)
@IosWait(IOS_PIN_INPUT)
public interface PinView<T extends MobileFlow<T>> extends StoreAccessor {
    String PIN_INPUT = "edit_pin_value";
    String PIN_SUBMIT = "btn_pin_submit";

    String IOS_PIN_SUBMIT = "PIN_BUTTON";
    String IOS_PIN_INPUT = "PIN_TEXT";

    @AndroidTypeInto(value = PIN_INPUT)
    @IosTypeInto(IOS_PIN_INPUT)
    @AndroidClick(value = PIN_SUBMIT)
    @IosClick(IOS_PIN_SUBMIT)
    T typePinAndContinue(String pin);

    @MobileCustomAction
    default T enterPinUsingKeyboard(StoreAccessor store, SmartBankingLoginKeyboard... pin) {
        SbEndpoint sbEndpoint = getEndpoint(SbEndpoint.class);
        if (MobilePlatform.isAndroid(store)) {
            Arrays.stream(pin).forEach(key -> sbEndpoint.click(key::getAndroidValue, MobileLookup.ID));
            sbEndpoint.click(() -> PIN_SUBMIT, MobileLookup.DEFAULT);
        } else {
            Arrays.stream(pin).forEach(key -> sbEndpoint.click(key::getValue, MobileLookup.ID));
            sbEndpoint.click(() -> IOS_PIN_SUBMIT, MobileLookup.DEFAULT);
        }
        return null;
    }
}