
using UnityEngine;
using System.Collections.Generic;
using NeonTea.Quakeball.Networking.Instances;
using NeonTea.Quakeball.Networking;
using NeonTea.Quakeball.Interface;

namespace NeonTea.Quakeball.Game {

    public class GameMaster {
        private NetInstance Instance;

        public GameMode GameMode;
        public Dictionary<Team, List<ulong>> Players = new Dictionary<Team, List<ulong>>();

        public GameMaster() {
            Instance = Net.Singleton.Instance;

            GameMode = GameMode.TeamPlay;
            Players[Team.FreeForAll] = new List<ulong>();
            Players[Team.Sun] = new List<ulong>();
            Players[Team.Moon] = new List<ulong>();
        }

        public void PlayerJoined(NetPlayer player) {
            Team team = Team.FreeForAll;
            if (GameMode == GameMode.TeamPlay) {
                if (Players[Team.Sun].Count > Players[Team.Moon].Count) {
                    team = Team.Sun;
                } else {
                    team = Team.Moon;
                }
            }
            Players[Team.Sun].Add(player.Id);
            player.Team = team;
            Terminal.Singleton.Println($"Put {player.Nick} to Team {player.Team}");
        }

        public void PlayerLeft(NetPlayer player) {
            Players[player.Team].Remove(player.Id);
        }

        public Vector3 GetSpawn(NetPlayer player) {
            string Tag = "Respawn";
            switch (player.Team) {
                case Team.Sun:
                    Tag = "SunRespawn";
                    break;
                case Team.Moon:
                    Tag = "MoonRespawn";
                    break;
            }
            GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag(Tag);
            return spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
        }
    }

    public enum GameMode {
        TeamPlay = 0,
        FreeForAll = 1,
    }

    public enum Team {
        FreeForAll = 0,
        Sun = 1,
        Moon = 2,
    }
}
