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

import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

public abstract class BaseRichDataColumn implements IRichDataColumn, Serializable {

    protected static int COLUMN_TYPE_TEXT = 0, COLUMN_TYPE_DATE = 1, COLUMN_TYPE_LIST = 2, COLUMN_TYPE_BOOLEAN = 3;

    protected int type;
    protected boolean editable;
    protected boolean moveable;
    protected boolean shownByDefault;
    protected String property;
    protected String titleProperty;
    protected String title;
    protected boolean sortable;
    protected boolean hidden;
    protected boolean filterable;
    protected boolean hideable;
    boolean firstInTable;
    boolean lastInTable;
    protected String width = "100px";

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public boolean isFirstInTable() {
        return firstInTable;
    }

    public boolean isLastInTable() {
        return lastInTable;
    }

    public boolean isHidden() {
        return hidden;
    }

    public BaseRichDataColumn(String property, String title, int type, boolean editable, boolean moveable,
                              boolean shownByDefault, boolean sortable, boolean filterable, boolean hideable) {
        super();
        this.title = title;
        this.type = type;
        this.editable = editable;
        this.moveable = moveable;
        this.shownByDefault = this.hidden = !shownByDefault;
        this.property = property;
        this.sortable = sortable;
        this.filterable = filterable;
        this.hideable = hideable;
    }

    public BaseRichDataColumn(String property, String title, int type) {
        this(property, title, type, true, true, true, true, true, true);
    }

    public String getTitle() {
        return title;
    }

    public int getType() {
        return type;
    }

    public boolean isEditable() {
        return editable;
    }

    public boolean isMoveable() {
        return moveable;
    }

    public boolean isShownByDefault() {
        return shownByDefault;
    }

    public String getProperty() {
        return property;
    }

    public boolean isSortable() {
        return sortable;
    }

    public String getTypeName() {
        if (type == COLUMN_TYPE_BOOLEAN) {
            return "boolean";
        } else if (type == COLUMN_TYPE_TEXT) {
            return "text";
        } else if (type == COLUMN_TYPE_DATE) {
            return "date";
        } else if (type == COLUMN_TYPE_LIST) {
            return "list";
        }
        return null;

    }


    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    public String toString() {
        return this.getClass().getName() + ", property=" + this.property + ", title=" + this.title;
    }

    public boolean isFilterable() {
        return filterable;
    }

    public static IRichDataColumn asIRichDataColumn(PropertyDescriptor descriptor) {
        IRichDataColumn column = null;
        String property = descriptor.getName();
        String title = propertyAsTitle(property);

        Class type = descriptor.getPropertyType();

        if (type == boolean.class || descriptor.getPropertyType() == Boolean.class) {
            column = new RichDataBooleanColumn(property, title);
        } else if (type.isPrimitive() || type == String.class ||
                type == Integer.class || type == Long.class || type == Double.class || type == BigDecimal.class ||
                type == BigInteger.class) {
            column = new RichDataTextColumn(descriptor.getPropertyType(), property, title);
        } else if (descriptor.getPropertyType() == Date.class) {
            column = new RichDataDateColumn(property, title);
        }


        return column;
    }

    public static String propertyAsTitle(String property) {

        char[] chars = property.toCharArray();
        StringBuffer buff = new StringBuffer();
        buff.append(Character.toUpperCase(chars[0]));
        boolean delim = false;
        for (int i = 1; i < chars.length; i++) {
            if (delim) {
                delim = false;
            }
            if (Character.isUpperCase(chars[i]) ||
                    (Character.isDigit(chars[i]) && !Character.isDigit(chars[i - 1]))) {
                buff.append(" ");
            }
            buff.append(chars[i]);
        }


        return buff.toString();
    }

    public boolean isHideable() {
        return hideable;
    }

    public void setHideable(boolean hideable) {
        this.hideable = hideable;
    }

	public String getTitleProperty() {
		return titleProperty;
	}

	public void setTitleProperty(String titleProperty) {
		this.titleProperty = titleProperty;
	}
    
    
}
