<script>
import Selecto from "vue-selecto";
import Moveable from "vue-moveable";
import { diff } from "@egjs/children-differ";

export default {
    components: { Moveable, Selecto },
    data() {
        const targets = [];
        const moveableRef = null;
        const selectoRef = null;
        const cubes = [];
        for (let i = 0; i < 30; ++i) {
            cubes.push(i);
        }
        return { targets, moveableRef, selectoRef, cubes };
    },
    methods: {
        onClickGroup(e) {
            this.$refs.selectoRef.clickTarget(e.inputEvent, e.inputTarget);
        },
        onDrag(e) {
            e.target.style.transform = e.transform;
        },
        onDragGroup(e) {
            e.events.forEach(ev => {
                ev.target.style.transform = ev.transform;
            });
        },
        onClick(e) {
            if (e.isDouble) {
                const inputTarget = e.inputTarget;
                const selectableElements = this.$refs.selectoRef.getSelectableElements();
                if (selectableElements.includes(inputTarget)) {
                    this.$refs.selectoRef.setSelectedTargets([inputTarget]);
                    this.targets = [inputTarget];
                }
            }
        },
        onDragStart(e) {
            const moveable = this.$refs.moveableRef;
            const target = e.inputEvent.target;
            if (moveable.isMoveableElement(target)
                || this.targets.some(t => t === target || t.contains(target))
            ) {
                e.stop();
            }
        },
        onSelectEnd(e) {
            const moveable = this.$refs.moveableRef;
            let selected = e.selected;
            selected = selected.filter(target => {
                return selected.every(target2 => {
                    return target === target2 || !target2.contains(target);
                });
            });
            const result = diff(e.startSelected, selected);
            e.currentTarget.setSelectedTargets(selected);
            if (!result.added.length && !result.removed.length) {
                return;
            }
            if (e.isDragStartEnd) {
                e.inputEvent.preventDefault();
                moveable.waitToChangeTarget().then(() => {
                    moveable.dragStart(e.inputEvent);
                });
            }
            this.targets = selected;
        }
    },
    mounted() {
        this.moveableRef = this.$refs.moveableRef;
        this.selectoRef = this.$refs.selectoRef;
    }
};
</script>
<template>
    <div class="root">
        <div class="container">
            <Moveable
                ref="moveableRef"
                :draggable="true"
                :target="targets"
                @clickGroup="onClickGroup"
                @drag="onDrag"
                @dragGroup="onDragGroup"
                @click="onClick"
            ></Moveable>
            <Selecto
                ref="selectoRef"
                :dragContainer="window"
                :selectableTargets="['.selecto-area .cube']"
                :hitRate="0"
                :selectByClick="true"
                :selectFromInside="false"
                :toggleContinueSelect="['shift']"
                :ratio="0"
                @dragStart="onDragStart"
                @selectEnd="onSelectEnd"
            ></Selecto>
            <div class="elements selecto-area">{{cubes.map(i => {
                    if (i === 2) {
                        return <div class="cube" key={i} style="width: 100px;height: 50px;">
                                            {i}
                                            <div class="cube" key={i}>{i}</div>
                                        </div>;
                    }
                    return <div class="cube" key={i}>{i}</div>;
                })}}
            </div>
            <div class="empty elements"></div>
        </div>
    </div>
</template>