const fs = require('fs')

module.exports = {
    setSubStatus: (bot, msg, logger, name) => {
        let subs = JSON.parse(fs.readFileSync('./data/group/subs.json', 'utf8'))
        const setSub = "set sub";
        if (msg.text && msg.text.toString().toLowerCase().includes(setSub)) {
            logger.info("ID - " + msg.from.id + " First Name - " + msg.from.first_name + " Asked how to get a subscription");
            sub = msg.text.match(/(?<=set sub )(.*)(?= to (up|down))/g)[0]
            status = msg.text.match(/\w+(?=( \w+){0}$)/g)[0]
            if (status === "up") {
                status = 1
            } else {
                status = 0
            }
            subs.forEach(function (element, index) {
                if (element.SUB.toLowerCase() === sub) {
                    subs[index].STATUS = status;   //set the value  
                }
            })
            console.log(subs);
            fs.writeFileSync('./data/group/subs.json', JSON.stringify(subs))
            bot.sendMessage(msg.chat.id, sub + " sub updated")
        }
    }
}