package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

public class PanelMenuTest extends SeleniumTestBase {

    @Test
    public 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();
    }

    @Test
    public 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();
    }

    @Test
    public 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();
    }

    @Test
    public 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();
    }

}
