import { Controller } from '@hotwired/stimulus';

/*
* The following line makes this controller "lazy": it won't be downloaded until needed
* See https://github.com/symfony/stimulus-bridge#lazy-controllers
*/
/* stimulusFetch: 'lazy' */
export default class extends Controller {
    H264_CODECS = ['h264', 'h.264', 'x264']
    H265_CODECS = ['h265', 'h.265', 'x265', 'hevc']

    static values = {
        title: String,
        tmdbId: String,
        imdbId: String,
        season: String,
        episode: String,
        active: Boolean,
    };

    static targets = ['list', 'count', 'episodeSelector', 'toggleButton', 'listContainer']
    static outlets = ['loading-icon']

    options = []
    optionsLoaded = false
    isOpen = false

    async connect() {
        await this.setOptions();
    }

    async setOptions() {
        if (this.optionsLoaded === false) {
            this.optionsLoaded = true;
            let response;

            try {
                response = await fetch(`/torrentio/tvshows/${this.tmdbIdValue}/${this.imdbIdValue}/${this.seasonValue}/${this.episodeValue}`)
            } catch (error) {
                console.log('There was an error', error);
            }

            if (response?.ok) {
                response = await response.text()
                this.listContainerTarget.innerHTML = response;
                this.options = this.element.querySelectorAll('tbody tr');
                if (this.options.length > 0) {
                    this.options.forEach((option) => option.querySelector('.download-btn').dataset['title'] = this.titleValue);
                    this.options[0].querySelector('input[type="checkbox"]').checked = true;
                } else {
                    this.episodeSelectorTarget.disabled = true;
                }
                this.loadingIconOutlet.increaseCount();
            } else {
                console.log(`HTTP Response Code: ${response?.status}`)
            }
        }
    }

    //
    // async clearCache() {
    //     await fetch(`/torrentio/tvshows/clear/${this.tmdbIdValue}/${this.imdbIdValue}/${this.seasonValue}/${this.episodeValue}`)
    //         .then(res => res.text())
    //         .then(response => {});
    // }

    async setActive() {
        if (false === this.optionsLoaded) {
            await this.setOptions();
        }
    }

    async setInActive() {
        this.episodeSelectorTarget.checked = false;
    }

    isActive() {
        return this.activeValue;
    }

    isSelected() {
        return this.episodeSelectorTarget.checked;
    }

    selectEpisodeForDownload() {
        if (true === this.isActive() && this.episodeSelectorTarget.disabled === false) {
            this.episodeSelectorTarget.checked = !this.episodeSelectorTarget.checked;
        }
    }

    toggleList() {
        // if (!this.isOpen) {
        //     this.toggleButtonTarget.classList.add('rotate-180');
        //     this.toggleButtonTarget.classList.remove('-rotate-180');
        // } else {
        //     this.toggleButtonTarget.classList.add('-rotate-180');
        //     this.toggleButtonTarget.classList.remove('rotate-180');
        // }
        this.listTarget.classList.toggle('hidden');
        this.toggleButtonTarget.classList.toggle('rotate-90');
        this.toggleButtonTarget.classList.toggle('-rotate-90');
    }

    download() {
        this.options.forEach(option => {
            const optionSelector = option.querySelector('input[type="checkbox"]');
            if (true === optionSelector.checked) {
                const downloadBtn = option.querySelector('button.download-btn');
                const downloadBtnController = this.application.getControllerForElementAndIdentifier(downloadBtn, 'download-button');
                downloadBtnController.download();
                optionSelector.checked = false;
                this.episodeSelectorTarget.checked = false;
            }
        })
    }

    async filter(activeFilter, currentSeason, newSeason) {
        if (currentSeason !== activeFilter['season']) {
            if (this.seasonValue === newSeason) {
                await this.setActive();
            } else {
                await this.setInActive();
            }
        }

        if (false === this.isActive()) {
            return;
        }

        let firstIncluded = true;
        let count = 0;
        let selectedCount = 0;

        this.options.forEach((option) => {
            const props = {
                "resolution": option.querySelector('#resolution').textContent.trim(),
                "codec": option.querySelector('#codec').textContent.trim(),
                "provider": option.querySelector('#provider').textContent.trim(),
                "languages": JSON.parse(option.dataset['languages']),
            }

            let include = true;
            option.classList.remove('hidden');
            option.querySelector('input[type="checkbox"]').checked = false;

            for (let [key, value] of Object.entries(activeFilter)) {
                if (value === "" || key === "season") {
                    continue;
                }
                if (key === "codec" && value === "h264") {
                    if (!this.H264_CODECS.includes(props[key].toLowerCase())) {
                        include = false;
                    }
                } else if (key === "codec" && value === "h265") {
                    if (!this.H265_CODECS.includes(props[key].toLowerCase())) {
                        include = false;
                    }
                } else if (key === "language") {
                    if (!props["languages"].includes(value)) {
                        include = false;
                    }
                } else if (props[key] !== value) {
                    include = false;
                }
            }

            if (false === include) {
                option.classList.add('hidden');
            } else if (true === firstIncluded) {
                count = 1;
                selectedCount = selectedCount + 1;
                option.querySelector('input[type="checkbox"]').checked = true;
                firstIncluded = false;
            } else {
                count = count + 1;
            }

            this.countTarget.innerText = count;
        });
    }
}
