<template>
    <f7-page name="detect">
      <!-- Top Navbar -->
      <f7-navbar :sliding="false" back-link="Back">
        <f7-nav-title sliding>{{ regions[activeRegion] }}</f7-nav-title>
      </f7-navbar>
      <f7-block style="display: flex; justify-content: flex-start; flex-direction: column; align-items: center; height: calc(100% - var(--f7-navbar-height) - var(--f7-safe-area-top));">
        <div class="image-box" style="display: flex; flex-direction: column; align-items: center; flex: 1 1 0%;" >
            <img :src="imageView" ref="image_src" style="min-width: 0; min-height: 0; flex: 1 1 0"/>
            <div ref="structure_box" style="border: solid 3px yellow; position: absolute; display: none;" />
        </div>
        <f7-segmented class="image-menu" raised style="margin: 5px; width: 50%; max-width: 400px; min-width: 192px; flex: 0 0 auto;">
            <f7-button popover-open="#region-popover">
                <img :src="imageRegion" style="height: 100%;" />
            </f7-button>
            <f7-button @click="setData" :class="(imageLoaded) ? '' : 'disabled'">
                <img src="../assets/icons/visibility.svg" style="height: 100%;" />
            </f7-button>
            <f7-button @click="selectImage">
                <img src="../assets/icons/image.svg" style="height: 100%;" />
            </f7-button>
            <f7-button @click="setData">
                <img src="../assets/icons/videocam.svg" style="height: 100%;" />
            </f7-button>
        </f7-segmented>
        <input type="file" ref="image_chooser" @change="getImage()" accept="image/png, image/jpg, image/jpeg" style="display: none;"/>
        <div v-if="resultData && resultData.detections" class="chip-results" style="flex: 0 0 auto;">
            <f7-chip v-for="(result, idx) in resultData.detections" :class="(idx == selectedChip) ? 'selected-chip' : ''" :text="result.label" media=" " :tooltip="result.confidence.toFixed(1)" :media-bg-color="chipColor(result.confidence)" deleteable @click="selectChip(idx)" @delete="deleteChip(idx)" />
        </div>
      </f7-block>
    </f7-page>
</template>

<style>
    .chip-results {
        display: flex;
        flex-wrap: wrap;
        gap: 5px;
        --f7-chip-border-radius: 16px;
        --f7-chip-media-size: 32px;
        --f7-chip-font-weight: normal;
    }

    .chip-results .chip {
        padding-left: 8px;
    }

    .selected-chip {
        font-weight: 500;
        box-shadow: 4px 4px 1px var(--f7-theme-color);
        transform: translate(-2px, -2px);
    }

    .image-menu .button {
        aspect-ratio: 1;
        height: auto;
        padding: 5px;
    }
</style>

<script>
    import { f7 } from 'framework7-vue'
    import fakeData from './testData.js'

    export default {
        props: {
            f7route: Object,
        },
        data () {
            return {
            regions: ['Thorax','Abdomen/Pelvis','Limbs','Head and Neck'],
            resultData: {},
            selectedChip: -1,
            activeRegion: 4,
            imageRegion: '',
            imageLoaded: false,
            imageView: '../assets/icons/image.svg',
            }
        },
        created () {
            switch (this.f7route.params.region) {
                case 'thorax':
                this.activeRegion = 0
                this.imageRegion = '../assets/regions/thorax.svg'
                    break;
                case 'abdomen':
                this.activeRegion = 1
                this.imageRegion = '../assets/regions/abdpel.svg'
                    break;
                case 'limbs':
                this.activeRegion = 2
                this.imageRegion = '../assets/regions/limb.svg'
                    break;
                case 'head':
                this.activeRegion = 3
                this.imageRegion = '../assets/regions/headneck.svg'
                    break;
            }
        },
        methods: {
            chipColor (confVal) {
                if (confVal >= 90) return 'green'
                if (confVal >= 70) return 'lime'
                return 'yellow';
            },
            setData () {
                this.resultData = fakeData.testData
            },
            selectImage () {
                this.$refs.image_chooser.click()
                //TODO This really needs to be a promise and resolve system
                this.imageLoaded = true;
                var box = this.$refs.structure_box
            },
            selectChip ( iChip ) {
                this.selectedChip = iChip
                var box = this.$refs.structure_box
                var img = this.$refs.image_src
                var imgWidth = img.offsetWidth
                var imgHeight = img.offsetHeight
                box.style.display = "block"
                box.style.left = `calc( 50% - ${imgWidth/2}px + ${this.resultData.detections[iChip].left * imgWidth}px)`
                box.style.top = `${this.resultData.detections[iChip].top * imgHeight}px`
                box.style.width = `${(this.resultData.detections[iChip].right - this.resultData.detections[iChip].left) * imgWidth}px`
                box.style.height = `${(this.resultData.detections[iChip].bottom - this.resultData.detections[iChip].top) * imgHeight}px`
            },
            deleteChip ( iChip ) {
                f7.dialog.confirm(`${this.resultData.detections[iChip].label} is identified with ${this.resultData.detections[iChip].confidence.toFixed(1)}% confidence. Are you sure you want to delete it?`, () => {
                    this.resultData.detections.splice(iChip, 1)
                });
            },
            getImage () {
                var example = this.$refs.image_chooser.files[0];
                this.imageView = URL.createObjectURL(example);
            }
        }
    }
</script>