
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 {TableColumn} from "widget/table/TableColumn";
import {TableItem} from "widget/table/TableItem";
import {Composite} from "widget/Composite";

export class Table extends Composite {

    private static TemplateNormal = `
        <div style="overflow: auto; outline: 0;">
            <table style="outline: 0;" class="adore_table" tabindex="-1">
                <thead>
                <tr style="color:#000"> 
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>`;

    public columns = new Array();
    public items = new Array();

    public $attachNode: JQuery;
    public $tHead: JQuery;
    public $tBody: JQuery;

    public isFocus: boolean;
    public isSortable: boolean = true;

    public rowNumCol: TableColumn;

    public isCellFocus = false;
    public sortable = true;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.children = new Array();
        this.columns = new Array();
        this.items = new Array();
        this.$thisNode = $(Table.TemplateNormal).attr("id", this.id);
        this.$attachNode = this.$thisNode;
        this.$tHead = this.$thisNode.find("tr");
        this.$tBody = this.$thisNode.find("tbody");
        let own = this;
        this.isFocus = false;
        this.$thisNode.find("table").on("focus", function (e) {
            console.log("table focus in");
            own.sendFocusGain();
            own.isFocus = true;
        }).on("blur", function (e) {
            own.isFocus = false;
        });
        this.$thisNode.find("table").on("dblclick", function (e) {
            if (e.target.localName && e.target.localName === "th") {
                EventHub.dispatchEvent(MessageType.WIDGET, {
                    id: own.id,
                    evtType: Event.DOUBLE_CLICK,
                    header: true
                });
            } else {
                EventHub.dispatchEvent(MessageType.WIDGET, {
                    id: own.id,
                    evtType: Event.DOUBLE_CLICK
                });
            }
        });
    }

    public addChild(child: Widget) {
        super.addChild(child);
        if (child instanceof TableColumn) {
            // 添加TableColumn
            this.columns.push(child);
        } else if (child instanceof TableItem) {
            // 添加TableItem
            this.items.push(child);
        }
    }

    public sendFocusGain() {
        super.sendFocusGain();
        this.isFocus = true;
    }

    public setFocus() {
        console.log("table setFocus");
        // if (!this.isFocus) {
        //     this.$thisNode.find("table").focus();
        // }
        if(!this.isFocus){
            this.sendFocusGain();
        }
    }

    public layout() {
        super.layout();
        // 自己实现layout
        // for(let i = 0 ; i < this.columns.length ; i++){
        //     (<TableColumn>this.columns[i]).append();
        // }
        // for(let i = 0 ; i < this.items.length ; i++){
        //     (<TableItem>this.items[i]).append();
        // }
        // for(let i = 0 ; i < this.columns.length ; i++){
        //     (<TableColumn>this.columns[i]).layout();
        // }
        // for(let i = 0 ; i < this.items.length ; i++){
        //     (<TableItem>this.items[i]).layout();
        // }

    }

    // 设置每列的宽度，保证每列的宽度均衡,由TableColumn的layout发起调用
    public refreshColumnsWidth() {
        // let size = 0, i: number;
        // for (i = 0; i < this.columns.length; i++) {
        //     if (this.columns[i].visible) {
        //         size++;
        //     }
        // }
        // size = size <= 0 ? 1 : size;
        // let widthContent = (100.0 / size) + "%";
        // this.$tHead.find("th").css("width", widthContent);
    }

    public handleKeyDownEvent(e: KeyboardEvent, hasFilter: boolean) {
        super.handleKeyDownEvent(e, hasFilter);
    }

    public setSortable(args: { isSortable: boolean }) {
        this.isSortable = args.isSortable;
    }

    public containRowNum = false;
    public addRowNum(args: { containRowNum: boolean }) {
        // do nothing now
        this.containRowNum = args.containRowNum;
    }

    public addTableColumnCell(index: number, defaultData: string) {
        let itemSize = this.items.length;
        for (let i = 0; i < itemSize; i++) {
            this.items[i].addCell(index, defaultData);
        }
    }

    public reorder(args: { reorderIndexes: Array<number> }) {
        let reorderIndexed = args.reorderIndexes;
        // 收到新的序列号之后就要重新排序鸟。
        let newItems = new Array();
        for (let i = 0; i < reorderIndexed.length; i++) {
            newItems[i] = this.items[reorderIndexed[i]];
            (<TableItem>newItems[i]).$thisNode.detach();
        }
        this.items = newItems;
        this.refreshCellRowNum();
        for (let i = 0; i < this.items.length; i++) {
            (<TableItem>newItems[i]).$thisNode.appendTo(this.$tBody);
        }
    }
    // 重新刷下序号列的序号值
    public refreshCellRowNum(){
        for (let i = 0; i < this.items.length; i++) {
            this.items[i].refreshCellRowNum(i);
        }
    }


}


