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

export class RowGridLayout extends Layout {
    private $rowDivs: Array<JQuery>;

    public constructor(composite: Composite) {
        super(composite);
    }

    public toString(): string {
        return "RowGridLayout";
    }

    public layout() {
        let rowCount = 0;

        for (let child of this.composite.children) {
            let layoutData = child.layoutData;
            if (layoutData instanceof RowGridLayoutData) {
                if (layoutData.row > rowCount) {
                    rowCount = layoutData.row;
                }
            }
        }

        let $curRowDivs: Array<JQuery> = [];
        for (let i = 0; i <= rowCount; i++) {
            let rowDiv = $(`<div class='row'></div>`);
            rowDiv.css("position", "relative");
            $curRowDivs.push(rowDiv);
        }
        $curRowDivs[rowCount].css("marginBottom", "0px");

        for (let child of this.composite.children) {
            let layoutData = child.layoutData;
            if (layoutData instanceof RowGridLayoutData) {
                /** 
                 *由于FloatPanelComposite必须放在body根节点，所以使用自己的append(先暂时如此处理) 
                 * modified by huangdeyun
                 */
                if (/*child instanceof FloatPanelComposite*/child.getId().indexOf("FloatPanelComposite") !== -1) {
                    child.append();
                    continue;
                }
                let column = layoutData.getColumn();
                let offset = layoutData.getOffset();
                let row = layoutData.row;
                let style = column + " col " + offset;
                child.$thisNode.addClass(style);
                //保证孩子节点的position设置成relative/absolute时不会跑到外边去
                if (child instanceof Composite) {
                    child.$thisNode.css({
                        position: "relative"
                    });
                }
                child.$thisNode.appendTo($curRowDivs[row]);
            }
        }

        if (this.$rowDivs) {
            for (let $rowDiv of this.$rowDivs) {
                $rowDiv.remove();
            }
        }

        this.$rowDivs = $curRowDivs;

        for (let i = 0; i <= rowCount; i++) {
            this.$rowDivs[i].appendTo(this.composite.$attachNode);
        }
    }
}