import Tagify from '@yaireo/tagify';
import '@yaireo/tagify/dist/tagify.css';

const taginput = document.querySelector("#tags");
const studioinput = document.querySelector("#studio");

// Get Tags from API
window.axios.get('/admin/tags').then(function (response) {
    if (response.status != 200) {
        return;
    }

    new Tagify(taginput, {
        whitelist: response.data.tags,
        dropdown: {
            classname: "color-blue",
            enabled: 0,              // show the dropdown immediately on focus
            maxItems: 10,
            position: "text",         // place the dropdown near the typed text
            closeOnSelect: false,          // keep the dropdown open after selecting a suggestion
            highlightFirst: true
        }
    });
}).catch(function (error) {
    console.log(error);
});

// Get Studios from API
window.axios.get('/admin/studios').then(function (response) {
    if (response.status != 200) {
        return;
    }

    new Tagify(studioinput, {
        whitelist: response.data.studios,
        dropdown: {
            classname: "color-blue",
            enabled: 0,              // show the dropdown immediately on focus
            maxItems: 10,
            position: "text",         // place the dropdown near the typed text
            closeOnSelect: false,          // keep the dropdown open after selecting a suggestion
            highlightFirst: true
        }
    });
}).catch(function (error) {
    console.log(error);
});

let eps = 1;

function dynEpisode() {
    let amount = this.value;

    if (amount > eps) {
        eps += 1;
        var episodeUploads = `
        <div class="grid grid-cols-2" id="dynU` + eps + `">
            <div class="p-4">
                <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="episodecover` + eps + `">Cover ` + eps + `:</label>
                <input class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-rose-800 focus:border-rose-900 dark:bg-neutral-900 dark:border-neutral-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-rose-800 dark:focus:border-rose-900" type="file" name="episodecover` + eps + `" id="episodecover` + eps + `" required>
            </div>
            <div class="p-4">
                <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="episodegallery` + eps + `">Gallery ` + eps + `:</label>
                <input class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-rose-800 focus:border-rose-900 dark:bg-neutral-900 dark:border-neutral-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-rose-800 dark:focus:border-rose-900" type="file" name="episodegallery` + eps + `[]" id="episodegallery` + eps + `" multiple="">
            </div>
        </div>

        <div class="p-4 pt-0" id="dynB` + eps + `">
            <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="description` + eps + `">Description ` + eps + `:</label>
            <textarea rows="4" cols="50" id="description` + eps + `" name="description` + eps + `" class="mt-1 block w-full border-gray-300 dark:border-gray-700 dark:bg-neutral-900 dark:text-gray-300 focus:border-rose-500 dark:focus:border-rose-600 focus:ring-rose-500 dark:focus:ring-rose-600 rounded-md shadow-sm" required>
            </textarea>
        </div>

        <div class="p-4 pt-0" id="dynD` + eps + `">
            <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="episodedlurl` + eps + `">Download 1080p ` + eps + `:</label>
            <input class="border-gray-300 dark:border-gray-700 dark:bg-neutral-900 dark:text-gray-300 focus:border-rose-500 dark:focus:border-rose-600 focus:ring-rose-500 dark:focus:ring-rose-600 rounded-md shadow-sm block w-full" id="episodedlurl` + eps + `" type="text" name="episodedlurl` + eps + `" required="required">
        </div>

        <div class="p-4 pt-0" id="dynD48fps` + eps + `">
            <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="episodedlurlinterpolated` + eps + `">Download 1080p48fps ` + eps + `:</label>
            <input class="border-gray-300 dark:border-gray-700 dark:bg-neutral-900 dark:text-gray-300 focus:border-rose-500 dark:focus:border-rose-600 focus:ring-rose-500 dark:focus:ring-rose-600 rounded-md shadow-sm block w-full" id="episodedlurlinterpolated` + eps + `" type="text" name="episodedlurlinterpolated` + eps + `">
        </div>

        <div class="p-4 pt-0" id="dynD4k` + eps + `">
            <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="episodedlurl4k` + eps + `">Download 4k ` + eps + `:</label>
            <input class="border-gray-300 dark:border-gray-700 dark:bg-neutral-900 dark:text-gray-300 focus:border-rose-500 dark:focus:border-rose-600 focus:ring-rose-500 dark:focus:ring-rose-600 rounded-md shadow-sm block w-full" id="episodedlurl4k` + eps + `" type="text" name="episodedlurl4k` + eps + `" required="required">
        </div>

        <div class="p-4 pt-0" id="dynDUHD48fps` + eps + `">
            <label class="leading-tight text-gray-800 dark:text-gray-200 w-full" for="downloadUHDi` + eps + `">Download 4k 48fps ` + eps + `:</label>
            <input class="border-gray-300 dark:border-gray-700 dark:bg-neutral-900 dark:text-gray-300 focus:border-rose-500 dark:focus:border-rose-600 focus:ring-rose-500 dark:focus:ring-rose-600 rounded-md shadow-sm block w-full" id="downloadUHDi` + eps + `" type="text" name="downloadUHDi` + eps + `">
        </div>
        `;

        var element = document.getElementById('moreEpisodes');
        element.innerHTML = element.innerHTML + episodeUploads;
    } else if (amount < eps) {
        if (amount == 0) {
            this.value = 1;
            return;
        }
        document.getElementById("dynU" + eps).remove();
        document.getElementById("dynD" + eps).remove();
        document.getElementById("dynD4k" + eps).remove();
        document.getElementById("dynD48fps" + eps).remove();
        document.getElementById("dynDUHD48fps" + eps).remove();
        document.getElementById("dynB" + eps).remove();
        eps -= 1;
    }
}


document.getElementById("episodes").addEventListener('change', dynEpisode);