/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package grader.UI.gradebook;

import grader.logic.category.Category;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.table.TableColumn;

/**
 *
 * @author kellen
 */
public class CategoryHeader {
    public final static String PLACE_HOLDER = "!_this_is_only_a_place_holder_123456_%432*&^23&*)(%^";
    public final static int COL_HEIGHT = 20;
    public final static int BTN_WIDTH = 20;

    private ArrayList<TableColumn> cols;
    private Category category;
    private JTextField txtLabel;
    private ArrayList<CategoryHeader> children;
    private JToggleButton btnExpand;
    private Boolean isPlaceHolder;
    private Boolean isExpanded;

    public CategoryHeader(){
        cols  = new ArrayList<TableColumn>();
        children = new ArrayList<CategoryHeader>();
        category = new Category("");
        setuptxtLabel();
        setupBtnExpand();
        isPlaceHolder = false;
    }

    public CategoryHeader(TableColumn col, Category cat, Boolean placeHolder){
        children = new ArrayList<CategoryHeader>();
        cols  = new ArrayList<TableColumn>();
        cols.add(col);
        category = cat;
        setuptxtLabel();
        setupBtnExpand();
        isPlaceHolder = placeHolder;
    }

    public CategoryHeader(TableColumn col){
        children = new ArrayList<CategoryHeader>();
        cols  = new ArrayList<TableColumn>();
        cols.add(col);
        category = (Category)col.getIdentifier();
        setuptxtLabel();
        setupBtnExpand();
        isPlaceHolder = false;
    }

    public CategoryHeader(TableColumn col, Category parent){
        children = new ArrayList<CategoryHeader>();
        cols  = new ArrayList<TableColumn>();
        cols.add(col);
        category = parent;
        setuptxtLabel();
        setupBtnExpand();
        isPlaceHolder = false;
    }


    public void addChild(CategoryHeader child){
        children.add(child);
    }
    
    public void addCol(TableColumn col){
        cols.add(col);
        
        if(category.getName().equals("")){
            category = (Category)col.getIdentifier();
            txtLabel.setText(category.getName());
        }
        
    }

    public ArrayList<TableColumn> getCols(){
        return cols;
    }

    public JTextField getTextField(){
        // set correct size
        int size = 0;
        for(TableColumn col : cols){
            size += col.getWidth();
        }
        size = size - btnExpand.getPreferredSize().width;

        txtLabel.setSize(size, COL_HEIGHT);
        txtLabel.setPreferredSize(new Dimension(size, COL_HEIGHT));
        txtLabel.setMinimumSize(new Dimension(size, COL_HEIGHT));

        return txtLabel;
    }

    public JToggleButton getBtnExpand(){
        return btnExpand;
    }

    public Category getCategory(){
        return category;
    }

    public String getCategoryName(){
        return category.getName();
    }

    public Boolean isPlaceHolder(){
        return isPlaceHolder;
    }

    public Boolean isExpanded(){
        return isExpanded;
    }

    public void setIsExpanded(Boolean expanded){
        isExpanded = expanded;
    }
    
    public ArrayList<CategoryHeader> getChildren(){
        return children;
    }

    private void setupBtnExpand(){
        btnExpand = new JToggleButton();
        btnExpand.setText("<>");
        btnExpand.setToolTipText("Click this button to expand/collapse the category");
        btnExpand.setMaximumSize(new Dimension(BTN_WIDTH, CategoryHeader.COL_HEIGHT));
        btnExpand.setMinimumSize(new Dimension(BTN_WIDTH, CategoryHeader.COL_HEIGHT));
        btnExpand.setPreferredSize(new Dimension(BTN_WIDTH, CategoryHeader.COL_HEIGHT));
        btnExpand.setSize(new Dimension(BTN_WIDTH, CategoryHeader.COL_HEIGHT));

        isExpanded = true;
    }

    private void setuptxtLabel(){
        txtLabel = new JTextField();
        txtLabel.setText(category.getName());
    }
}