"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const hidive_1 = __importDefault(require("../../../hidive"));
const module_args_1 = require("../../../modules/module.args");
const module_langsData_1 = require("../../../modules/module.langsData");
const base_1 = __importDefault(require("./base"));
const log_1 = require("../../../modules/log");
const yargs = __importStar(require("../../../modules/module.app-args"));
class HidiveHandler extends base_1.default {
    constructor(ws) {
        super(ws);
        this.name = 'hidive';
        this.hidive = new hidive_1.default();
        this.initState();
    }
    auth(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.hidive.doAuth(data);
        });
    }
    checkToken() {
        return __awaiter(this, void 0, void 0, function* () {
            //TODO: implement proper method to check token
            return { isOk: true, value: undefined };
        });
    }
    search(data) {
        return __awaiter(this, void 0, void 0, function* () {
            log_1.console.debug(`Got search options: ${JSON.stringify(data)}`);
            const hidiveSearch = yield this.hidive.doSearch(data);
            if (!hidiveSearch.isOk) {
                return hidiveSearch;
            }
            return { isOk: true, value: hidiveSearch.value };
        });
    }
    handleDefault(name) {
        return __awaiter(this, void 0, void 0, function* () {
            return (0, module_args_1.getDefault)(name, this.hidive.cfg.cli);
        });
    }
    availableDubCodes() {
        return __awaiter(this, void 0, void 0, function* () {
            const dubLanguageCodesArray = [];
            for (const language of module_langsData_1.languages) {
                if (language.new_hd_locale)
                    dubLanguageCodesArray.push(language.code);
            }
            return [...new Set(dubLanguageCodesArray)];
        });
    }
    availableSubCodes() {
        return __awaiter(this, void 0, void 0, function* () {
            const subLanguageCodesArray = [];
            for (const language of module_langsData_1.languages) {
                if (language.new_hd_locale)
                    subLanguageCodesArray.push(language.locale);
            }
            return ['all', 'none', ...new Set(subLanguageCodesArray)];
        });
    }
    resolveItems(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const parse = parseInt(data.id);
            if (isNaN(parse) || parse <= 0)
                return false;
            log_1.console.debug(`Got resolve options: ${JSON.stringify(data)}`);
            const res = yield this.hidive.selectSeries(parseInt(data.id), data.e, data.but, data.all);
            if (!res.isOk || !res.value)
                return res.isOk;
            this.addToQueue(res.value.map(item => {
                return Object.assign(Object.assign({}, data), { ids: [item.id], title: item.title, parent: {
                        title: item.seriesTitle,
                        season: item.episodeInformation.seasonNumber + ''
                    }, image: item.thumbnailUrl, e: item.episodeInformation.episodeNumber + '', episode: item.episodeInformation.episodeNumber + '' });
            }));
            return true;
        });
    }
    listEpisodes(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const parse = parseInt(id);
            if (isNaN(parse) || parse <= 0)
                return { isOk: false, reason: new Error('The ID is invalid') };
            const request = yield this.hidive.listSeries(parse);
            if (!request.isOk || !request.value)
                return { isOk: false, reason: new Error('Unknown upstream error, check for additional logs') };
            return { isOk: true, value: request.value.map(function (item) {
                    var _a, _b;
                    const description = item.description.split('\r\n');
                    return {
                        e: item.episodeInformation.episodeNumber + '',
                        lang: [],
                        name: item.title,
                        season: item.episodeInformation.seasonNumber + '',
                        seasonTitle: (_b = (_a = request.series.seasons[item.episodeInformation.seasonNumber - 1]) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : request.series.title,
                        episode: item.episodeInformation.episodeNumber + '',
                        id: item.id + '',
                        img: item.thumbnailUrl,
                        description: description ? description[0] : '',
                        time: ''
                    };
                }) };
        });
    }
    downloadItem(data) {
        return __awaiter(this, void 0, void 0, function* () {
            this.setDownloading(true);
            log_1.console.debug(`Got download options: ${JSON.stringify(data)}`);
            const _default = yargs.appArgv(this.hidive.cfg.cli, true);
            const res = yield this.hidive.selectSeries(parseInt(data.id), data.e, false, false);
            if (!res.isOk || !res.showData)
                return this.alertError(new Error('Download failed upstream, check for additional logs'));
            for (const ep of res.value) {
                yield this.hidive.downloadEpisode(ep, Object.assign(Object.assign({}, _default), { callbackMaker: this.makeProgressHandler.bind(this), dubLang: data.dubLang, dlsubs: data.dlsubs, fileName: data.fileName, q: data.q, force: 'y', noaudio: data.noaudio, novids: data.novids }));
            }
            this.sendMessage({ name: 'finish', data: undefined });
            this.setDownloading(false);
            this.onFinish();
        });
    }
}
exports.default = HidiveHandler;
