/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.component.tristatecheckbox;

import javax.faces.event.ActionEvent;
import java.io.Serializable;

/**
 * Bean code for the <ice-cc:triStateCheckbox/> composition component,
 * which is able to represent a three-state checkbox, with unchecked, unchecked,
 * and partially checked associated states.
 *
 * @author philip.breau@icesoft.com
 */
public class TriStateCheckbox implements Serializable {

    /**
     * The unchecked state value
     */
    public static String UNCHECKED = "unchecked";
    public static String CHECKED = "checked";
    public static String PARTIALLY_CHECKED = "partial";

    public static String[] DEFAULT_CHECK_ORDER = {UNCHECKED, PARTIALLY_CHECKED, CHECKED};
    public static String[] UNCHECKED_CHECKED_ORDER = {UNCHECKED, CHECKED};
    public static String[] UNCHECKED_CHECKED_PARTIALLY_ORDER = {UNCHECKED, CHECKED, PARTIALLY_CHECKED};

    private String[] checkOrder = DEFAULT_CHECK_ORDER;
    private int checkIndex = 0;
    private String checkStatus = UNCHECKED;
    private boolean disabled = false;

    public TriStateCheckbox() {
    }

    public TriStateCheckbox(String[] checkOrder) {
        this.checkOrder = checkOrder;
        checkStatus = this.checkOrder[0];
    }

    public void checkboxClicked(ActionEvent event) {
        checkIndex++;
        if (checkIndex == checkOrder.length) {
            checkIndex = 0;
        }
        checkStatus = checkOrder[checkIndex];
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setPartiallyChecked() {
        this.checkStatus = PARTIALLY_CHECKED;
        updateCheckIndex();
    }

    public void setUnchecked() {
        this.checkStatus = UNCHECKED;
        updateCheckIndex();
    }

    public void setChecked() {
        this.checkStatus = CHECKED;
        updateCheckIndex();
    }

    private void updateCheckIndex() {
        for (int i = 0; i < checkOrder.length; i++) {
            if (checkOrder[i].equals(checkStatus)) {
                checkIndex = i;
                break;
            }
        }
    }

    public String getCheckStatus() {
        return checkStatus;
    }

    public boolean isChecked() {
        return CHECKED.equals(checkStatus);
    }

    public boolean isPartiallyChecked() {
        return PARTIALLY_CHECKED.equals(checkStatus);
    }

    public String getStatusImageKey() {
        return checkStatus + (disabled ? "-dis" : "");
    }

}
