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']

    languages = []
    providers = []
    seasons = []

    activeFilter = {
        "resolution": "",
        "codec": "",
        "language": "",
        "provider": "",
    }

    static outlets = ['movie-results', 'tv-results', 'tv-episode-list']
    static targets = ['resolution', 'codec', 'language', 'provider', 'season', 'selectAll', 'downloadSelected']
    static values = {
        'media-type': String,
        'episodes': Array,
    }

    async connect() {
        if (this.mediaTypeValue === "tvshows") {
            this.activeFilter['season'] = 1;
        }
        await this.filter();
    }

    async movieResultsOutletConnected(outlet) {
        await this.parseDownloadOptionForFilter(outlet)
    }

    async tvResultsOutletConnected(outlet) {
        await this.parseDownloadOptionForFilter(outlet)
    }

    async parseDownloadOptionForFilter(outlet) {
        if (outlet.options.length === 0) {
            await outlet.setOptions();
        }
        outlet.options.forEach((option) => {
            this.addLanguages(option, option.dataset);
            this.addProviders(option, option.dataset);
        })
        await this.filter();
    }

    addLanguages(option, props) {
        const languages = Object.assign([], JSON.parse(props['languages']));
        languages.forEach((language) => {
            if (!this.languages.includes(language)) {
                this.languages.push(language);
            }
        });

        const preferred = this.languageTarget.dataset.preferred;
        if (preferred) {
            this.languageTarget.innerHTML = '<option value="'+preferred+'" selected>'+preferred+'</option>';
            this.languageTarget.innerHTML += '<option value="">n/a</option>';
        } else {
            this.languageTarget.innerHTML = '<option value="">n/a</option>';
        }

        this.languageTarget.innerHTML += this.languages.sort()
            .map((language) => {
                const preferred = this.languageTarget.dataset.preferred;
                if (preferred === language) {
                    return;
                }
                return '<option value="'+language+'">'+language+'</option>';
            })
            .join();
    }

    addProviders(option, props) {
        if (!this.providers.includes(props['provider'])) {
            this.providers.push(props['provider']);
        }

        const preferred = this.providerTarget.dataset.preferred;
        if (preferred) {
            this.providerTarget.innerHTML = '<option value="'+preferred+'" selected>'+preferred+'</option>';
            this.providerTarget.innerHTML += '<option value="">n/a</option>';
        } else {
            this.providerTarget.innerHTML = '<option value="">n/a</option>';
        }

        this.providerTarget.innerHTML += this.providers.sort()
            .map((provider) => {
                const preferred = this.languageTarget.dataset.preferred;
                if (preferred === provider) {
                    return;
                }
                return '<option value="' + provider + '">' + provider + '</option>'
            })
            .join();

    }

    async filter() {
        const currentSeason = this.activeFilter['season'];

        let results = [];
        this.activeFilter = {
            "resolution": this.resolutionTarget.value,
            "codec": this.codecTarget.value,
            "language": this.languageTarget.value,
            "provider": this.providerTarget.value,
        }

        if ("movies" === this.mediaTypeValue) {
            results = this.movieResultsOutlets;
            await results.forEach((list) => list.filter(this.activeFilter));

        } else if ("tvshows" === this.mediaTypeValue) {
            results = this.tvResultsOutlets;
            this.activeFilter.season = this.seasonTarget.value;
            await results.forEach((list) => list.filter(this.activeFilter, currentSeason, this.seasonTarget.value));
        }
    }

    setSeason(event) {
        this.tvEpisodeListOutlet.setSeason(event.target.value);
    }

    uncheckSelectAllBtn() {
        this.selectAllTarget.checked = false;
    }

    selectAllEpisodes() {
        this.tvResultsOutlets.forEach((episode) => {
            if (episode.isActive()) {
                episode.selectEpisodeForDownload()
            }
        });
    }

    downloadSelectedEpisodes() {
        this.tvResultsOutlets.forEach(episode => {
            if (episode.isActive() && episode.isSelected()) {
                episode.download();
            }
        });
        this.selectAllTarget.checked = false;
    }
}
