<template>
    <teleport to=".scene-designer .lines" v-if="active">
        <OffsetLine v-for="(t, i) in modelValue.activationTriggers" :x1="vd.x1" :y1="vd.y1" 
            :x2="parent.data.items.find(i=>i.data.id == t)?.vd.x1"
            :y2="parent.data.items.find(i=>i.data.id == t)?.vd.y1" :o1="52" :o2="52"
            class="trigger" marker-start="url(#arrow)" ></OffsetLine>
    </teleport>
    <teleport to=".scene-designer .game-objects" v-if="active">
        <g @mousedown="$emit('target', {target:vd, attrs:['x1', 'y1'], delta: true})" :class="{gameObject: true, selected}" 
            v-show="showInView">
            <line :x1="vd.x1" :y1="vd.y1" :x2="parent.vd.x1" :y2="parent.vd.y1"></line>
            <svg-icon :src="`/asset/thumb/${modelValue.go||0}.webp`" :x="vd.x1" :y="vd.y1" :size="37"></svg-icon>
            <text :x="vd.x1" :y="vd.y1" dy="58">{{ modelValue.title }}</text>
            <image v-if="modelValue.go" href="/static/svg/play-circle.svg" width="20" class="cursor-pointer"
                :x="vd.x1 + 37 - 10" :y="vd.y1 + 37 - 10" @click="$emit('preview', modelValue.go)">
            </image>
        </g>
    </teleport>
    <v-card v-if="selected" :title="modelValue.title" class="mx-2" variant="text">
        <asset-selector @select="assignGameObject" :type="['GameObject']">
            <template v-slot:activator="props">
                <v-btn v-bind="props" prepend-icon="mdi-panorama-outline" color="success" block>Choose game object</v-btn>
            </template>
        </asset-selector>
        <v-form class="pt-4">
            <v-text-field density="compact" :label="l.name" v-model="modelValue.title"></v-text-field>
            <!-- <v-text-field density="compact" :label="l.id" v-model="modelValue.id"></v-text-field> -->
            <v-number-input density="compact" label="Completion points" v-model="modelValue.points"></v-number-input>
        </v-form>
        <v-container v-if="selected && modelValue.type">
            <component :is="modelValue.type" v-model="mv"></component>
        </v-container>

        <v-card title="Activation requirements" v-if="modelValue.type != 'CharacterObject'">
            <v-number-input density="compact" label="Level score should be above" v-model="modelValue.activationScore"></v-number-input>
            <v-select density="compact" label="Following elements should be completed"  v-model="modelValue.activationTriggers"
                :items="parent.data.items.filter(v=>!v.data.exclude && v.data!==modelValue).map(v=>({title: v.data.title, value: v.data.id}))" multiple ></v-select>
        </v-card>
    </v-card>
    
    
</template>

<script>
import SvgIcon from './SvgIcon.vue';

import VideoPlayer from '../InteractiveObjects/VideoPlayer.vue';
import PuzzleGame1 from '../InteractiveObjects/PuzzleGame1.vue';
import PuzzleGame2 from '../InteractiveObjects/PuzzleGame2.vue';
import MazeQuizGame from '../InteractiveObjects/MazeQuizGame/MazeQuizGame.vue';
import ClassicPuzzle from '../InteractiveObjects/ClassicPuzzle.vue';
import Particles from '../InteractiveObjects/Particles.vue';
import GenericObject from '../InteractiveObjects/GenericObject.vue';
import CharacterObject from '../InteractiveObjects/CharacterObject.vue';
import OffsetLine from './OffsetLine.vue';

export default {
    emits:['target', 'preview'],
    components: { 
        SvgIcon, OffsetLine, GenericObject, CharacterObject, VideoPlayer, 
        PuzzleGame1, PuzzleGame2, MazeQuizGame, Particles, ClassicPuzzle
    },
    data(){
        return {
            active: false
        }
    },
    mounted(){
        this.active = true;
        this.modelValue.points ??= 10;
        this.modelValue.activationScore ??= 0;
    },
    props:{
        //context: Object,
        modelValue: Object,
        vd: Object,
        selected: Boolean, 
        cid:String, 
        visible: Boolean,
        parent: Object
    },
    computed:{
        showInView(){
            this.vd.__showInView = this.visible && this.parent.visible;
            return this.vd.__showInView;
        },
        mv(){
            return this.modelValue
        }
    },
    steps: [['x1', 'y1']],
    name: 'game-object',
    modifiers: ['x1', 'y1'],
    methods:{
        intersect(v){
            return Utils.intersectPointRect([this.vd.x1, this.vd.y1], v);
        },
        assignGameObject(e){
            this.modelValue.go = e.id;
            if (this.modelValue.id == this.modelValue.title){
                this.modelValue.title = e.name
            }
            if (e.type == 'InteractiveObject'){
                this.modelValue.type = e.id;
            }else{
                this.modelValue.type = 'GenericObject'
            }
        }
    }
}
</script>