const
    {
        market_list,
        shop_data
    } = require("./server");
const NewsItemServer = require("./NewsItemServer.js");
const newsArray = [
   new NewsItemServer("2023-11-11", "Новость 1"),
   new NewsItemServer("2023-11-11", "Новость 2"),
   new NewsItemServer("2023-11-11", "Новость 3"),
   new NewsItemServer("2023-11-11", "Новость 4"),
   new NewsItemServer("2023-11-11", "Новость 5")
];
const Area = require("./Area.js"),
    {
        playerJson,
        battle_items,
        send
    } = require("./server");
var CTFBattle,
    TDMBattle,
    DMBattle,
    DOMBattle,
    ASLBattle,
    effect_model = {
        effects: []
    };

class Lobby extends Area {

    constructor() {
        super();
        this.battles = [];
        this.chat = require("./LobbyChat");
        this.battle_count = 0;
        this.anticheat = 0;
		this.newsShown = false; // Флаг, указывающий, были ли новости показаны игроку


    }

    createBattles() {
        if (this.battles.length === 0) {
            var battle = new DMBattle(true, "For newbies", "map_sandbox", 0, 8, 1, 5, 10, false, false);
            this.battles.push(battle);
            this.broadcast("create_battle;" + JSON.stringify(battle.toObject()));


        }
    }

//battle;init_mines;{"mines":[]}

    getChat() {
        return this.chat;
    }

    getPrefix() {
        return "lobby";
    }

    getBattles(socket, tank) {
        var arr = [];

        for (var i in this.battles) {
            arr.push(this.battles[i].toObject());
        }

        var sub = false;
        if (tank !== null) {
            sub = tank.garage.hasItem("no_supplies");
            tank.garage.removePlayer(tank.name);
            this.addPlayer(tank);
        }

        setTimeout(() => {
            this.send(socket, "init_battle_select;" + JSON.stringify(
                {
                    haveSubscribe: sub,
                    battles: arr,
                    items: battle_items
                }));
        }, 100);
    }

    findBattle(id) {
        for (var i in this.battles)
            if (this.battles[i].battleId === id)
                return this.battles[i];

        return null;
    }

    /**
     * Removes a battle.
     *
     * @param {string} battleId
     *
     * @returns boolean
     */
    removeBattle(battleId) {
        for (var i in this.battles) {
            if (this.battles[i].battleId === battleId) {
                this.battles.splice(i, 1);
                this.broadcast("remove_battle;" + battleId);
                this.createBattles();
                return true;

            }
        }

        return false
    }

    initEffectModel(socket) {
        this.send(socket, "init_effect_model;" + JSON.stringify(effect_model));
        this.createBattles();
    }

    sendNews(socket) {
        if (!this.newsShown) { // Проверяем, были ли новости уже показаны
            const newsJSON = JSON.stringify(newsArray);
            this.send(socket, "show_news;" + newsJSON);
            this.newsShown = true; // Устанавливаем флаг, что новости были показаны
        }
    }

