package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.RichSeleniumTest;
import org.richfaces.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class PanelMenuTest extends SeleniumTestBase implements RichSeleniumTest {

    public PanelMenuTest() {
        super("http", "localhost", serverPort);
    }

    /**
     * This method are invoking before selenium tests started
     */
    @BeforeMethod
    @Parameters({"browser", "filterPrefix"})
    public void startSelenium(String browser, String filterPrefix) {
    	super.startSelenium(browser, filterPrefix);
    }
    
    
    @BeforeMethod
    @Parameters({"loadStyleStrategy", "loadScriptStrategy"})
    protected void loadConfiguration(String loadStyleStrategy, String loadScriptStrategy) throws Exception {
	super.loadConfiguration(loadStyleStrategy, loadScriptStrategy);
    }

    /**
     * This method are invoking after selenium tests completed
     */
    @AfterMethod(alwaysRun=true)
    public void stopSelenium() {
        super.stopSelenium();
    }

    @Test
    public void testPanelMenuComponent() throws Exception {

        _testPanelMenuComponent(Template.SIMPLE);
        _testPanelMenuComponent(Template.DATA_TABLE);
        _testPanelMenuComponent(Template.MODAL_PANEL);

        _testPanelMenuComponentSingleMode(Template.SIMPLE);
        _testPanelMenuComponentSingleMode(Template.DATA_TABLE);
        _testPanelMenuComponentSingleMode(Template.MODAL_PANEL);

        _testPanelMenuItemAction(Template.SIMPLE);
        _testPanelMenuItemAction(Template.DATA_TABLE);
        _testPanelMenuItemAction(Template.MODAL_PANEL);

        _testPanelMenuClientAPI(Template.SIMPLE);
        _testPanelMenuClientAPI(Template.DATA_TABLE);
        _testPanelMenuClientAPI(Template.MODAL_PANEL);
    }

    private void _testPanelMenuClientAPI(Template template) {
        renderPage(template);

        String form1 = getParentId() + "_form1:";
        String form2 = getParentId() + "_form2:";

        String group = form1 + "pGroup3";
        String sigleGroup = form2 + "pGroup3_single";

        String ajaxItem = "tablehide" + form1 + "pItem1";
        String serverItem = "tablehide" + form1 + "pItem2";
        String clientItem = "tablehide" + form1 + "pItem3";

        writeStatus("Test expand JS API method for ajax multiple panel menu.");
        selenium.runScript("$('" + group + "').component.expand()");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertTrue(isVisibleById(clientItem));

        writeStatus("Test collapse JS API method for ajax multiple panel menu.");
        selenium.runScript("$('" + group + "').component.collapse()");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        ajaxItem = "tablehide" + form2 + "pItem1_single";
        serverItem = "tablehide" + form2 + "pItem2_single";
        clientItem = "tablehide" + form2 + "pItem3_single";

        writeStatus("Test expand JS API method for ajax single panel menu.");
        selenium.runScript("$('" + sigleGroup + "').component.expand()");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertTrue(isVisibleById(clientItem));

        writeStatus("Test collapse JS API method for ajax single panel menu.");
        selenium.runScript("$('" + sigleGroup + "').component.collapse()");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        tearDown();
    }

    private void _testPanelMenuComponent(Template template) {
        renderPage(template);
        String parentId = getParentId() + "_form1:";

        String value1Id = getParentId() + "_value";
        String value2Id = getParentId() + "_value2";

        String ajaxGroup = "tablehide" + parentId + "pGroup1";
        String serverGroup = "tablehide" + parentId + "pGroup2";
        String clientGroup = "tablehide" + parentId + "pGroup3";
        String disabledGroup = "tablehide" + parentId + "pGroup4";

        String ajaxItem = "tablehide" + parentId + "pItem1";
        String serverItem = "tablehide" + parentId + "pItem2";
        String clientItem = "tablehide" + parentId + "pItem3";
        String disabledItem = "tablehide" + parentId + "pItem4";

        writeStatus("Click on ajax group");
        clickById(ajaxGroup);
        waitForAjaxCompletion();
        AssertTextEquals(value1Id, "pGroup1");
        AssertTextEquals(value2Id, "1");
        Assert.assertTrue(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on server group");
        clickCommandAndWait(serverGroup);
        AssertTextEquals(value1Id, "pGroup2");
        AssertTextEquals(value2Id, "2");
        Assert.assertTrue(isVisibleById(ajaxItem));
        Assert.assertTrue(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on ajax group");
        clickById(ajaxGroup);
        waitForAjaxCompletion();
        AssertTextEquals(value1Id, "pGroup1");
        AssertTextEquals(value2Id, "3");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertTrue(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on server group");
        clickCommandAndWait(serverGroup);

        AssertTextEquals(value1Id, "pGroup2");
        AssertTextEquals(value2Id, "4");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on client group");
        clickById(clientGroup);
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertTrue(isVisibleById(clientItem));

        writeStatus("Click on client group");
        clickById(clientGroup);
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on disabled client group");
        clickById(disabledGroup);
        Assert.assertFalse(isVisibleById(disabledItem));

        tearDown();
    }

    private void _testPanelMenuComponentSingleMode(Template template) {
        renderPage(template);
        String parentId = getParentId() + "_form2:";

        String value1Id = getParentId() + "_value";
        String value2Id = getParentId() + "_value2";

        String ajaxGroup = "tablehide" + parentId + "pGroup1_single";
        String serverGroup = "tablehide" + parentId + "pGroup2_single";
        String clientGroup = "tablehide" + parentId + "pGroup3_single";
        String disabledGroup = "tablehide" + parentId + "pGroup4_single";

        String ajaxItem = "tablehide" + parentId + "pItem1_single";
        String serverItem = "tablehide" + parentId + "pItem2_single";
        String clientItem = "tablehide" + parentId + "pItem3_single";
        String disabledItem = "tablehide" + parentId + "pItem4_single";

        writeStatus("Click on ajax group");
        clickById(ajaxGroup);
        waitForAjaxCompletion();
        AssertTextEquals(value1Id, "pGroup1_single");
        AssertTextEquals(value2Id, "1");
        Assert.assertTrue(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on server group");
        clickCommandAndWait(serverGroup);
        AssertTextEquals(value1Id, "pGroup2_single");
        AssertTextEquals(value2Id, "2");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertTrue(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on ajax group");
        clickById(ajaxGroup);
        waitForAjaxCompletion();
        AssertTextEquals(value1Id, "pGroup1_single");
        AssertTextEquals(value2Id, "3");
        Assert.assertTrue(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on server group");
        clickCommandAndWait(serverGroup);
        AssertTextEquals(value1Id, "pGroup2_single");
        AssertTextEquals(value2Id, "4");
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertTrue(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on client group");
        clickById(clientGroup);
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertTrue(isVisibleById(clientItem));

        writeStatus("Click on client group");
        clickById(clientGroup);
        Assert.assertFalse(isVisibleById(ajaxItem));
        Assert.assertFalse(isVisibleById(serverItem));
        Assert.assertFalse(isVisibleById(clientItem));

        writeStatus("Click on disabled client group");
        clickById(disabledGroup);
        Assert.assertFalse(isVisibleById(disabledItem));

        tearDown();
    }

    private void _testPanelMenuItemAction(Template template) {
        renderPage(template);
        String parentId = getParentId() + "_form";

        String value1Id = getParentId() + "_value";
        String value2Id = getParentId() + "_value2";

        String group = "tablehide" + parentId + "1:pGroup1";
        String groupSingle = "tablehide" + parentId + "2:pGroup1_single";

        String item = "tablehide" + parentId + "1:pItem1";
        String itemSingle = "tablehide" + parentId + "2:pItem1_single";

        writeStatus("Click on ajax group");
        clickById(group);
        waitForAjaxCompletion();
        AssertTextEquals(value1Id, "pGroup1");

        writeStatus("Click on ajax group");
        clickById(groupSingle);
        waitForAjaxCompletion();
        AssertTextEquals(value1Id, "pGroup1_single");

        writeStatus("Click on server group");
        clickCommandAndWait(item);
        AssertTextEquals(value1Id, "pItem1");
        AssertTextEquals(value2Id, "1");

        writeStatus("Click on server group");
        clickCommandAndWait(itemSingle);
        AssertTextEquals(value1Id, "pItem1_single");
        AssertTextEquals(value2Id, "2");

        tearDown();
    }

    public String getTestUrl() {
        return "pages/panelMenu/panelMenuTest.xhtml";
    }

    private void tearDown() {
        writeStatus("Clean bean values");
        clickById(getParentId() + "_form2:clean");
        waitForAjaxCompletion();
    }

}
