/* global Bdf,$$,Intl,TableSheet */

TableSheet.Column = function (index, name, title) {
    this.index = index;
    this.name = name;
    this.title = title;
    this.variant = "";
    this.cssClasses = "";
    this.context = {};
    this.match = "";
    this.attributeMap = new Map();
    this.withSum = false;
    this.sumValueArray = null;
};

TableSheet.Column.buildFromJson = function (index, data, locale, cssResolver) {
    var name = data.name;
    var column = new TableSheet.Column(index, name, data.title);
    var variant = "";
    if (index === 0) {
        variant = "edit";
    }
    for(let propKey in data.attributes) {
        let valueArray = data.attributes[propKey];
        switch(propKey) {
            case "format:datestyle":
                column.context.dateFormatter = new Bdf.DateFormatter(locale, valueArray[0]);
                break;
            case "table:columnstyle":
                column.cssClasses = cssResolver.resolveCss(name, valueArray);
                break;
            case "table:variant":
                variant = valueArray[0];
                break;
            default:
                 column.attributeMap.set(propKey, valueArray);
        }      
    }
    if (!column.cssClasses) {
        column.cssClasses = cssResolver.getDefaultCssClass(name);
    }
    if ((data.cast === "date") && (!column.context.dateFormatter)) {
        column.context.dateFormatter = new Bdf.DateFormatter(locale, "medium");
    }
    column.match = data.match;
    column.variant = variant;
    return column;
};

TableSheet.Column.toColumnArray = function (json, locale, cssResolver, sumJson) {
    let columnArray = [];
    let columnIndex = 0;
    for(let colData of json) {
        let column = TableSheet.Column.buildFromJson(columnIndex, colData, locale, cssResolver);
        columnArray.push(column);
        columnIndex++;
    }
    return columnArray;
};

TableSheet.Column.checkSum = function (columnArray, sumJson, cellFactory) {
    if (!sumJson) {
        return false;
    }
    for(let column of columnArray) {
        let sumObj = sumJson[column.index];
        if ((sumObj) || (sumObj === 0)) {
            column.withSum = true;
            if (typeof sumObj === "object") {
                if (sumObj.moneyArray) {
                    let array = [];
                    for(let moneyObj of sumObj.moneyArray) {
                        array.push({
                            value: moneyObj,
                            text: cellFactory.getAmountText(moneyObj)
                        });
                    }
                    column.sumValueArray = array;
                } else {
                    column.sumValueArray = [
                        {
                            value: sumObj,
                            text: cellFactory.getNumberText(sumObj)
                        }  
                    ];
                }
            } else {
                column.sumValueArray = [
                    {
                        value: sumObj,
                        text: sumObj.toString()
                    }  
                ];
            }
        }
    }    
    return true;
};