/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 liushuai on 2015/1/21.
 */
define(["adoreapp/widget/BaseWidget"
    , "adoreapp/widget/WidgetManager"
    , "adoreapp/layout/HeaderFooterLayout"
    , "adoreapp/layout/RowGridLayout"
    , "adoreapp/layout/SashLayout"
    , "adoreapp/layout/FillLayout"
    , "adoreapp/widget/constants/ADORE"], function (BaseWidget, Manager, HeaderFooterLayout, RowGridLayout, SashLayout, FillLayout, ADORE) {
    "use strict";
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.style = attrs.style;
                this.children = new Array();
                //var template = attrs.template;
                //this.templates = template.split("<!-- split -->");
            },

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

            _setLayout: function (attrs) {
                var layout = attrs.layout;
                var type = layout.type;
                if (type === "HeaderFooterLayout") {
                    this.layout = new HeaderFooterLayout();
                    this.layout.setShowStyle(layout.showStyle);
                }

                if (type === "RowGridLayout") {
                    this.layout = new RowGridLayout();
                    this.layout.setRow(layout.row);
                    var rowTemplate = "<div class='row'>";
                    for (var i = 0; i < layout.row; i++) {
                        this.layout.pushRow($(rowTemplate));
                    }
                }

                if (type === "SashLayout") {
                    this.layout = new SashLayout();
                    var leftSide = $("<div>").css("padding", "0px");
                    var rightSide = $("<div>").css("padding", "0px");
                    this.layout.setLeftSide(leftSide);
                    this.layout.setRightSide(rightSide);
                }

                if (type === "FillLayout") {
                    this.layout = new FillLayout();
                }

                if (attrs.layout.style) {
                    this.layout.setStyles(attrs.layout.style);
                }
            },

            _layout: function (attrs) {
                if (attrs)
                    this.template = attrs.template;
                var layout = this.father ? this.father.layout : undefined;
                var isHorizontal = false;
                var horizontalBodyLeft = 0;
                var horizontalBodyRight = 0;
                var verticalBodyTop = 0;
                var verticalBodyBottom = 0;
                var isHeaderAndFooter = false;
                //1. 根据父容器的layout类型创建自身jquery实例
                if (layout) {
                    var parentLayoutType = layout.toString();
                    if (parentLayoutType === "HeaderFooterLayout") {
                        var showStyle = layout.getShowStyle();
                        var style = this.layoutData.getStyle();
                        var height = this.layoutData.getHeight();
                        if (showStyle === layout.getHorizontal()) {
                            isHorizontal = true;
                        }
                        if (style === this.layoutData.HEADER) {
                            isHeaderAndFooter = true;
                            //1.1 header
                            if (this.style === ADORE.Nav) {
                                if (showStyle === layout.getHorizontal()) {
                                    this.jqueryNode = $(this.template).attr("id", this.id).removeClass("adore_header").addClass("adore_header_horizontal").css({width: height + "px"});
                                    this.jqueryNode.find(".container").css({width: height, lineHeight: height + "px"});
                                } else {
                                    this.jqueryNode = $(this.template).attr("id", this.id).css({height: height + "px"});
                                    this.jqueryNode.find(".container").css({height: height, lineHeight: height + "px"});
                                }
                                this.attachNode = this.jqueryNode.find("div");
                            } else {
                                if (showStyle === layout.getHorizontal()) {
                                    this.jqueryNode = $("<div>").attr("id", this.id)
                                        .css({position: "absolute", height: "100%", width: height + "px"});
                                    this.attachNode = this.jqueryNode;
                                } else {
                                    this.jqueryNode = $("<div>").attr("id", this.id)
                                        .css({position: "absolute", width: "100%", height: height + "px"});
                                    this.attachNode = this.jqueryNode;
                                }
                            }
                        }
                        else if (style === this.layoutData.FOOTER) {
                            isHeaderAndFooter = true;
                            //1.2 footer
                            if (this.style === ADORE.Footer) {
                                if (showStyle === layout.getHorizontal()) {
                                    this.jqueryNode = $(this.template).attr("id", this.id).removeClass("adore_footer").addClass("adore_footer_horizontal").
                                        css({width: height + "px"});
                                    this.attachNode = this.jqueryNode;
                                } else {
                                    this.jqueryNode = $(this.template).attr("id", this.id).css({height: height + "px"});
                                    this.attachNode = this.jqueryNode;
                                }
                            } else {
                                if (showStyle === layout.getHorizontal()) {
                                    this.jqueryNode = $("<div>").attr("id", this.id)
                                        .css({
                                            position: "absolute",
                                            height: "100%",
                                            width: height + "px",
                                            bottom: "0px",
                                            top: "0px",
                                            right: "0px"
                                        });
                                    this.attachNode = this.jqueryNode;
                                } else {
                                    this.jqueryNode = $("<div>").attr("id", this.id)
                                        .css({
                                            position: "absolute",
                                            width: "100%",
                                            height: height + "px",
                                            bottom: "0px",
                                            left: "0px",
                                            right: "0px"
                                        });
                                    this.attachNode = this.jqueryNode;
                                }
                            }
                        }
                        else if (style === this.layoutData.BODY) {
                            //1.3 body
                            var top = layout.getHeader_height();
                            var bottom = layout.getFooter_height();
                            if (showStyle === layout.getHorizontal()) {
                                horizontalBodyLeft = top;
                                horizontalBodyRight = bottom;
                                this.jqueryNode = $(this.template).removeClass("adore_body").addClass("adore_body_horizontal").attr("id", this.id).css({
                                    left: top + "px",
                                    right: bottom + "px"
                                });
                                this.attachNode = this.jqueryNode;
                            } else {
                                verticalBodyTop = top;
                                verticalBodyBottom = bottom;
                                this.jqueryNode = $(this.template).attr("id", this.id).css({
                                    top: top + "px",
                                    bottom: bottom + "px"
                                });
                                this.attachNode = this.jqueryNode;
                            }
                        }
                        this.jqueryNode.appendTo(this.father.attachNode);
                    } else if (parentLayoutType === "SashLayout") {
                        var side = this.layoutData.getSide();
                        var smallColumn = this.layoutData.getSmallColumn();
                        var middleColumn = this.layoutData.getMiddleColumn();
                        var largeColumn = this.layoutData.getLargeColumn();
                        var style;
                        if (side === 0) {
                            //1.4 left
                            style = "col s12 m12 l" + largeColumn;
                            this.jqueryNode = layout.getLeftSide().css({
                                position: "absolute",
                                top: 0,
                                bottom: 0
                            }).addClass(style);
                        } else {
                            //1.5 right
                            style = "col s12 m12 l" + largeColumn + " offset-l" + (12 - largeColumn);
                            this.jqueryNode = layout.getRightSide().css({
                                position: "absolute",
                                top: 0,
                                bottom: 0
                            }).addClass(style);
                        }
                        this.attachNode = this.jqueryNode;
                    }
                }

                //2 如果没有设置layout则创建一个div
                if (!this.jqueryNode) {
                    this.jqueryNode = $("<div>").attr("id", this.id);
                    this.attachNode = this.jqueryNode;
                    base.append.call(this);
                }

                //2.1 检查是不是RowGridLayout并实例化jquery实例
                base._layout.call(this);

                //3 检查自身的layout类型，必要的jquery节点append到先前建立好的Jquery实例上。
                var ownLayout = this.layout ? this.layout : undefined;
                if (ownLayout) {
                    var layoutType = this.layout.toString();

                    if (layoutType === "RowGridLayout") {
                        var row = this.layout.getRowNumber();
                        for (var i = 0; i < row; i++) {
                            this.layout.getRow(i).appendTo(this.attachNode);
                            if (i === (row - 1))
                                this.layout.getRow(i).css("marginBottom", "0px");
                            if (this.rowCssStyle) {
                                this.layout.getRow(i).css(this.rowCssStyle);
                            }
                        }
                    } else if (layoutType === "SashLayout") {
                        var sashNode = $(this.template).appendTo(this.attachNode);
                        this.layout.getLeftSide().appendTo(sashNode);
                        this.layout.getRightSide().appendTo(sashNode);
                    } else if (layoutType === "FillLayout" || layoutType === "SashLayout") {
                        var layoutData = this.father ? this.father.layoutData : undefined;
                        if (layoutData) {
                            if (layoutData.toString() === "HeaderFooterLayoutData" && !isHorizontal) {
                                var parentStyle = layoutData.getStyle();
                                if (parentStyle === layoutData.HEADER && layoutData.getHeader_height) {
                                    //header
                                    this.jqueryNode.css({
                                        position: "absolute",
                                        height: layoutData.getHeader_height(),
                                        top: "0px",
                                        left: "0px",
                                        right: "0px"
                                    });
                                } else if (parentStyle === layoutData.FOOTER && layoutData.getFooter_height) {
                                    //footer
                                    this.jqueryNode.css({
                                        position: "absolute",
                                        height: layoutData.getFooter_height(),
                                        bottom: "0px",
                                        left: "0px",
                                        right: "0px"
                                    });
                                } else if (parentStyle === layoutData.BODY) {
                                    //main
                                    this.jqueryNode.css({
                                        position: "absolute",
                                        top: "0px",
                                        bottom: "0px",
                                        left: "0px",
                                        right: "0px"
                                    });
                                }
                            }
                        } else if(!isHeaderAndFooter){
                            this.jqueryNode.css({
                                position: "absolute",
                                top: (!verticalBodyTop || verticalBodyTop <= 0) ? "0" : verticalBodyTop + "px",
                                bottom: (!verticalBodyBottom || verticalBodyBottom <= 0) ? "0" : verticalBodyBottom + "px",
                                left: (!horizontalBodyLeft || horizontalBodyLeft <= 0) ? "0": horizontalBodyLeft + "px",
                                right: (!horizontalBodyRight || horizontalBodyRight <= 0)? "0" :horizontalBodyRight + "px"
                            });
                        }
                    }

                    if (this.layout.getStyles())
                        this.jqueryNode.css(this.layout.getStyles());
                }

                var normal = 0x00;
                var control = 0x15;

                if ((this.style & control) === control)
                    this.jqueryNode.css({
                        position: "absolute", /*width:"100%",*/
                        top: "48px",
                        bottom: "0px",
                        overflow: "hidden"
                    });

                //样式
                if (this.jqueryNode && this.cssStyle) {
                    this.jqueryNode.css(this.cssStyle);
                }
                //背景图
                if (this.jqueryNode && this.backgroundImage) {
                    this._setBackgroundImage({src: this.backgroundImage});
                }
                if (this.divImage) {
                    this._setImageInDiv({divImageSrc: this.divImage});
                }

                //背景图片的z-index
                if (this.imageZIndex === 0 || this.imageZIndex) {
                    this._setBackgroundImageZIndex({"imageZIndex": this.imageZIndex});
                }
            },

            _setBackgroundImage: function (attrs) {
                this.backgroundImage = attrs.src;
                if (this.jqueryNode) {
                    this.jqueryNode.find("#" + this.id + "_img").remove();//先删除之前的img
                    $("<img>").addClass("adore_background_image").attr({
                        src: this.backgroundImage,
                        id: this.id + "_img"
                    }).prependTo(this.jqueryNode);
                    if (this.imageZIndex === 0 || this.imageZIndex) {
                        this._setBackgroundImageZIndex({"imageZIndex": this.imageZIndex});
                    }
                }
            },

            _setImageInDiv: function (attrs) {
                this.divImage = attrs.divImageSrc;
                if (this.jqueryNode) {
                    this.jqueryNode.css("background-image", "url(" + this.divImage + ")");
                    this.jqueryNode.css("background-repeat", "round");
                }
            },


            _setBackgroundImageZIndex: function (attrs) {
                this.imageZIndex = attrs.imageZIndex;
                var imageNode = $("#" + this.id + "_img");
                imageNode.css("z-index", this.imageZIndex);
                //imageNode.css("zIndex", this.imageZIndex+" !important");
            },

            _rowCss: function (attrs) {
                if (!this.rowCssStyle) {
                    this.rowCssStyle = {};
                }
                this.rowCssStyle = $.extend(this.rowCssStyle, attrs.rowCssStyle);
            }

        };
    });
});
