/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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.
 */
/**
 * Created by huangdeyun on 2015/3/25.
 */
define(["adoreapp/widget/BaseWidget", "adoreapp/widget/EventHub"], function (BaseWidget, EventHub) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.children = new Array();
                var template = attrs.template;
                this.templates = template.split("<!-- split -->");
                // init时，是普通节点
                this.jqueryNode = $(this.templates[0]).attr("id", this.id);
                this.attachNode = this.jqueryNode;
                this.a = this.jqueryNode.find("a");
                this.hadChildren = false;
                this.isTreeItem = true;
                if(this.parent && this.father.treeId){
                    this.treeId = this.father.treeId;
                }
            },

            addChild: function (child) {
                this.children.push(child);
            },

            _layout: function () {
                this.append();
                //$("#"+this.treeId).find(".collapsible").collapsible();//
                if(this.father){
                    var treeNode = this.father.jqueryNode.find(".collapsible");
                    if(treeNode){
                        treeNode.collapsible();
                    }
                }
                //var tmpId = this.treeId;
                var itemId = this.id;
                var own = this;
                // 添加click事件
                this.a.on("click",function(e){
//                    $("#"+tmpId).find("li.bold").removeClass("adore_tree_node_selected");
//                    $(this.parentNode).addClass("adore_tree_node_selected");
//                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET,{id:itemId, treeId:tmpId, evtType:EventHub.CLICK});
                    own.handleClick(itemId,$(this.parentNode));
                });
            },

            handleClick:function(itemId,itemNode){
                var tmpId = this.treeId;
                var tree = $("#"+tmpId);
                tree.find("li.bold").removeClass("adore_tree_node_selected");
                itemNode.addClass("adore_tree_node_selected");
                var treeNodeCircle = itemNode.find("div.adore_tree_node_circle");
                if(treeNodeCircle.length !=0){
                    var circleDiv =treeNodeCircle.find("div");
                    //说明是复杂节点，需要展开
                    if(treeNodeCircle.attr("expand") ==="false"){
                        treeNodeCircle.attr("expand","true");
                        treeNodeCircle.removeClass("adore_circle").addClass("adore_mincircle");
                        circleDiv.text("-");
                        circleDiv.removeClass("adore_addcharpan").addClass("adore_mincharpan");
                    }else{
                        treeNodeCircle.attr("expand","false");
                        treeNodeCircle.removeClass("adore_mincircle").addClass("adore_circle");
                        circleDiv.text("+");
                        circleDiv.removeClass("adore_mincharpan").addClass("adore_addcharpan");
                    }
                }

                EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET,{id:itemId, treeId:tmpId, evtType:EventHub.CLICK});
            },

            _createChildren: function () {
                if (!this.father.hadChildren) {
                    //用复杂节点替换原来的普通节点
                    this.father.jqueryNode.remove();
                    // 第二层及以上的复杂节点
                    if(this.father.parent && this.father.father._ul){
                        this.father.jqueryNode = $(this.father.templates[2]).attr("id", this.father.id);
                    }
                    // 第一层复杂节点
                    else{
                        this.father.jqueryNode = $(this.father.templates[1]).attr("id", this.father.id);
                    }
                    this.father.attachNode = this.father.jqueryNode;
                    this.father.a = this.father.jqueryNode.find("a");
                    //var tmpId = this.treeId;
                    var own = this;
                    var itemId = this.father.id;
                    // 添加click事件
                    this.father.a.on("click",function(e){
//                        $("#"+tmpId).find("li.bold").removeClass("adore_tree_node_selected");
//                        $(this.parentNode).addClass("adore_tree_node_selected");
//                        EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET,{id:itemId, treeId:tmpId, evtType:EventHub.CLICK});
                        own.handleClick(itemId,$(this.parentNode));
                    });
                    this.father._ul = this.father.jqueryNode.find("ul.collapsible").find("ul");
                    if (this.father.text) {
                        this.father.a.text(this.father.text);
                    }
                    this.father.hadChildren = true;
                    //append
                    if (this.father.parent) {
                        if(this.father.father._ul){
                            this.father.jqueryNode.appendTo(this.father.father._ul);
                        }else{
                            this.father.jqueryNode.appendTo(this.father.father.attachNode);
                        }
                    } else {
                        this.father.jqueryNode.appendTo(document.body);
                    }
                }
            },

            append: function () {
                if (this.parent) {
                    if (this.father.isTreeItem) {
                        this._createChildren();
                        this.jqueryNode.appendTo(this.father._ul);
                    } else {
                        this.jqueryNode.appendTo(this.father.attachNode);
                    }
                } else {
                    this.jqueryNode.appendTo(document.body);
                }
            },

            _setText: function (attrs) {
                this.text = attrs.text;
                this.a.text(this.text);
            }
        }
    })
})