import { SGraphEditScene } from "../edit"
import { SMouseEvent, SUndoStack } from "@persagy-web/base/lib";
import { SPoint, SFont, SColor, SRect } from '@persagy-web/draw/lib';
import { SRectSelectItem } from '@persagy-web/big/lib';
import { SGraphItem, SGraphSelectContainer } from "@persagy-web/graph/";
import { uuid } from "./until";
export class SBaseEditScene extends SGraphEditScene {

    /**图例节点 */
    Nodes: any = [];  // 图例节点，所有与工程信息化相关的图例（图标类型与区域）
    /**图例节点 */  // 与工程信息无关的标识对象（增加文本注释，图上的图片说明）
    Markers: any = [];
    /**  管线对象 */
    Relations: any = [];

    copyString: any[] = []

    constructor() {
        super()
    }

    /**
     * 重做
     *
     * @return any
     */
    redo(): void {
        if (this.grabItem && this.grabItem.redo) {
            this.grabItem.redo()
        } else {
            this.undoStack.redo();
        }
    }

    /**
     * 撤销
     *
     * @return any
     */
    undo(): void {
        if (this.grabItem && this.grabItem.undo) {
            this.grabItem.undo()
        } else {
            this.undoStack.undo();
        }
    }

    /**
     * 删除
     *
     * @return item[]
     */
    deleteItem(): any {
        if (this.selectContainer.count == 0) {
            return []
        }
        let itemList = this.selectContainer.itemList;
        itemList.forEach((element: any) => {
            this.removeItem(element)
        });
        if (this.view) {
            this.view.update()
        }
        return itemList
    }

    /**
     * 框选
     *
     */
    addRectSelect(event: SMouseEvent): void {
        let point = new SPoint(event.x, event.y);
        let rect = new SRectSelectItem(null, point);
        this.addItem(rect);
        this.grabItem = rect;
    }

    /**
     * 计算框选交集
     */
    groupSelect(ctrl: boolean) {
        // if (!ctrl) {
        //     this.selectContainer.clear()
        // }
        if (this.grabItem instanceof SRectSelectItem) {
            const rect = this.grabItem.boundingRect();
            this.arrToSelect(this.root.children, rect)
        }
    }

    /**
     * 选中item:框选
     */
    private arrToSelect(arr: SGraphItem[], rect: SRect) {
        if (Array.isArray(arr) && arr.length) {
            arr.forEach(t => {
                if (!(t instanceof SGraphSelectContainer) && t.parent) {
                    let temp = t.boundingRect();
                    let lefttop = t.mapToScene(temp.left, temp.top)
                    let rightbottom = t.mapToScene(temp.right, temp.bottom)
                    let r = new SRect(lefttop, rightbottom)
                    if (rect.isIn(r)) {
                        this.selectContainer.toggleItem(t)
                    }
                }
            })
        }
    }

    /////////////////////////////////////////////////////////////////////
    // 鼠标事件

    /**
     * 鼠标左键按下
     *
     * @param   event   鼠标事件参数
     */
    onMouseDown(event: SMouseEvent): any {
        if (!super.onMouseDown(event) && 1 == event.buttons) {
            this.addRectSelect(event);
        }
    }

    /**
     * 鼠标抬起
     *
     * @param   event   鼠标事件参数
     */
    onMouseUp(event: SMouseEvent): boolean {
        if (this.grabItem) {
            // 鼠标抬起时，如果grabItem为框选则删除框选item
            if (this.grabItem instanceof SRectSelectItem) {
                this.removeItem(this.grabItem);
                this.groupSelect(false);
                this.grabItem = null;

                if (this.view) {
                    this.view.update()
                }
                return true;
            }
            return this.grabItem.onMouseUp(event);
        }
        return super.onMouseUp(event)
    }

    /**
     * 复制
     *
     */
    copy() {
        const itemList = this.selectContainer.itemList
        if (itemList.length) {
            itemList.forEach(t => {
                const data = JSON.parse(JSON.stringify(t.toData()))
                data.ID = uuid()
                this.copyString.push(data)

            })
            // 生成复制字符串
            console.log(this.copyString)
            // 获取input dom
            const input = document.createElement('input');
            input.setAttribute('id', 'COPYINPUT')
            input.value = JSON.stringify(this.copyString)
            sessionStorage.setItem("copyString", input.value);
            document.body.appendChild(input);
            input.select()
            document.execCommand('copy');
            input.style.display = 'none';
            console.log(input.value, Date.now());
            document.body.removeChild(input)
        }
    }

    /**
     * 粘贴
     *
     */
    paste() {
        const copyList = JSON.parse(JSON.stringify(this.copyString));
        copyList.forEach((t:any) => {
            if (this.view) {
                t.Pos.X += 10,
                    t.Pos.Y += 10
            }
            t.moveable = true;
            console.log('t', t)
            this.addItem(t)
        });
        this.view ? this.view.update() : ''
        console.log(this.view)
    }
}