<template>
    <div>
        <el-button @click="addCircle">Circle</el-button>
        <el-button @click="addRect">Rect</el-button>
        <el-button @click="deleteItem">Delete</el-button>
        <el-button @click="undo">Undo</el-button>
        <el-button @click="redo">Redo</el-button>
        <el-button @click="log">日志</el-button>
        <canvas id="undoFrame" width="800" height="400" />
    </div>
</template>

<script lang="ts">
    import { SMouseEvent, SUndoStack } from "@persagy-web/base";
    import { SColor, SPainter, SRect } from "@persagy-web/draw";
    import { SGraphItem, SGraphScene, SGraphView, SGraphAddCommand, SGraphMoveCommand } from "@persagy-web/graph";
    import {SPoint} from "@persagy-web/draw/lib";

    class RectItem extends SGraphItem {
        width = 100;
        height = 100;
        id = new Date().getTime()
        constructor(parent: SGraphItem | null) {
            super(parent);
            this.moveable = true;
            this.selectable = true;
            this.selected = true;
            this.isTransform = true;
            this.rolate = 60;
        }

        boundingRect(): SRect {
            return new SRect(0, 0, this.width, this.height);
        }

        onDraw(canvas: SPainter): void {
            canvas.pen.color = SColor.Blue;
            canvas.pen.lineWidth = 5;
            canvas.brush.color = this.selected ? SColor.Red : SColor.Green;
            canvas.drawRect(0, 0, this.width, this.height)
        }
    }

    class CircleItem extends SGraphItem {
        r = 50;
        id = new Date().getTime()

        constructor(parent: SGraphItem | null) {
            super(parent);
            this.moveable = true;
            this.selectable = true;
            this.isTransform = false;
        }

        boundingRect(): SRect {
            return new SRect(0, 0, this.r * 2, this.r * 2);
        }

        onDraw(canvas: SPainter): void {
            canvas.pen.color = SColor.Blue;
            canvas.pen.lineWidth = 5;
            canvas.brush.color = this.selected ? SColor.Red : SColor.Green;
            canvas.drawCircle(this.r, this.r, this.r);
        }
    }

    class SScene extends SGraphScene {
        undoStack = new SUndoStack();
        /** 定义命令 */
        cmd = 0;

        constructor() {
            super();
        }


        onMouseUp(event: SMouseEvent): boolean {
            switch(this.cmd) {
                case 1:
                    this.addCircle(event.x, event.y);
                    break;
                case 2:
                    this.addRect(event.x, event.y);
                    break;
                default:
                    super.onMouseUp(event);
            }
            this.cmd = 0;
            return false
        }

        private addCircle(x: number, y: number): void {
            let item = new CircleItem(null);
            item.moveTo(x - 50, y - 50);
            this.addItem(item);
            this.undoStack.push(new SGraphAddCommand(this, item));
            item.connect("onMove", this, this.onItemMove.bind(this));
        }

        private addRect(x: number, y: number): void {
            let item = new RectItem(null);
            item.moveTo(x - 50, y - 50);
            this.addItem(item);
            this.undoStack.push(new SGraphAddCommand(this, item));
            item.connect("onMove", this, this.onItemMove.bind(this));
        }

        onItemMove(item: SGraphItem, ...arg: any): void {
            this.undoStack.push(new SGraphMoveCommand(this, item, arg[0][0] as SPoint, arg[0][1] as SPoint));
        }
    }

    class TestView extends SGraphView {
        constructor() {
            super("undoFrame");
        }
    }

    export default {
        data() {
            return {
                scene: new SScene(),
            }
        },
        mounted(): void {
            let view = new TestView();
            view.scene = this.scene;//new SScene(); //this.data.scene;
        },
        methods: {
            addCircle() {
                this.scene.cmd = 1;
            },
            addRect() {
                this.scene.cmd = 2;
            },
            deleteItem() {

            },
            undo() {
                this.scene.undoStack.undo();
            },
            redo() {
                this.scene.undoStack.redo();
            },
            log() {
                let str = this.scene.undoStack.toLog();
                console.log(str)
            }
        }
    }
</script>

<style scoped>

</style>
