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

import cz.moneta.test.dsl.mobile.smartbanking.accounts.AccountSelectionView;
import cz.moneta.test.dsl.mobile.smartbanking.payment.Payment;
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.AndroidWait;
import cz.moneta.test.harness.support.mobile.ios.IosWait;
import cz.moneta.test.harness.support.mobile.share.MobileCustomAction;
import cz.moneta.test.harness.support.web.Until;

import static cz.moneta.test.dsl.mobile.smartbanking.CurrentAccounts.ANDROID_ACCOUNTS_LIST;
import static cz.moneta.test.dsl.mobile.smartbanking.CurrentAccounts.IOS_CURRENT_ACCOUNT;

@IosWait(IOS_CURRENT_ACCOUNT)
@AndroidWait(ANDROID_ACCOUNTS_LIST)
public interface CurrentAccounts extends SbViewFlow<CurrentAccounts>, StoreAccessor {
    String ANDROID_ACCOUNTS_LIST = "list_item_account_account";
    String ACCOUNT = "//android.widget.TextView[@text= '%s']";

    String IOS_CURRENT_ACCOUNT = "Běžný účet";

    @MobileCustomAction
    default AccountSelectionView clickOnAccount(StoreAccessor storeAccessor, Payment payment) {
        SbEndpoint sbEndpoint = getEndpoint(SbEndpoint.class);
        String account = String.format(ACCOUNT, payment.getFromAccountWithBankCode());
        if (MobilePlatform.isAndroid(storeAccessor)) {
            sbEndpoint.waitForElementsToLoad(20, MobileLookup.XPATH, Until.VISIBLE, account);
            sbEndpoint.click(() -> account, MobileLookup.XPATH);
        } else {
            sbEndpoint.click(payment::getFromAccountWithBankCode, MobileLookup.IOS_NAME);
        }
        return null;
    }
}
