
import {Widget} from "widget/Widget";
import {EventHub} from "event/EventHub";
import {MessageType} from "event/MessageType";
import {Event} from "event/Event";
import {Keys} from "hotkey/Keys";
import {ADORE} from "ADORE";
import {Table} from "widget/table/Table";
import {TableConstants} from "widget/table/TableConstants";

export class TableColumn extends Widget {

    private static TemplateNormal = `<th></th>`;

    private static TemplateCheck = `
        <th>
            <p tabindex="-1">
                <input type="checkbox" id="" tabindex="-1" class="filled-in"/>
                <label for="" style="margin-top: 9px"></label>
            </p>
        </th>`;

    private $attachNode: JQuery;
    
    // 用来赋值文本的
    private $textNode : JQuery;

    private type: string;

    private options: string[];

    private alignment: number;

    private alignmentContent: string;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(TableColumn.TemplateNormal).attr("id", this.id);
        this.$attachNode = this.$thisNode;
        this.$textNode = this.$attachNode;
        // 默认的cellType是None
        this.type = TableConstants.CELL_TYPE_NULL;
        this.visible = true;

        let that = this;
        let tableColumnId = this.id;
        this.$thisNode.on("click", function () {//给当前表头th增加点击事件
            if ((<Table>that.parent).isSortable === true) {
                let thisIndex = (<Table>that.parent).columns.indexOf(that);
                let dataType = (<Table>that.parent).columns[thisIndex].type; //获取所在列的类型
                EventHub.dispatchEvent(MessageType.WIDGET, {
                    id: tableColumnId,
                    col: thisIndex,
                    dataType: dataType,
                    evtType: Event.MODIFY
                });
            }
        });
    }

    public setText(args: { text: string }) {
        this.$textNode.text(args.text);
    }

    public layout() {
        this.doLayout();
    }

    private doLayout() {
        this.append();
        if (this.parent) {
            (<Table>this.parent).refreshColumnsWidth();
        }
    }

    public setType(args: { type: string }) {
        this.type = args.type;
        this.changeStyleToCheck();
    }

    public setOptions(args: { options: string[] }) {
        this.options = args.options;
    }

    public append() {
        if (this.parent && this.parent.getId()) {
            if ((<Table>this.parent).$tHead) {
                if (this.colIndex >= 0 && this.colIndex < ((<Table>this.parent).columns.length - 1)) {
                    let index = (<Table>this.parent).columns.indexOf(this);
                    (<Table>this.parent).columns.splice(index, 1);
                    this.$attachNode.insertBefore((<TableColumn>(<Table>this.parent).columns[this.colIndex]).$attachNode.eq(0));
                    (<Table>this.parent).columns.splice(this.colIndex, 0, this)
                    this.addTableColumnCell(this.colIndex);
                    // 重置为-1，下次就不使用到了
                    this.colIndex = -1;
                } else {
                    let tempStyle = this.$thisNode.html();
                    this.$thisNode.appendTo((<Table>this.parent).$tHead);
                }
            } else {
                this.$thisNode.appendTo((<Table>this.parent).$attachNode);
            }
        } else {
            this.$thisNode.appendTo(document.body);
        }
    }

    public setAlignment(args: { alignment: number }) {
        this.alignment = args.alignment;
        this.alignmentContent = "left";
        if (args.alignment === ADORE.RIGHT) {
            this.alignmentContent = "right";
        } else if (args.alignment === ADORE.CENTER) {
            this.alignmentContent = "center";
        }
        this.$thisNode.css("text-align", this.alignmentContent);
        let index = (<Table>this.parent).columns.indexOf(this);
        let tableItems = (<Table>this.parent).$tBody.find("tr");
        for (let i = 0; i < tableItems.length; i++) {
            $($(tableItems[i]).find("td")[index]).css({ "text-align": this.alignmentContent });
        }
    }

    public setVisible(args: { visible: boolean }) {
        super.setVisible(args);
        this.visible = args.visible;
        let index = (<Table>this.parent).columns.indexOf(this);
        let tableItems = (<Table>this.parent).$tBody.find("tr");
        let i: number;
        if (this.visible) {
            //刷新列可见
            for (i = 0; i < tableItems.length; i++) {
                $($(tableItems[i]).find("td")[index]).css({ "display": "block" });
            }
        } else {
            //刷新列不可见
            for (i = 0; i < tableItems.length; i++) {
                $($(tableItems[i]).find("td")[index]).css({ "display": "none" });
            }
        }
        if (this.parent) {
            (<Table>this.parent).refreshColumnsWidth();
        }
    }

    public setEnabled(args: { enabled: boolean }) {
        super.setEnabled(args);
        if (this.type === TableConstants.CELL_TYPE_NULL) {
            return;//NONE类型无需刷新enabled
        }
        let index = (<Table>this.parent).columns.indexOf(this);
        let tableItems = (<Table>this.parent).$tBody.find("tr");
        let i: number;
        let typeCell = "input";
        if (this.type === TableConstants.CELL_TYPE_BUTTON) {
            typeCell = "a";
        } else if (this.type === TableConstants.CELL_TYPE_COMBO) {
            typeCell = "select";
        }
        if (this.enabled) {
            //刷新列可用
            for (i = 0; i < tableItems.length; i++) {
                $($(tableItems[i]).find("td")[index]).find(typeCell).removeAttr("disabled");
            }
        } else {
            //刷新列不不用
            for (i = 0; i < tableItems.length; i++) {
                $($(tableItems[i]).find("td")[index]).find(typeCell).attr("disabled", "disabled");
            }
        }
    }

    public dispose() {
        super.dispose();
        if (this.parent) {
            let index = (<Table>this.parent).columns.indexOf(this);
            (<Table>this.parent).columns.splice(index, 1);
        }
    }

    public setFocus() {
    }

    public changeStyleToCheck() {
        if (this.type === TableConstants.CELL_TYPE_CHECK) {
            let tempStyle = this.$attachNode.attr("style");
            this.$attachNode.remove();
            this.$attachNode = $(TableColumn.TemplateCheck).attr("id", this.id);
            this.$thisNode = this.$attachNode;
            this.$attachNode.find("input").attr("id", this.id + "demo");
            this.$textNode = this.$attachNode.find("label");
            this.$textNode.attr("for", this.id + "demo");
            this.$attachNode.attr("style", tempStyle);
            let that = this;
            this.$attachNode.find("input").on("click", function () {
                // 处理选中事件
                let checked = $(this).prop("checked");
                let index = (<Table>that.parent).columns.indexOf(that);
                let size = (<Table>that.parent).items.length;
                for (let i = 0; i < size; i++) {
                    (<Table>that.parent).items[i].setCheckboxState(index, checked);
                }
                EventHub.dispatchEvent(MessageType.WIDGET, {
                    id: that.id,
                    col: (<Table>that.parent).columns.indexOf(that),
                    data: this.checked + "",
                    type: "handleChecked",
                    evtType: Event.MODIFY
                });
            });

        }
    }

    private width = -1;
    public setWidth(args: { width: number }) {
        this.width = args.width;
        let index = (<Table>this.parent).columns.indexOf(this);
        this.$attachNode.css("width", this.width + "px");
        let tableItems = (<Table>this.parent).$tBody.find("tr");
        let i: number;
        for (i = 0; i < tableItems.length; i++) {
            $($(tableItems[i]).find("td")[index]).css("width", this.width + "px");
        }
    }

    public setRowNumCol(args: { rowNumCol: boolean }) {
        if (args.rowNumCol) {
            let index = (<Table>this.parent).columns.indexOf(this);
            (<Table>this.parent).columns.splice(index, 1);
            // 指定序号列
            (<Table>this.parent).rowNumCol = this;
            this.$thisNode.unbind("click")
        }
    }

    public refreshRowNumCol(index: number) {
        // 从index行刷新下序号列的值
        let tableItems = (<Table>this.parent).$tBody.find("tr");
        for (let i = index; i < tableItems.length; i++) {
            $(tableItems[i]).find("td").eq(0).text((i + 1) + "");
        }
    }

    private colIndex: number = -1;
    public setColIndex(args: { colIndex: number }) {
        // 这个值是动态添加列才设置的，第一次layout的时候需要使用到，然后layout完后重置为-1，避免后续使用到出现问题
        this.colIndex = args.colIndex;
    }

    private fillData: string;
    public setFillData(args: { fillData: string }) {
        this.fillData = args.fillData;
    }

    public addTableColumnCell(index: number) {
        (<Table>this.parent).addTableColumnCell(index, this.fillData);
    }

}

