//const ini = require('ini');
const commandLineArgs = require('command-line-args');
class CommandLine {
    constructor({ config, logger, utils, diyanet, player } = {}) {
        this.config = config;
        this.logger = logger;
        this.utils = utils;
        //this.ini = ini;
        this.diyanet = diyanet;
        this.player = player;
        this.optionDefinitions = [
            { name: 'getsoundcards', alias: 's', type: Boolean },
            { name: 'getrecordingdevices', alias :'r', type: Boolean },
            { name: 'enableprayer', alias: 'e', type: Number },
            { name: 'getcities', type: Number },
            { name: 'gettowns', alias: 'i', type: Number, multiple: true, defaultOption: false },
            { name: 'setcity', type: Number, multiple: true },
            { name: 'settown', type: Number, multiple: true },
            // { name: 'setvolume',  type: Number },
            { name: 'setvolumedown', type: Number },
            { name: 'setvolumeup', type: Number },
            { name: 'checkhour', type: Number },
            { name: 'checkminute', type: Number }
        ];
        this.setPrayerOptions();

    }
    async setPrayerOptions() {
        try {
            const options = commandLineArgs(this.optionDefinitions);
            if (options.getsoundcards) {
                let devices = this.player.getDevices();

                this.logger.System('Device list:');
                devices.forEach((c) => {
                    if (c != null) {
                        this.logger.System(`${c.index}-${c.name} ${c.IsDefault ? '(default)' : ''}, active:${c.enabled}`);
                    }
                });
                process.exit(6)
            }

            if(options.getrecordingdevices) {
                let recordingDevices = this.player.recordingDevicesInfos();
                this.logger.System('Recording devices list:');
                recordingDevices.forEach((c) => {
                    if(c != null) {
                        console.log(`${c.index}-${c.name} ${c.IsDefault ? '(default)' : ''}, active:${c.enabled}, microphone: ${c.IsMic}, linein: ${c.IsLineIn}`);
                    }
                });
                process.exit(6);

            }
            if (options.enableprayer != undefined) {
                this.config.PrayerTime.Enabled = options.enableprayer;
                this.utils.writeConfig(this.config);
            }
            if (options.setvolumedown) {
                this.config.PrayerTime.volumeDown = options.setvolumedown;
                this.utils.writeConfig(this.config);
            }
            if (options.setvolumeup) {
                this.config.PrayerTime.volumeUp = options.setvolumeup;
                this.utils.writeConfig(this.config);
            }
            // if(options.setvolume) {
            //     this.config.PrayerTime.volume = options.setvolume;
            //     this.utils.writeConfig(this.config);
            // }
            if (options.checkhour) {
                this.config.PrayerTime.checkHour = options.checkhour;
                this.utils.writeConfig(this.config);
            }
            if (options.checkminute) {
                this.config.PrayerTime.checkMinute = options.checkminute;
                this.utils.writeConfig(this.config);
            }
            if (options.gettowns) {
                if (options.gettowns.length == 2) {
                    let countryCode = options.gettowns[0];
                    let cityCode = options.gettowns[1];
                    this.diyanet.getIlceler(countryCode, cityCode, (done) => {
                        this.logger.PrayerTime(done);
                    }, (err) => {
                        this.logger.PrayerTime(`Error getting towns with args: ${err}`);
                    });
                }
            }
            if (options.getcities) {
                let countryCode = options.getcities;
                this.diyanet.getSehirler(countryCode, (done) => {
                    this.logger.PrayerTime(done);
                }, (err) => {
                    this.logger.PrayerTime(`Error getting city list with args: ${err}`);
                })
            }
            if (options.settown) {
                if (options.settown.length == 3) {
                    if (this.config.PrayerTime.townID != options.settown[2]) {
                        await this.diyanet.deleteJson(this.diyanet.prayerJson);
                    }
                    this.config.PrayerTime.countryID = options.settown[0];
                    this.config.PrayerTime.cityID = options.settown[1];
                    this.config.PrayerTime.townID = options.settown[2];
                    this.utils.writeConfig(this.config);
                    this.diyanet.getIlceler(this.config.PrayerTime.countryID, this.config.PrayerTime.cityID, (done) => {
                        this.logger.PrayerTime(done);
                        for (let i = 0; i < done.length; i++) {
                            let townID = parseInt(done[i].IlceID);
                            if (townID == this.config.PrayerTime.townID) {
                                this.config.PrayerTime.townUrl = done[i].IlceUrl;
                                this.config.PrayerTime.townName = done[i].IlceAdi;
                                this.utils.writeConfig(this.config);
                                this.logger.PrayerTime(`townUrl: ${this.config.PrayerTime.townUrl}`);
                            }
                        }
                    }, (err) => {
                        this.logger.PrayerTime(`Error setting town & town url with args: ${err}`);
                    })
                }
            }

        } catch (err) {
            const getUsage = require('command-line-usage');
            const optionList = [
                {
                    name: 'getsoundcards',
                    description: 'Lists available devices'
                },
                {
                    name: 'enableprayer',
                    description: 'Enables/Disables Prayer times.'
                },
                {
                    name: 'getcities',
                    typeLabel: '[underline]{countryCode}',
                    description: 'Get a list of cities in a country.'
                },
                {
                    name: 'gettowns',
                    typeLabel: '[underline]{countryCode} [underline]{cityCode}',
                    description: 'Get a list of towns entering country and city code.'
                },
                {
                    name: 'setcity',
                    typeLabel: '[underline]{contryCode} [underline]{cityCode}',
                    description: 'Set config entering country and city codes.'
                },
                {
                    name: 'settown',
                    typeLabel: '[underline]{countryCode} [underline]{cityCode} [underline]{townCode}',
                    description: 'Set config entering country, city and town codes.'
                },
                // {
                //     name: 'setvolume',
                //     typeLabel: '[underline]{volume}',
                //     description: 'Set config entering volume.'
                // },
                {
                    name: 'setvolumedown',
                    typeLabel: '[underline]{minute}',
                    description: 'Set how long before the prayer time to turn volume down. ex: -10'
                },
                {
                    name: 'setvolumeup',
                    typeLabel: '[underline]{minute}',
                    description: 'Set how long after the prayer time to turn volume up. ex: 10'
                },
                {
                    name: 'checkhour',
                    typeLabel: '[underline]{hour}',
                    description: 'When to check for new prayer times every day'
                },
                {
                    name: 'checkminute',
                    typeLabel: '[underline]{minute}',
                    description: 'When to check for new prayer times every day'
                },
                {
                    name: 'help',
                    description: 'Prints this info.'
                }
            ];
            const sections = [
                {
                    header: `SMGPlayer ${this.utils.appversion}`,
                    content: 'Music for business'
                },
                {
                    header: 'Options',
                    optionList: optionList
                }
            ];
            const usage = getUsage(sections);
            this.logger.PrayerTime(usage);
            process.exit(6)
        }

    }

}

module.exports = CommandLine;
