const DipositController = require("../controllers/Diposit.Controller");
const MatchController = require("../controllers/Match.Controller");
const PublicController = require("../controllers/Public.Controller");
const WidthdrowController = require("../controllers/Widthdrow.Controller");
const Deposit = require("../model/Deposit");
const Widthdrow = require("../model/Widthdrow");
const { AppEvent, AppEventType } = require("./my.event");

const dpData = async (socket) => {
    const datas = await DipositController.data();
    socket.emit("depositedata", datas);
}
const adData = async (socket) => {
    const datas = await WidthdrowController.data();
    socket.emit("widthdrowdata", datas);
}
const Notifacation = async () => {
    const [dpcount, wdcount] = await Promise.all([
        Deposit.where("status", Deposit.status.pending),
        Widthdrow.where("status", Widthdrow.status.pending)
    ])
    const arrya = [dpcount.length, wdcount.length];
    if (arrya.some(v => v > 0)) {
        return {
            diposit: dpcount.length || null,
            widthdraw: wdcount.length || null,
        }
    }
}

const ioSystems = (io) => {
    io.on("connection", (socket) => {
        console.log("a user connected");
        // public match update
        socket.on("public-init", async () => {
            const datas = await PublicController.datas();
            socket.emit("public-match", datas);
        })
        // panel match data
        socket.on("match-init", async (id) => {
            const datas = await MatchController.q_data(id);
            socket.emit("panel-match", datas);
            const headata = await MatchController.headata(id);
            socket.emit("panel-match-head", headata)
        })
        //event diposit
        socket.on("deposite", x => dpData(socket));
        // EVENT widthdraw
        socket.on("widthdrow", x => adData(socket));
        // notifacation-----------------------------
        socket.on("gave-notifacation", async () => {
            socket.emit("recive-notifacation", await Notifacation());
        })
        // notifacation- end----------------------------
        // bet box
        const Betbox = async (data) => {
            const resuld = await PublicController.box(data);
            socket.emit("box-data", resuld);
        }
        socket.on("init-box", Betbox)



        // disconnect
        socket.on("disconnect", () => {
            console.log("user disconnected");
        });
    });
    // ------------------GLOBAL EVENT-----------------------
    AppEvent.on(AppEventType.call, () => {
        io.emit("public-update");
        io.emit("box-update");
        io.emit("panel-update");
    })
    AppEvent.on(AppEventType.NOTIFACATION, async () => {
        io.emit("recive-notifacation", await Notifacation());
        const datas1 = await WidthdrowController.data();
        io.emit("widthdrowdata", datas1);
        const datas = await DipositController.data();
        io.emit("depositedata", datas);
    })
}
module.exports = ioSystems;