using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lol_coder.Data
{
    public class DataControl
    {
        
        

        #region Singleton

        private static DataControl uniqueInstance = null;
        private static readonly Object mInstanceLocker = new Object();

        public static DataControl getInstance()
        {
            lock (mInstanceLocker)
            {
                if (uniqueInstance == null)
                {
                    uniqueInstance = new DataControl();
                }
            }
            return uniqueInstance;
        }

        #endregion


        #region Team

        public List<Team> Teams = new List<Team>();
        public struct Team
        {
            public string TeamName;
            public List<Tuple<string, string, bool>> Players;
        }


        string teamPath = Environment.CurrentDirectory + @"\Data\Teams.json";
        string playerPath(string teamName) => Environment.CurrentDirectory + @"\Data\Team\" + teamName + ".json";


        public void SaveTeams()
        {
            try
            {
                JObject json = new JObject();

                JArray jArray = new JArray();
                foreach (Team t in Teams)
                {                    
                    jArray.Add(t.TeamName);
                }

                json.Add("Teams", jArray);


                File.WriteAllText(teamPath, json.ToString(), Encoding.UTF8);


            }
            catch (Exception ex)
            {
                Console.WriteLine("DatControl Err");
                Console.WriteLine(ex.Message);
            }
        }

        public void SavePlayer(string teamName)
        {
            try
            {
                int pIndex = 0;
                JObject json = new JObject();
                var target = Teams.Find(x => x.TeamName.Equals(teamName));

                if (target.Players == null) target.Players = new List<Tuple<string, string, bool>>();
                
                foreach (var t in target.Players)
                {
                    pIndex++;
                    JArray jArray = new JArray();
                    jArray.Add(t.Item1);
                    jArray.Add(t.Item2);
                    jArray.Add(t.Item3);
                    json.Add("Player" + pIndex, jArray);
                } 
                
                File.WriteAllText(playerPath(teamName), json.ToString(), Encoding.UTF8);


            }
            catch (Exception ex)
            {
                Console.WriteLine("DatControl Err");
                Console.WriteLine(ex.Message);
            }
        }

        public void LoadTeams()
        { 
            try
            {
                if (File.Exists(teamPath))
                {
                    using (StreamReader file = new StreamReader(teamPath))
                    {
                        using (JsonTextReader reader = new JsonTextReader(file))
                        {
                            JObject json = (JObject)JToken.ReadFrom(reader);
                            JToken teams = json.GetValue("Teams");

                            Teams.Clear();

                            foreach(string v in teams.ToArray())
                            {
                                Team team = new Team();
                                team.TeamName = v;
                                team.Players = new List<Tuple<string, string, bool>>();
                                Teams.Add(team);
                            }                             
                        }
                    } 
                }
            }
            catch (Exception ex) { }
        }

        public void LoadPlayer(string teamName)
        {
             

            try
            {
                if (File.Exists(playerPath(teamName)))
                {
                    using (StreamReader file = new StreamReader(playerPath(teamName)))
                    {
                        using (JsonTextReader reader = new JsonTextReader(file))
                        {
                            JObject json = (JObject)JToken.ReadFrom(reader);

                            int t = Teams.FindIndex(x => x.TeamName.Equals(teamName));

                            Teams.RemoveAt(t);

                            Team newTeam = new Team();
                            newTeam.TeamName = teamName;
                            newTeam.Players = new List<Tuple<string, string, bool>>();
                            
                            for (int i = 0; i <json.Count; i++)
                            {
                                
                                var v = json.GetValue("Player" + (i+1));
                                string name = (string)v[0];
                                string position = (string)v[1];
                                bool isPlay = (bool)v[2];
                                newTeam.Players.Add(new Tuple<string, string, bool>(name, position, isPlay));
                            }
                            
                            /*
                            foreach (JArray v in json.AsJEnumerable())
                            {
                                //newTeam.Players.Add(new Tuple<string, string, bool>("", "", false));
                                string name = (string)v[0];
                                string position = (string)v[1];
                                bool isPlay = (bool)v[2];
                                newTeam.Players.Add(new Tuple<string, string, bool>(name, position, isPlay));
                            }
                            */
                            Teams.Add(newTeam);


                        }
                    }
                }
            }
            catch (Exception ex) { }
        }

        #endregion 


        #region Match

        public List<Match> Matchs = new List<Match>();

        public struct Match
        {
            public string MatchNumber;
            public string BlueTeam;
            public string RedTeam;
            public string RoomName;
        }

        string matchPath = Environment.CurrentDirectory + @"\Data\Matchs.json";

        public void SaveMatchs()
        {
            try
            {
                
                JObject json = new JObject();

                int mIndex = 0;
                foreach (var m in Matchs)
                {
                    mIndex++;
                    JArray jArray = new JArray();
                    jArray.Add(m.MatchNumber);
                    jArray.Add(m.BlueTeam);
                    jArray.Add(m.RedTeam);
                    jArray.Add(m.RoomName);
                     
                    json.Add("Match" + mIndex, jArray);
                } 


                File.WriteAllText(matchPath, json.ToString(), Encoding.UTF8);


            }
            catch (Exception ex)
            {
                Console.WriteLine("DataControl Err");
                Console.WriteLine(ex.Message);
            }
        }

        public void LoadMatchs()
        { 
            try
            {
                if (File.Exists(matchPath))
                {
                    using (StreamReader file = new StreamReader(matchPath))
                    {
                        using (JsonTextReader reader = new JsonTextReader(file))
                        {
                            JObject json = (JObject)JToken.ReadFrom(reader);

                            Matchs.Clear();

                            for (int i = 0; i < json.Count; i++)
                            {

                                var v = json.GetValue("Match" + (i + 1));
                                Match bufMatch = new Match();
                                bufMatch.MatchNumber = (string)v[0];
                                bufMatch.BlueTeam = (string)v[1];
                                bufMatch.RedTeam = (string)v[2];
                                bufMatch.RoomName = (string)v[3];

                                Matchs.Add(bufMatch);
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { }
        }
        #endregion


        #region Liner

        public Liners BlueLiner = new Liners();
        public Liners RedLiner = new Liners();

        public class Liners
        {
            public string TeamName;
            public Liner Top = new Liner();
            public Liner Jungle = new Liner();
            public Liner Mid = new Liner();
            public Liner ADCarry = new Liner();
            public Liner Supporter = new Liner();

            public Liner GetLiner(int index)
            {
                if (index == 0) return Top;
                else if (index == 1) return Jungle;
                else if (index == 2) return Mid;
                else if (index == 3) return ADCarry;
                else  return Supporter;
            }

            public void SetLiner(int index, Liner liner)
            {
                if (index == 0) Top = liner;
                else if (index == 1) Jungle = liner;
                else if (index == 2) Mid = liner;
                else if (index == 3) ADCarry = liner;
                else Supporter = liner;
            }
        }

        public class Liner
        {

            public string Name = "";
            public string champ = "";
            public string GoldHave = "";
            public string GoldSum = "";
            public string GoldRate = "";

            public bool isQuest = false;

            public int champID = -1;
            public string state = "";

            public string ban = "";
        }


        #endregion
    }
}
