"use strict";
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 open_1 = __importDefault(require("open"));
const __1 = require("..");
const path_1 = __importDefault(require("path"));
const log_1 = require("../../../modules/log");
const module_cfg_loader_1 = require("../../../modules/module.cfg-loader");
const package_json_1 = __importDefault(require("../../../package.json"));
class Base {
    constructor(ws) {
        this.ws = ws;
        this.name = 'default';
        this.downloading = false;
        this.queue = [];
        this.workOnQueue = false;
        this.state = (0, module_cfg_loader_1.getState)();
    }
    version() {
        return new Promise(() => {
            return package_json_1.default.version;
        });
    }
    initState() {
        if (this.state.services[this.name]) {
            this.queue = this.state.services[this.name].queue;
            this.queueChange();
        }
        else {
            this.state.services[this.name] = {
                'queue': []
            };
        }
    }
    setDownloading(downloading) {
        this.downloading = downloading;
    }
    getDownloading() {
        return this.downloading;
    }
    alertError(error) {
        log_1.console.error(`${error}`);
    }
    makeProgressHandler(videoInfo) {
        return ((data) => {
            this.sendMessage({
                name: 'progress',
                data: {
                    downloadInfo: videoInfo,
                    progress: data
                }
            });
        });
    }
    sendMessage(data) {
        this.ws.sendMessage(data);
    }
    isDownloading() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.downloading;
        });
    }
    openFolder(folderType) {
        return __awaiter(this, void 0, void 0, function* () {
            switch (folderType) {
                case 'content':
                    (0, open_1.default)(__1.cfg.dir.content);
                    break;
                case 'config':
                    (0, open_1.default)(__1.cfg.dir.config);
                    break;
            }
        });
    }
    openFile(data) {
        return __awaiter(this, void 0, void 0, function* () {
            switch (data[0]) {
                case 'config':
                    (0, open_1.default)(path_1.default.join(__1.cfg.dir.config, data[1]));
                    break;
                case 'content':
                    throw new Error('No subfolders');
            }
        });
    }
    openURL(data) {
        return __awaiter(this, void 0, void 0, function* () {
            (0, open_1.default)(data);
        });
    }
    getQueue() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.queue;
        });
    }
    removeFromQueue(index) {
        return __awaiter(this, void 0, void 0, function* () {
            this.queue.splice(index, 1);
            this.queueChange();
        });
    }
    clearQueue() {
        return __awaiter(this, void 0, void 0, function* () {
            this.queue = [];
            this.queueChange();
        });
    }
    addToQueue(data) {
        this.queue = this.queue.concat(...data);
        this.queueChange();
    }
    setDownloadQueue(data) {
        this.workOnQueue = data;
        this.queueChange();
    }
    getDownloadQueue() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.workOnQueue;
        });
    }
    queueChange() {
        return __awaiter(this, void 0, void 0, function* () {
            this.sendMessage({ name: 'queueChange', data: this.queue });
            if (this.workOnQueue && this.queue.length > 0 && !(yield this.isDownloading())) {
                this.setDownloading(true);
                this.sendMessage({ name: 'current', data: this.queue[0] });
                this.downloadItem(this.queue[0]);
                this.queue = this.queue.slice(1);
                this.queueChange();
            }
            this.state.services[this.name].queue = this.queue;
            (0, module_cfg_loader_1.setState)(this.state);
        });
    }
    onFinish() {
        return __awaiter(this, void 0, void 0, function* () {
            this.sendMessage({ name: 'current', data: undefined });
            this.queueChange();
        });
    }
    //Overriten
    // eslint-disable-next-line
    downloadItem(_) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error('downloadItem not overriden');
        });
    }
}
exports.default = Base;
