/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 {MessageDialog} from "widget/MessageDialog";
import {HeaderFooterLayout} from "layout/HeaderFooterLayout";
import {Layout} from "layout/Layout";
import {RowGridLayout} from "layout/RowGridLayout";
import {FillLayout} from "layout/FillLayout";
import {HeaderFooterLayoutData} from "layout/HeaderFooterLayoutData";
import {SideNavLayout} from "layout/SideNavLayout";
import {ADORE} from "ADORE";
export class Composite extends Widget {
    public $attachNode: JQuery;
    public layout_: Layout;
    private backgroundImage: string;
    private divImage: string;
    private imageZIndex: number;
    private rowCssStyle: {};
    private content: string;
    private script: string;
    public children: Array<Widget> = new Array();

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);

        this.$thisNode = $("<div>").attr("id", this.id);
        this.$attachNode = this.$thisNode;

        // TabItem里的control
        if ((this.style & ADORE.Control) === ADORE.Control)
            this.$thisNode.css({
                position: "absolute", /*width:"100%",*/
                top: "48px",
                bottom: "0px",
                overflow: "hidden",
                // TabFolder.setLayout是空实现的，而放在签页里的Control默认就是FillLayout
                right: "0",
                left: "0",
            });

        // cardPanel样式
        if ((this.style & ADORE.CardPanel) === ADORE.CardPanel) {
            this.$thisNode.addClass("card-panel");
        }

        if ((this.style & ADORE.Scrollable) === ADORE.Scrollable) {
            this.$thisNode.css("overflow", "auto");
        }
    }

    public setFocus() {
    }

    public setLayout(args: { layout: { type: string, layoutOrientation?: number, hiddenFooter: boolean } }) {
        if (this.layout_) {
            return;
        }

        let layout = args.layout;
        let type = layout.type;

        if (type === "RowGridLayout") {
            this.layout_ = new RowGridLayout(this);
        } else if (type === "HeaderFooterLayout") {
            this.layout_ = new HeaderFooterLayout(this, layout.layoutOrientation, layout.hiddenFooter);
        } else if (type === "FillLayout") {
            this.layout_ = new FillLayout(this);
        } else if (type === "SideNavLayout") {
            this.layout_ = new SideNavLayout(this);
        }
    }

    // 注意，本方法允许被调用多次，需要控制好资源创建和销毁！！！
    public layout() {
        this.doLayout();
        if (this.content) {
            this.setContentView({ content: this.content, script: this.script });
        }
    }

    // 注意，本方法允许被调用多次，需要控制好资源创建和销毁！！！
    private doLayout() {
        //RootComposite
        if (!this.parent) {
            if (this.id.indexOf("Composite") !== -1) {
                this.$thisNode.css({
                    "height": "100vh",
                    "width": "100vw"
                });
            }
            this.$thisNode.appendTo(document.body);
        }

        if (!this.layout_) {
            return;
        }

        this.layout_.layout();

        for (let child of this.children) {
            child.postLayout();
        }
    }

    public setBackgroundImage(args: { src: string }) {
        this.backgroundImage = args.src;
        this.$thisNode.find("#" + this.id + "_img").remove(); //先删除之前的img
        $("<img>").addClass("adore_background_image").attr({
            src: this.backgroundImage,
            id: this.id + "_img"
        }).prependTo(this.$thisNode);
        if (this.imageZIndex === 0 || this.imageZIndex) {
            this.setBackgroundImageZIndex({ "imageZIndex": this.imageZIndex });
        }
    }

    public setBackgroundImageZIndex(args: { imageZIndex: number }) {
        this.imageZIndex = args.imageZIndex;
        this.$thisNode.find("#" + this.id + "_img").css("z-index", this.imageZIndex);
    }

    public setImageInDiv(args: { divImageSrc: string }) {
        this.divImage = args.divImageSrc;
        if (this.$thisNode) {
            this.$thisNode.css("background-image", "url(" + this.divImage + ")");
            this.$thisNode.css("background-repeat", "round");
        }
    }

    public setContentView(args: { content: string, script: string }) {
        this.content = args.content;
        this.script = args.script;
        if (this.$attachNode) {
            if (this.script && this.script.trim()) {
                let scriptArr = this.script.trim().split("\n");
                require(scriptArr, () => {
                    this.$attachNode.html(args.content);
                });
            } else {
                this.$attachNode.html(args.content);
            }
        }
    }


    public addChild(child: Widget) {
        this.children.push(child);
    }

    public dispose() {
        if (this.layout_) {
            this.layout_.dispose();
        }
        super.dispose();
    }


    public handleMouseDownEvent(e: MouseEvent) {
        //容器类组件不处理Mouse事件
    }

    public handleMouseUpEvent(e: MouseEvent) {
        //容器类组件不处理Mouse事件
    }

}
