/*
 * 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.tabs;

import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class TabsController implements Serializable {

    private Tab currentTab;
    private Stack<Tab> previousTabs = new Stack<Tab>();
    private List<Tab> tabs = new ArrayList<Tab>();
    private List<Tab> openTabs = new ArrayList<Tab>();

    public Tab getCurrentTab() {
        return currentTab;
    }

    public List getOpenTabs() {
        return openTabs;
    }

    public void setCurrentTab(Tab newCurrentTab) {
        if (!newCurrentTab.isClosed()) {
            previousTabs.push(this.currentTab);
            this.currentTab = newCurrentTab;
            if (!openTabs.contains(newCurrentTab)) {
                openTabs.add(currentTab);
            }
        }

    }

    public void closeTab(ActionEvent evt) {
        Tab tab = (Tab) evt.getComponent().getAttributes().get("tab");
        tab.setClosed(true);
        if (previousTabs.contains(tab)) {
            previousTabs.remove(tab);
        }
        openTabs.remove(tab);
        if (currentTab == tab) {
            currentTab = getPreviousTab();
            if (currentTab == null) {
                currentTab = getNextTab();
            }
        }
        else if( currentTab == null ){
           currentTab = getNextTab();
        }
    }

    public Tab getPreviousTab() {
        Tab tab = null;
        if (previousTabs.size() > 0) {
            tab = previousTabs.pop();
        }
        return tab;
    }

    public Tab getNextTab() {
        Tab tab = null;
        if( currentTab == null && openTabs.size() > 0 ){
           tab = (Tab)openTabs.get(0);
        }
        else if (currentTab != null && openTabs.size() > 0) {
            int currentIndex = tabs.indexOf(currentTab);
            int newIndex = currentIndex + 1;
            if (newIndex > openTabs.size()) {
                newIndex = 0;
            }
            tab = openTabs.get(newIndex);
        }
        return tab;
    }

    public void addTab(Tab tab) {
        tabs.add(tab);
        if (!tab.isClosed()) {
            openTabs.add(tab);
        }
        if (currentTab == null)
            currentTab = tab;
    }

    public int getNumberOpenTabs() {
        return openTabs.size();
    }

}