/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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";
import {WidgetUtil} from "util/WidgetUtil";
/**
 * Created by huang.dy on 2016/6/28.
 */
export class AdoreShell extends Composite {

    private static TemplateNormal = `<div class="wrap_modal"><iframe class="myclass"></iframe>
    <div>
        <div class="modal modal-fixed-footer adore_shell">
            <div class="adore_shell_title">
                <h4 class="adore_shell_title_content"></h4>
            </div>
            <div class="modal-content adore_shell_content"></div>
        </div></div></div>`;

    private static TemplateOverlay = `<div class="adore_lean_overlay"></div>`;

    private static TemplateClose = `<div class="adore_shell_exit_button waves-effect waves-light large mdi-content-clear"></div>`;

    private originWidth: any;
    private originHeight: any;
    private resizeListener: any;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(AdoreShell.TemplateNormal).attr("id", this.id);
        this.$attachNode = this.$thisNode.find("div.modal-content");

        if ((this.style & ADORE.Close) === ADORE.Close) {
            let own = this;
            let $cancleIconNode: JQuery = $(AdoreShell.TemplateClose);
            $cancleIconNode.appendTo(this.$thisNode.find("div.adore_shell_title"));
            $cancleIconNode.bind("click", function () {
                EventHub.dispatchEvent(MessageType.WIDGET, { "id": own.id, evtType: Event.ON_CLOSE });
            });
        }
    }

    public setTitle(args: { title: string }) {
        let text = args.title;
        this.$thisNode.find("h4")[0].innerHTML = text;
    }

    public show() {
        this.$thisNode.find(".adore_shell").css({
            display: "block",
            opacity: 0,
            scaleX: "0.7"
        });
        this.$thisNode.find(".adore_shell").velocity({ opacity: 1, scaleX: "1" }, {
            duration: 320,
            queue: false,
            ease: "easeOutCubic"
        });
        this.$thisNode.find(".myclass").css({
            "width": this.$thisNode.find(".adore_shell").width(),
            "height": this.$thisNode.find(".adore_shell").height(),
            "left": this.$thisNode.find(".adore_shell").offset().left,
            "top" : this.$thisNode.find(".adore_shell").offset().top
        });
        if (this.cssStyle) {
            this.$thisNode.find(".adore_shell").css(this.cssStyle);
            this.cssStyle = null;
        }
        $(AdoreShell.TemplateOverlay).attr("id", this.id + "_overlay").appendTo(document.body);
        WidgetUtil.refreshDialogZIndex(this.$thisNode.find(".adore_shell")[0]);
        let width: any = this.$thisNode.find(".adore_shell").css("width");
        let height: any = this.$thisNode.find(".adore_shell").css("height");
        if ((width && width.indexOf("%") === -1) || (height && height.indexOf("%") === -1)) {
            this.originWidth = width;
            this.originHeight = height;
            this.refreshSize();
            let own = this;
            this.resizeListener = function (e: BaseJQueryEventObject) {
                own.refreshSize();
            };
            $(window).on("resize", this.resizeListener);
        }
        if (height && height.indexOf("%") !== -1) {
            height = height.replace(/%/, "");
            if (height >= 90) {
                this.$thisNode.find(".adore_shell").addClass("adore_shell_max_height");
            }
        }

    }

    private refreshSize() {
        let css = {};
        let totalW = window.innerWidth;
        let totalH = window.innerHeight;
        if (this.originWidth && this.originWidth.indexOf("%") === -1) {
            let width = this.originWidth.replace(/px/g, "");
            if (width >= totalW) {
                css = $.extend(css, { width: "100%" });
            } else {
                css = $.extend(css, { width: (width / (totalW * 0.01)) + "%" });
            }
        }
        if (this.originHeight && this.originHeight.indexOf("%") === -1) {
            let height = this.originHeight.replace(/px/g, "");
            let hProportion = 0;
            if (height >= totalH) {
                hProportion = 100;
                css = $.extend(css, { height: "100%" });
            } else {
                hProportion = height / (totalH * 0.01);
                css = $.extend(css, {
                    height: hProportion + "%"
                });
            }
            if (hProportion >= 90) {
                this.$thisNode.find(".adore_shell").addClass("adore_shell_max_height");
            } else {
                this.$thisNode.find(".adore_shell").removeClass("adore_shell_max_height");
            }
        }
        this.$thisNode.find(".adore_shell").css(css);
        this.$thisNode.find(".myclass").css({
            "width": this.$thisNode.find(".adore_shell").width(),
            "height": this.$thisNode.find(".adore_shell").height(),
            "left": this.$thisNode.find(".adore_shell").offset().left,
            "top" : this.$thisNode.find(".adore_shell").offset().top
        });
    }


    public dispose() {
        $("#" + this.id + "_overlay").remove();
        if (this.resizeListener) {
            $(window).off("resize", this.resizeListener);
        }
        super.dispose();
    }

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

}