package Utility;

import Email.EmailPage;
import Inbox.InboxPage;
import Login.LoginPage;
import Overview.OverviewPage;
import Seznam.SeznamPage;
import org.openqa.selenium.WebDriver;
import org.testng.asserts.SoftAssert;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class KeywordUtils {

    private ExcelUtils excelUtils;
    private List<Object> pageInstances;

    private List<String> testSteps;
    private List<String> pagesNames;
    private List<String> usedParameters;

    private Method testMethod;

    /**
     * constructor
     * @param excelUtils reference to excel which we are using
     */
    public KeywordUtils(ExcelUtils excelUtils)
    {
        this.excelUtils = excelUtils;
    }

    /**
     * perform whole test based on keywords from excel
     * @throws Exception
     */
    public void performTest() throws Exception {

        for(int i=0; i<testSteps.size(); i++)
        {
            for(int j=0; j<pageInstances.size();j++) //get test methods
            {
                if (pageInstances.get(j).getClass().getSimpleName().equals(pagesNames.get(i)))
                {
                    if (usedParameters.get(i)==""||usedParameters.get(i)==null) {
                        testMethod = pageInstances.get(j).getClass().getMethod(testSteps.get(i));
                        testMethod.invoke(pageInstances.get(j));
                    }
                    else
                    {
                        if(usedParameters.get(i).equals("true")||usedParameters.get(i).equals("false"))
                        {
                            testMethod = pageInstances.get(j).getClass().getMethod(testSteps.get(i),Boolean.class);
                            testMethod.invoke(pageInstances.get(j), Boolean.valueOf(usedParameters.get(i)));
                        }
                        else
                        {
                            testMethod = pageInstances.get(j).getClass().getMethod(testSteps.get(i),String.class);
                            testMethod.invoke(pageInstances.get(j),usedParameters.get(i));
                        }

                    }
                }
            }
        }
    }

    /**
     * create instaces of all pages we are using
     * @param driver which we are using
     * @param sa soft assert which we are using
     * @throws Exception
     */
    public void initPages(WebDriver driver, SoftAssert sa) throws Exception {

        SeznamPage seznamPage = new SeznamPage(driver, sa);
        OverviewPage overviewPage = new OverviewPage(driver, sa);
        LoginPage loginPage = new LoginPage(driver, sa);
        InboxPage inboxPage = new InboxPage(driver, sa);
        EmailPage emailPage = new EmailPage(driver, sa);

        pageInstances = new ArrayList<Object>();
        pageInstances.add(seznamPage);
        pageInstances.add(overviewPage);
        pageInstances.add(loginPage);
        pageInstances.add(inboxPage);
        pageInstances.add(emailPage);

    }

    /**
     * set excel to current TC
     * @param TCName name of TC which we want to get from current excel file
     * @throws Exception
     */
    public void initTC(String TCName) throws Exception {
        excelUtils.setExcelWSheet(TCName);
        testSteps = excelUtils.getTestSteps();
        pagesNames = excelUtils.getNamesOfUsedPages();
        usedParameters = excelUtils.getParameters();
    }

}
