/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.richfaces.testng;

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

public class OrderingListTest extends SeleniumTestBase {
    private String firstButton;

    private String firstButtonDisabled;

    private String upButton;

    private String upButtonDisabled;

    private String downButton;

    private String downButtonDisabled;

    private String lastButton;

    private String lastButtonDisabled;

    private String firstRow;

    private String actionResultText;

    private String ajax;

    private String server;

    private String thirdRow;

    private String selectionText;

    private String activeItemText;

    @Test
    public void testButtons(Template template) {
        renderPage(template);
        initFields();

        writeStatus("Check if all buttons are disabled first time");
        checkButtons(true, true, true, true);

        writeStatus("Click on first row");
        fireMouseEvent(firstRow, "click", 0, 0, false);
        checkButtons(true, true, false, false);

        writeStatus("Click on down button");
        Assert.assertEquals(selenium.getElementIndex("id=" + firstRow), 0);
        clickById(downButton);
        checkButtons(false, false, false, false);
        Assert.assertEquals(selenium.getElementIndex("id=" + firstRow), 1);

        writeStatus("Click on last button");
        clickById(lastButton);
        checkButtons(false, false, true, true);
        Assert.assertEquals(selenium.getElementIndex("id=" + firstRow), 3);

        writeStatus("Click on first button");
        clickById(firstButton);
        checkButtons(true, true, false, false);
        Assert.assertEquals(selenium.getElementIndex("id=" + firstRow), 0);
    }

    @Test
    public void testActions(Template template) {
        renderPage(template);
        initFields();
        
        writeStatus("Select two rows");
        
        fireMouseEvent(firstRow, "click", 0, 0, false);
        fireMouseEvent(thirdRow, "click", 0, 0, true);
        checkButtons(true, true, false, false);

        writeStatus("Click on ajax button");
        clickById(ajax);
        waitForAjaxCompletion();
        AssertTextEquals(actionResultText, "item0");
        AssertTextEquals(selectionText, "item0,item2");
        //AssertTextEquals(activeItemText, "Item [item0]");

        writeStatus("Select one row");
        fireMouseEvent(firstRow, "click", 0, 0, false);
        writeStatus("Click on server link");
        clickCommandAndWait(server);
        AssertTextEquals(actionResultText, "item1");
        AssertTextEquals(selectionText, "item0");
        //AssertTextEquals(activeItemText, "Item [item0]");
        checkButtons(true, true, false, false);
    }

    @Test
    public void testJSFunctions(Template template) {
        renderPage(template);
        initFields();

        writeStatus("Select one row");
        fireMouseEvent(thirdRow, "click", 0, 0, false);

        writeStatus("Check if 'onupclick' event works");
        clickById(upButton);
        Assert.assertEquals(runScript("theLatestEvent"), "up orderchanged");

        writeStatus("Check if 'ondownclick' event works");
        clickById(downButton);
        Assert.assertEquals(runScript("theLatestEvent"), "down orderchanged");

        writeStatus("Check if 'ontopclick' event works");
        clickById(firstButton);
        Assert.assertEquals(runScript("theLatestEvent"), "top orderchanged");

        writeStatus("Check if 'onbottomclick' event works");
        clickById(lastButton);
        Assert.assertEquals(runScript("theLatestEvent"), "bottom orderchanged");
    }

    private void checkButtons(boolean firstDisabled, boolean upDisabled, boolean downDisabled, boolean lastDisabled) {
        if (firstDisabled) {
            Assert.assertTrue(isVisibleById(firstButtonDisabled));
            Assert.assertFalse(isVisibleById(firstButton));
        } else {
            Assert.assertFalse(isVisibleById(firstButtonDisabled));
            Assert.assertTrue(isVisibleById(firstButton));
        }

        if (upDisabled) {
            Assert.assertTrue(isVisibleById(upButtonDisabled));
            Assert.assertFalse(isVisibleById(upButton));
        } else {
            Assert.assertFalse(isVisibleById(upButtonDisabled));
            Assert.assertTrue(isVisibleById(upButton));
        }

        if (downDisabled) {
            Assert.assertTrue(isVisibleById(downButtonDisabled));
            Assert.assertFalse(isVisibleById(downButton));
        } else {
            Assert.assertFalse(isVisibleById(downButtonDisabled));
            Assert.assertTrue(isVisibleById(downButton));
        }

        if (lastDisabled) {
            Assert.assertTrue(isVisibleById(lastButtonDisabled));
            Assert.assertFalse(isVisibleById(lastButton));
        } else {
            Assert.assertFalse(isVisibleById(lastButtonDisabled));
            Assert.assertTrue(isVisibleById(lastButton));
        }
    }

    private void initFields() {
        String parentId = getParentId() + "_form:orderingList";

        firstButton = parentId + "first";
        firstButtonDisabled = parentId + "disfirst";

        upButton = parentId + "up";
        upButtonDisabled = parentId + "disup";

        downButton = parentId + "down";
        downButtonDisabled = parentId + "disdown";

        lastButton = parentId + "last";
        lastButtonDisabled = parentId + "dislast";

        firstRow = parentId + ":0";
        thirdRow = parentId + ":2";

        actionResultText = getParentId() + "_form:actionResult";
        selectionText = getParentId() + "_form:selection";
        activeItemText = getParentId() + "_form:activeItem";
        ajax = firstRow + ":_ajax";
        server = parentId + ":1:_server";
    }

    public String getTestUrl() {
        return "pages/orderingList/orderingListTest.xhtml";
    }

}
