/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
import {Widget} from "widget/Widget";
import {Composite} from "widget/Composite";
import {ADORE} from "ADORE";
import {EventHub} from "event/EventHub";
import {MessageType} from "event/MessageType";
import {Event} from "event/Event";

export class FlatTree extends Widget {
    private static Template = `
        <div class="adore_flatTree">
            <div class="breadCumb">
                <!-- 根目录 -->
                <a href="javascript:void(0)"></a>
                <div>
                    <span>&gt;</span>
                    <a href="javascript:void(0)"></a>
                </div>
                <div>
                    <span>&gt;</span>
                    <a href="javascript:void(0)"></a>
                </div>
                <div>
                    <span>&gt;</span>
                    <a href="javascript:void(0)"></a>
                </div>
            </div>
            <div class="wrap_flatTree">
            </div>
            <div class="wrap_flatTree">
            </div>
            <div class="wrap_flatTree">
            </div>
            <div class="wrap_flatTree">
            </div>
        </div>`;
    private static TemplateOneItem = `
        <a class="oneItem" href="javascript:void(0)">
          <img class="img " src="images/flat_tree/u152.png">
          <span></span>
        </a>`;
    private static TemplateOtherItem  = `
        <a class="otherItem" href="javascript:void(0)">
          <img class="img " src="">
          <span></span>
        </a>`;
    private contentOne: any;
    private contentTwo: any;
    private contentThree: any;
    private contentFour: any;
    private oneDegree: JQuery;
    private twoDegree: JQuery;
    private threeDegree: JQuery;
    private fourDegree: JQuery;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(FlatTree.Template).attr("id", this.id);
        this.oneDegree = this.$thisNode.find(".wrap_flatTree:eq(0)").attr("id", "one_"+this.id);
        this.twoDegree = this.$thisNode.find(".wrap_flatTree:eq(1)").attr("id", "two_"+this.id);
        this.threeDegree = this.$thisNode.find(".wrap_flatTree:eq(2)").attr("id", "three_"+this.id);
        this.fourDegree = this.$thisNode.find(".wrap_flatTree:eq(3)").attr("id", "four_"+this.id);
        this.contentOne = new Array();
        this.contentTwo = new Array();
        this.contentThree = new Array();
        this.contentFour = new Array();
    }

    /*args: { degree:string, text: string [], parentId: string}*/
    public setContents(args: { attribute: string }) {
        let data = JSON.parse(args.attribute);
        for (let key in data) {
            var cutKey: any;
            var location: any;
            var m: number;
            var n: number;
            var k: number;
            var h: number;
            var belongTo = key.length - key.replace(/,/g, "").length;
            switch (belongTo) {
                       case 0:
                            this.contentOne[parseInt(key)] = data[key];
                            this.contentTwo[parseInt(key)] = new Array();
                            this.contentThree[parseInt(key)] = new Array();
                            this.contentFour[parseInt(key)] = new Array();
                            break;

                        case 1:
                            cutKey = key.substr(1, key.length - 2);
                            location = cutKey.split(",");
                            m = Number(location[0]);
                            n = Number(location[1]);
                            this.contentTwo[m][n] = data[key];
                            this.contentThree[m][n] = new Array();
                            this.contentFour[m][n] = new Array();
                            break;

                        case 2:
                            cutKey = key.substr(1, key.length - 2);
                            location = cutKey.split(",");
                            var m = Number(location[0]);
                            var n = Number(location[1]);
                            var k = Number(location[2]);
                            this.contentThree[m][n][k] = data[key];
                            this.contentFour[m][n][k] = new Array();
                            break;
                            
                        case 3:
                            cutKey = key.substr(1,key.length -2);
                            location = cutKey.split(",");
                            var m = Number(location[0]);
                            var n = Number(location[1]);
                            var k = Number(location[2]);
                            var h = Number(location[3]);
                            this.contentFour[m][n][k][h] = data[key];
                            break;
                            
                        default:
                            console.log("data error");
                            break;
             }
        }
    }

    /*setIconPath(args:{iconPath:string})*/
    public setIconPaths(args: { iconPaths: string }) {
        let pathData = JSON.parse(args.iconPaths);
        for (var key in pathData) {
            //修改路径的，避免路径错误
            var path = pathData[key].replace(/\\/g, "\\\\");
            this.oneDegree.find("img:eq("+ parseInt(key)+")").attr("src",path);
        }
    }

    /*setRootTitle(args:{rootTitle:string})*/
    public setRootTitle(args: { rootTitle : string}){
        this.$thisNode.find(".breadCumb a:eq(0)").html(args.rootTitle);
    }

    public initFlatTree() {
        //节点事件绑定和初始化
        var Index1 = 0;
        var Index2 = 0;
        var Index3 = 0;
        var Index4 = 0;
        var own = this;
        this.$thisNode.find(".breadCumb a:eq(0)").html("全部菜单");
        //第一层数据加载
        for (var i = 0, z = this.contentOne.length; i < z; i++) {
                $(FlatTree.TemplateOneItem).appendTo(this.oneDegree);
                this.oneDegree.find("span:eq("+i+")").text(this.contentOne[i]);
                this.oneDegree.show();
        }
        //第一层的绑定事件
        this.oneDegree.on("click","a",{
            contentTwo: this.contentTwo,
            contentThree: this.contentThree
        },function(event){
            Index1 = own.oneDegree.find("a").index($(this));
            if (event.data.contentTwo[Index1].length > 0) {
                for (var i = 0, z = event.data.contentTwo[Index1].length; i < z; i++) {
                        $(FlatTree.TemplateOtherItem).appendTo(own.twoDegree);
                        own.twoDegree.find("span:eq("+i+")").text(event.data.contentTwo[Index1][i]);
                        if (event.data.contentThree[Index1][i].length > 0) {
                            own.twoDegree.find("img:eq("+i+")").attr("src","images/flat_tree/u235.png");
                        }
                        else{
                            own.twoDegree.find("img:eq("+i+")").attr("src","images/flat_tree/u88.png");
                        }
                }
                own.$thisNode.find(".breadCumb a:eq(1)").html($(this).find("span").html());
                own.$thisNode.find(".breadCumb div:eq(0)").css("display","inline-block");
                own.oneDegree.hide();
                own.twoDegree.show();
            }
            var content = "" + Index1;
            //传递一级节点信息
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                location: content,
                evtType: Event.CLICK
            });
        });
        //第二层绑定事件
        this.twoDegree.on("click","a",{
            contentThree: this.contentThree,
            contentFour: this.contentFour
        },function(event){
            Index2 = own.twoDegree.find("a").index($(this));
            if (event.data.contentThree[Index1][Index2].length > 0) {
                for (var i = 0, z = event.data.contentThree[Index1][Index2].length; i < z; i++) {
                        $(FlatTree.TemplateOtherItem).appendTo(own.threeDegree);
                        own.threeDegree.find("span:eq("+i+")").text(event.data.contentThree[Index1][Index2][i]);
                        if (event.data.contentFour[Index1][Index2][i].length > 0) {
                            own.threeDegree.find("img:eq("+i+")").attr("src","images/flat_tree/u235.png");
                        }
                        else{
                            own.threeDegree.find("img:eq("+i+")").attr("src","images/flat_tree/u88.png");
                        }
                }
                own.$thisNode.find(".breadCumb a:eq(2)").html($(this).find("span").html());
                own.$thisNode.find(".breadCumb div:eq(1)").css("display","inline-block");
                own.twoDegree.hide();
                own.threeDegree.show();
            }
            var content = '[' + Index1 + ',' + Index2 + ']';
            //传递二级节点信息
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                location: content,
                evtType: Event.CLICK
            });
        });
        //第三层绑定事件
        this.threeDegree.on("click","a",{
            contentFour: this.contentFour
        },function(event){
            Index3 = own.threeDegree.find("a").index($(this));
            if (event.data.contentFour[Index1][Index2][Index3].length > 0) {
                for (var i = 0, z = event.data.contentFour[Index1][Index2][Index3].length; i < z; i++) {
                        $(FlatTree.TemplateOtherItem).appendTo(own.fourDegree);
                        own.fourDegree.find("span:eq("+i+")").text(event.data.contentFour[Index1][Index2][Index3][i]);
                        own.fourDegree.find("img:eq("+i+")").attr("src","images/flat_tree/u88.png");
                }
                own.$thisNode.find(".breadCumb a:eq(3)").html($(this).find("span").html());
                own.$thisNode.find(".breadCumb div:eq(2)").css("display","inline-block");
                own.threeDegree.hide();
                own.fourDegree.show();
            }
            var content = '[' + Index1 + ',' + Index2 + ',' + Index3 + ']';
            //传递三级节点信息
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                location: content,
                evtType: Event.CLICK
            });

        });
        //第四层绑定事件
        this.fourDegree.on("click","a",
            function(event){
            Index4 = own.fourDegree.find("a").index($(this));
            var content = '[' + Index1 + ',' + Index2 + ',' + Index3 + ','+Index4+']';
            //传递第四级节点信息
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                location: content,
                evtType: Event.CLICK
            });
        });
        //面包屑事件
        own.$thisNode.find(".breadCumb a").on("click",function(){
            var index_bread = own.$thisNode.find(".breadCumb a").index($(this));
            own.$thisNode.find(".wrap_flatTree:eq("+index_bread+")").show().siblings(".wrap_flatTree").hide();
            own.$thisNode.find(".wrap_flatTree:eq("+index_bread+")").nextAll().empty();
            if(index_bread>0){
                $(this).parent("div").nextAll("div").hide();
            }
            else{
                $(this).nextAll("div").hide();
            }
        });
    }

    public dispose() {
        super.dispose();
    }
}