package com.intland.codebeamer.functionaltest.page.trackers;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
import com.intland.codebeamer.functionaltest.page.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import static com.codeborne.selenide.Selenide.*;

public class TrackersCustomizationPage extends BasePage {

    public enum TabTypes {
        GENERAL("#tracker-customize-general-tab", GeneralTab.class),
        PERMISSIONS("#tracker-customize-permissions-tab", PermissionsTab.class),
        STATE_TRANSITIONS("#tracker-customize-transitions-tab", StateTransitionsTab.class),
        FIELDS("#tracker-customize-field-properties-tab", FieldsTab.class),
        ESCALATION("#tracker-customize-escalation-tab", EscalationTab.class),
        NOTIFICATIONS("#tracker-customize-notification-tab", NotificationsTab.class),
        SERVICE_DESK("#tracker-customize-serviceDesk-tab", ServiceDeskTab.class);

        private final String selector;
        private final Class pageObjectClass;

        TabTypes(String s, Class pageObjectClass) {
            selector = s;
            this.pageObjectClass = pageObjectClass;
        }

        public boolean equalsName(String otherSelector) {
            return (otherSelector == null) ? false : selector.equals(otherSelector);
        }

        public String toString() {
            return this.selector;
        }
    }


    public Object clickOnTab(TabTypes tabType) {
        $(tabType.selector).click();
        return page(tabType.pageObjectClass);
    }

    public void checkIfSaveIs(Condition condition){
        $(By.xpath(".//input[@name='SAVE']")).shouldBe(condition);
    }
    /*
        public <T> T clickOnTab(Class<T> pageObject) {
            for(TabTypes t : TabTypes.values()){
                System.out.println("t:" + t.pageObjectClass);
                if (t.pageObjectClass.equals(pageObject)){
                    $(withText(t.text)).click();
                    return page(pageObject);
                }
            }
            return null;
        }
    */

    @Override
    public boolean isPageLoaded() throws UnsupportedOperationException {
        //.withTimeout(3, TimeUnit.SECONDS).pollingEvery(200, TimeUnit.MILLISECONDS)
        return Wait().until(ExpectedConditions.titleIs("Customization"));
    }

    abstract static class Tab extends BasePage{

        public void clickOnCancel(){
            $(By.xpath("(.//div[@id='tracker-customize']/div[@class='ditch-tab-pane-wrap']/div[@class='ditch-tab-pane'])" +
                    "[count(//div[@id='tracker-customize']/div[@class='ditch-tab-wrap']//div[contains(@class, 'ditch-focused')]//preceding-sibling::div)+1]" +
                    "//input[@name='_cancel']")).click();
        }

        public void clickOnSave(){
            $(By.xpath("(.//div[@id='tracker-customize']/div[@class='ditch-tab-pane-wrap']/div[@class='ditch-tab-pane'])" +
                    "[count(//div[@id='tracker-customize']/div[@class='ditch-tab-wrap']//div[contains(@class, 'ditch-focused')]//preceding-sibling::div)+1]" +
                    "//input[@name='SAVE']")).click();
        }
    }

    public static class GeneralTab extends Tab {

        public GeneralTab(){

        }

    }

    public static class PermissionsTab extends Tab {


    }

    public static class StateTransitionsTab extends Tab {


    }



    public static class FieldsTab extends Tab {

        public enum LayoutTypes {
            RESULT_OPTIONS("Options", FieldsTab.ResultOptionsModal.class),
            CATEGORY_OPTIONS("Options", CategoryOptionsModal.class);

            private final String text;
            private final Class pageObjectClass;

            LayoutTypes(String s, Class pageObjectClass) {
                text = s;
                this.pageObjectClass = pageObjectClass;
            }

            public boolean equalsName(String otherSelector) {
                return (otherSelector == null) ? false : text.equals(otherSelector);
            }

            public String toString() {
                return this.text;
            }
        }

        public Object clickOnLayoutTypeByRowNumber(LayoutTypes layoutType, int rowNr) {
            $(By.xpath(".//table[@id='trackerFields']/tbody[" + rowNr + "]//td[8]//a[text()='" + layoutType.text + "']")).click();
            //return new ResultOptionsModal();
            return page(layoutType.pageObjectClass);

        }

        public class ResultOptionsModal extends BasePage {
            public String getMeaningByName(String name) {
                return $(By.xpath(".//td[./span[contains(text(), '" + name + "')]]/following-sibling::td/span")).getText().trim();
            }
        }


    }

    public static class EscalationTab extends Tab {

        private SelenideElement tabPaneSelector = $("#tracker-customize-escalation");
        public SelenideElement getWarning(){
            return tabPaneSelector.$(".warning");
        }


    }

    public static class NotificationsTab extends Tab {


    }



    public class CategoryOptionsModal extends Tab {

    }


    public static class ServiceDeskTab extends Tab {
        private SelenideElement tabPaneSelector = $("#tracker-customize-serviceDesk");
        public SelenideElement getWarning(){
            return tabPaneSelector.$(".warning");
        }
    }
}