    onData(socket, args) {
        var battles = this.battles,
            battle_count = this.battle_count,
            tank = socket.tank;
        if (tank === null) {
            socket.destroy();
            return;
        }
		

	
     if (args.length === 1 && args[0] === "show_news") {
    // Отправка новостей на клиент
    sendNews(socket);
}

        if (args.length === 1 && args[0] === "get_data_init_battle_select") {
            var arr = [];



            for (var i in this.battles) {
                arr.push(this.battles[i].toObject());
            }

            var sub = false;
            if (tank !== null) {
                sub = tank.garage.hasItem("no_supplies");
                tank.garage.removePlayer(tank.name);
                this.addPlayer(tank);
            }

            setTimeout(() => {
                this.send(socket, "init_battle_select;" + JSON.stringify(
                    {
                        haveSubscribe: sub,
                        battles: arr,
                        items: battle_items
                    }));
            }, 100);
        }
        if (args.length === 1 && args[0] === "get_rating") {
            this.send(socket, "open_rating;" + JSON.stringify(
                {
                    crystalls: [
                        {
                            "count": tank.crystals,
                            "rank": tank.rank,
                            "id": tank.name
                        }]
                }));

        } else if (args.length === 1 && args[0] === "show_quests") {


        } 
		else if (args[0] === "user_inited") {

        }
		else if (args.length === 1 && args[0] === "show_profile") 
		{
            this.send(socket, "show_profile;" + JSON.stringify(
                {
                    isComfirmEmail: false,
                    emailNotice: false
                }));
        } else if (args.length === 2 && args[0] === "try_create_battle_ctf") {
            if (tank.rank > 3) {
                try {
                    const data = JSON.parse(args[1]);
                    const battle = new CTFBattle(false, data.gameName, data.mapId, data.time, data.numPlayers, data.minRang, data.maxRang, data.numFlags, !data.inventory, data.autoBalance, data.frielndyFire, data.pay, false);
                    this.battles.push(battle);
                    this.broadcast("create_battle;" + JSON.stringify(battle.toObject()));
                    console.log('\x1b[35m%s\x1b[0m', ' > ' + tank.name + " создал битву " + data.gameName);
                } catch (e) {
                    console.log(e);
                }
            } else {
                this.send(socket, "server_message;Создавать битвы можно со звания Капрал")
            }
        } else if (args.length === 2 && args[0] === "try_create_battle_dom") {
            if (tank.rank > 3) {
                try {
                    const data = JSON.parse(args[1]);
                    const battle = new DOMBattle(false, data.gameName, data.mapId, data.time, data.numPlayers, data.minRang, data.maxRang, data.numPointsScore, !data.inventory, data.autoBalance, data.frielndyFire, data.pay, false);
                    this.battles.push(battle);
                    this.broadcast("create_battle;" + JSON.stringify(battle.toObject()));
                    console.log('\x1b[35m%s\x1b[0m', ' > ' + tank.name + " создал битву " + data.gameName);
                } catch (e) {
                    console.log(e);
                }
            } else {
                this.send(socket, "server_message;Создавать битвы можно со звания Капрал")
            }
        } else if (args.length === 2 && args[0] === "try_create_battle_tdm") {
            if (tank.rank > 3) {
                try {
                    const data = JSON.parse(args[1]);
                    console.log('data', data);
                    const battle = new TDMBattle(false, data.gameName, data.mapId, data.time, data.numPlayers, data.minRang, data.maxRang, data.numKills, !data.inventory, data.autoBalance, data.frielndyFire, data.pay);
                    this.battles.push(battle);
                    this.broadcast("create_battle;" + JSON.stringify(battle.toObject()));
                    console.log('\x1b[35m%s\x1b[0m', ' > ' + tank.name + " создал битву " + data.gameName);
                } catch (e) {
                    console.log(e);
                }
            } else {
                this.send(socket, "server_message;Создавать битвы можно со звания Капрал")
            }
        } else if (args[0] === "try_create_battle_dm") {
            if (tank.rank > 3) {
                try {
                    const withoutBonuses = args[10] === 'true';
                    const withoutSupplies = args[9] === 'true';

                    var battle = new DMBattle(false, args[1], args[2], args[3], args[5], args[6], args[7], args[4], !withoutBonuses, withoutSupplies);
                    this.battles.push(battle);
                    this.broadcast("create_battle;" + JSON.stringify(battle.toObject()));
                    console.log('\x1b[35m%s\x1b[0m', ' > ' + tank.name + " создал битву в режиме DM ");
                } catch (e) {
                    console.log(e);
                }

            } else {
                this.send(socket, "server_message;Создавать битвы можно со звания Капрал")
            }
        } else if (args.length === 2 && args[0] === "get_show_battle_info") {
            for (var i in battles) {
                if (battles[i].battleId === args[1]) {
                    this.send(socket, "show_battle_info;" + JSON.stringify(battles[i].toDetailedObject(tank)));
                }

            }

        } else if (args.length === 2 && (args[0] === "enter_battle_spectator")) {
            var battle = this.findBattle(args[1]);

            if (battle !== null)
                battle.addSpectator(tank);
        } else if (args[3] !== "null" && args.length === 3 && (args[0] === "enter_battle_team")) {

            var battle = this.findBattle(args[1]);

            if (battle !== null) {
                this.anticheat++;
                setTimeout(() => {
                    if (this.anticheat > 1) {
                        this.send(tank.socket, "battle;kick_by_cheats");
                        tank.socket.destroy();
                    }
                }, 50);
                setTimeout(() => {
                    this.anticheat--;
                }, 1000);
                var type = args[2] === "false" ? "BLUE" : "RED";
                send(socket, "lobby;start_battle");
                var user = battle.addPlayer(tank, type);

                if (user !== null)
                    this.broadcast("add_player_to_battle;" + JSON.stringify(
                        {
                            kills: user.kills,
                            battleId: args[2],
                            name: tank.name,
                            rank: tank.rank,
                            id: tank.name,
                            type: type
                        }));

            }
        } else if (args[3] !== "null" && args.length === 3 && (args[0] === "enter_battle")) {
            var battle = this.findBattle(args[1]);
            send(socket, "lobby;start_battle");

            if (battle !== null && battle.getTeamCount() === 1) {
                var user = battle.addPlayer(tank);
                if (user !== null)
                    this.broadcast("add_player_to_battle;" + JSON.stringify(
                        {
                            kills: user.kills,
                            battleId: args[2],
                            name: tank.name,
                            rank: tank.rank,
                            id: tank.name,
                            type: "NONE"
                        }));
            }
        } else if (args.length === 2 && args[0] === "check_battleName_for_forbidden_words") {
            send(socket, "lobby;check_battle_name;" + args[1]);
        }         else if (args.length === 1 && args[0] === "get_garage_data") {

            }
            var tank = socket.tank;
            if (tank !== null) {
                setTimeout(() => {
                    tank.garage.initiate(socket);
                    this.initEffectModel(socket);

                    if (this.hasPlayer(tank.name))
                        this.removePlayer(tank.name);
                }, 1000);
            }
        }
    }

}

module.exports = new Lobby();

CTFBattle = require("./CTFBattle");
TDMBattle = require("./TDMBattle");
DMBattle = require("./DMBattle");
DOMBattle = require("./DOMBattle");
ASLBattle = require("./ASLBattle"); как сделать чтобы 			            if (!this.newsShown) { // Проверяем, были ли новости уже показаны
                this.sendNews(socket);
            } загружались тоже