const datetime = require("../../config/datetime");
const reponceTitle = require("../helpers/replaceTitle");
const Answare = require("../model/Answare");
const Match = require("../model/Match");
const Quartion = require("../model/Quartion");
const Controller = require("./Controller");
module.exports = new class PublicController extends Controller {
    anyTrue = (...data) => data.some((d) => d);
    datas = async () => {
        const _matchs = await Match.get()
            .where("is_show", "=", "on")
            .andWhere("type", "!=", "closed")
            .orderBy("start_time", "asc");
        const sned = [];
        if (_matchs.length > 0) {
            for (const data of _matchs) {
                const title = `${data.tream_a} vs ${data.tream_b}`;
                const subTitle = `${data.league_name} @ ${data.start_time}`;
                const _quartions = await Quartion.get().where("is_show", "=", "on").andWhere("match_id", "=", data.id);
                const qur = [];
                for (const _qr of _quartions) {
                    const title = reponceTitle(_qr.title, data.tream_a, data.tream_b, _qr.runs, _qr.rets, _qr.overs);
                    const ans = [];
                    const _answers = await Answare.where("quertion_id", "=", _qr.id).andWhere("is_show", "=", "off");
                    for (const _ans of _answers) {
                        const title = reponceTitle(_ans.title, data.tream_a, data.tream_b, _qr.runs, _qr.rets, _qr.overs);
                        ans.push({
                            id: _ans.id,
                            title,
                            ret: _qr.is_ret_show == "on" ? _qr.rets : _ans.ret,
                            isLock: this.anyTrue(data.is_lock == "on", _qr.is_lock == "on", _ans.is_lock == "on"),
                            isShow: this.anyTrue(data.is_show == "on", _qr.is_show == "on", _ans.is_show == "on")
                        })
                    }
                    qur.push({
                        title,
                        id: _qr.id,
                        list: ans
                    })
                }
                sned.push({
                    id: data.id,
                    icon: data.icon,
                    title,
                    subTitle,
                    sportId: data.sport_id,
                    startTime: data.start_time,
                    score: data.score,
                    type: data.type,
                    sportName: data.sport_name,
                    leagueName: data.league_name,
                    list: qur
                })
            }
        }
        return sned;
    }
    box = async ({ match_id, q_id, a_id }) => {
        try {
            const data = await Match.find(match_id);
            const q_data = await Quartion.find(q_id);
            const a_data = await Answare.find(a_id);
            const getTitle = (t) => reponceTitle(t, data.tream_a, data.tream_b, q_data.runs, q_data.rets, q_data.overs)
            const title = `${getTitle(q_data.title)} - ${getTitle(a_data.title,)}`;
            const sendata = {
                title,
                ret: q_data.is_ret_show == "on" ? q_data.rets : a_data.ret,
                is_lock: this.anyTrue(data.is_lock == "on", q_data.is_lock == "on", a_data.is_lock == "on"),
                is_show: this.anyTrue(data.is_show == "on", q_data.is_show == "on", a_data.is_show == "on")
            };
            return sendata;
        } catch (error) {
            console.log(error.toString());
        }
    }
    // match list
    match = async (req, res, next) => {
        try {
            return res.success("success", await this.datas());
        } catch (error) {
            console.log(error.toString());
        }
    }
}