<script>
import { deepFlat } from "@daybrush/utils";
import Selecto from "vue-selecto";
import Moveable, { MoveableTargetGroupsType } from "vue-moveable";
import { GroupManager, TargetList } from "@moveable/helper";

export default {
    components: { Moveable, Selecto },
    data() {
        const groupManager = new GroupManager([]);
        const targets = [];
        const moveableRef = null;
        const selectoRef = null;
        const cubes = [];
        for (let i = 0; i < 30; ++i) {
            cubes.push(i);
        }
        return { groupManager, targets, moveableRef, selectoRef, cubes };
    },
    methods: {
        setSelectedTargets(nextTargetes) {
            this.$refs.selectoRef.setSelectedTargets(deepFlat(nextTargetes));
            this.targets = nextTargetes;
        },
        onClick() {
            const nextGroup = this.groupManager.group(this.targets, true);
            if (nextGroup) {
                this.targets = nextGroup;
            }
        },
        onClick$0() {
            const nextGroup = this.groupManager.ungroup(this.targets);
            if (nextGroup) {
                this.targets = nextGroup;
            }
        },
        onDrag(e) {
            e.target.style.transform = e.transform;
        },
        onRenderGroup(e) {
            e.events.forEach(ev => {
                ev.target.style.cssText += ev.cssText;
            });
        },
        onClickGroup(e) {
            if (!e.moveableTarget) {
                this.setSelectedTargets([]);
                return;
            }
            if (e.isDouble) {
                const childs = this.groupManager.selectSubChilds(
                    this.targets,
                    e.moveableTarget
                );
                this.setSelectedTargets(childs.targets());
                return;
            }
            if (e.isTrusted) {
                this.$refs.selectoRef.clickTarget(
                    e.inputEvent,
                    e.moveableTarget
                );
            }
        },
        onDragStart(e) {
            const moveable = this.$refs.moveableRef;
            const target = e.inputEvent.target;
            const flatted = deepFlat(this.targets);
            if (target.tagName === "BUTTON" || moveable.isMoveableElement(target)
                || flatted.some(t => t === target || t.contains(target))
            ) {
                e.stop();
            }
            e.data.startTargets = this.targets;
        },
        onSelect(e) {
            const { startAdded, startRemoved, isDragStartEnd } = e;
            if (isDragStartEnd) {
                return;
            }
            const nextChilds = this.groupManager.selectSameDepthChilds(
                e.data.startTargets,
                startAdded,
                startRemoved
            );
            this.setSelectedTargets(nextChilds.targets());
        },
        onSelectEnd(e) {
            const {
                isDragStartEnd,
                isClick,
                added,
                removed,
                inputEvent,
            } = e;
            const moveable = this.$refs.moveableRef;
            if (isDragStartEnd) {
                inputEvent.preventDefault();
                moveable.waitToChangeTarget().then(() => {
                    moveable.dragStart(inputEvent);
                });
            }
            let nextChilds;
            if (isDragStartEnd || isClick) {
                nextChilds = this.groupManager.selectCompletedChilds(
                    e.data.startTargets,
                    added,
                    removed
                );
            } else {
                nextChilds = this.groupManager.selectSameDepthChilds(
                    e.data.startTargets,
                    added,
                    removed
                );
            }
            e.currentTarget.setSelectedTargets(nextChilds.flatten());
            this.setSelectedTargets(nextChilds.targets());
        }
    },
    mounted() {
        const elements = this.$refs.selectoRef.getSelectableElements();
        this.groupManager.set([], elements);
        this.moveableRef = this.$refs.moveableRef;
        this.selectoRef = this.$refs.selectoRef;
    }
};
</script>
<template>
    <div class="root">
        <div class="container">
            <button @click="onClick">Group</button>
            &nbsp;
            <button @click="onClick$0">Ungroup</button>
            <Moveable
                ref="moveableRef"
                :draggable="true"
                :rotatable="true"
                :scalable="true"
                :target="targets"
                @drag="onDrag"
                @renderGroup="onRenderGroup"
                @clickGroup="onClickGroup"
            ></Moveable>
            <Selecto
                ref="selectoRef"
                :dragContainer="window"
                :selectableTargets="['.selecto-area .cube']"
                :hitRate="0"
                :selectByClick="true"
                :selectFromInside="false"
                :toggleContinueSelect="['shift']"
                :ratio="0"
                @dragStart="onDragStart"
                @select="onSelect"
                @selectEnd="onSelectEnd"
            ></Selecto>
            <div class="elements selecto-area">
                <div
                    class="cube"
                    :key="i"
                    v-for="i in cubes"
                >{{i}}</div>
            </div>
            <div class="empty elements"></div>
        </div>
    </div>
</template>