<template>
<div class="main-page-root">
    <edit-page v-if="showEditor" class="image-editor" :image="image" @closeRequested="onEditorCloseRequested" />

    <div class="paper root-container" :class="{ 'root-container-image-state': image !== null }">
        <div v-if="image === null" class="upload-area" @click="uploadFile">
            <span class="heading1">{{ translations.get("Title") }}</span>
            <span class="material-icons mdi-xl">upload_file</span>
            <span>Upload an image/PDF</span>
        </div>

        <div v-if="image !== null" class="image-area">
            <div class="controls">
                <button class="btn-primary" @click="doOcr" :disabled="ocrInProgress">
                    <span class="material-icons">play_arrow</span>
                    <span>{{ translations.get("Main_PerformOCR") }}</span>
                </button>

                <button class="btn-primary" @click="showEditor = true">
                    <span class="material-icons">edit</span>
                    <span>Edit Image</span>
                </button>

                <button class="btn-primary" @click="reset" :disabled="ocrInProgress">
                    <span class="material-icons">restart_alt</span>
                    <span>New</span>
                </button>

                <div v-if="ocrInProgress" class="progress-bar-container">
                    <div class="progress-bar-value progress-bar-indeterminate" />
                </div>
            </div>

            <img class="image-display" :src="imageUrl" />

            <div class="text-container">
                <pre>{{ ocrResult }}</pre>
            </div>
        </div>
    </div>

    <input ref="fileInput" type="file" @input="onFilesChanged" class="hidden"
        accept="image/png,image/jpeg,image/gif,image/bmp,image/tiff,image/webp,application/pdf" />
</div>

</template>

<style lang="scss" scoped>
.main-page-root {
    display: flex;
    align-items: center;
    justify-content: center;
}

.root-container {
    padding: 1em;
    transition-duration: var(--transition-duration);
}

.root-container-image-state {
    width: calc(100% - 3em);
    height: calc(100% - 3em);
}

.upload-area {
    display: grid;
    grid-template-columns: auto auto;
    grid-template-rows: auto 1fr;
    align-items: center;
    justify-items: center;

    padding: 2em;
    gap: 1em;

    font-size: 2rem;
    cursor: pointer;
    border: 3px dashed var(--bg-color);
    border-radius: var(--border-radius);

    .heading1 {
        grid-column-start: span 2;
        color: #666;
    }
}

.hidden {
    display: none;
}

.image-editor {
    position: absolute;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    z-index: 9;
}

.image-area {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto 1fr;
    height: 100%;

    gap: 1rem;
    align-items: center;
    justify-items: center;

    position: relative;
    overflow: hidden;

    .text-container {
        height: 100%;
        width: 100%;
    }
}

.image-display {
    border: 1px solid #444;
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
}

.controls {
    display: flex;
    gap: 1em;
    grid-column-start: span 2;
}
</style>

<script>
import { mapState } from "vuex";
import EditPage from "./EditPage.vue";
import OcrService, { OcrOptions } from "../js/OcrService"
import { log } from "../js/Util";

const ocrService = new OcrService();

export default {
    name: "MainPage",
    components: {
        EditPage
    },
    data() {
        return {
            /** @type {File} */
            image: null,
            imageUrl: null,
            ocrInProgress: false,
            ocrResult: null,
            showEditor: false
        }
    },
    computed: {
        ...mapState({
            translations: state => state.translations
        })
    },
    methods: {
        uploadFile() {
            this.$refs.fileInput.click();
        },
        async onFilesChanged() {
            /** @type {File[]} */
            const files = this.$refs.fileInput.files;
            if (files === null || files === undefined || files.length !== 1) {
                return;
            }

            this.image = files[0];
            this.imageUrl = URL.createObjectURL(this.image);
        },
        async doOcr() {
            try {
                this.ocrInProgress = true;

                const imageBlob = await this.$refs.imageEditor.getImageBlobAsync();

                const result = await ocrService.run([
                    {
                        image: imageBlob,
                        options: new OcrOptions(false)
                    }
                ]);

                this.ocrResult = result[0].text;
            }
            catch (ex) {
                // TODO: Display error
                log("Failed to perform OCR", "error");
                log(ex, "error");
            }
            finally {
                this.ocrInProgress = false;
            }
        },
        onEditorCloseRequested() {
            this.showEditor = false;
        },
        reset() {
            this.image = null;
            URL.revokeObjectURL(this.imageUrl);
            this.imageUrl = null;
            this.ocrInProgress = false;
            this.ocrResult = null;
            this.showEditor = false;
            this.$refs.fileInput.value = "";
        }
    }
}
</script>
