using Thousandto.Core.Base;
using Thousandto.Core.Support;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Thousandto.Plugins.Common.UniScene
{
    public static class FlyTeleportDataManager
    {
        private static Dictionary<int, FlyTeleportCfgData> _datasTable = new Dictionary<int, FlyTeleportCfgData>();
        private static Dictionary<string, List<FlyTeleportCfgData>> _mapDatasTable = new Dictionary<string, List<FlyTeleportCfgData>>();

        public static FlyTeleportCfgData Find(int id)
        {
            FlyTeleportCfgData result = null;
            _datasTable.TryGetValue(id, out result);
            return result;
        }

        public static List<FlyTeleportCfgData> Find(string mapName)
        {
            List<FlyTeleportCfgData> result = null;
            _mapDatasTable.TryGetValue(mapName, out result);
            return result;
        }

        public static Dictionary<int, FlyTeleportCfgData> DatasTable
        {
            get
            {
                return _datasTable;
            }
        }
        public static Dictionary<string, List<FlyTeleportCfgData>> MapDatasTable
        {
            get
            {
                return _mapDatasTable;
            }
        }

        public static void Remove(int id)
        {
            _datasTable.Remove(id);
        }

        public static void Load()
        {
            _datasTable.Clear();
            _mapDatasTable.Clear();
            FileStream fs = null;
            try
            {
                fs = new FileStream(FileUtils.FlyTeleportConfigBinaryPath, FileMode.Open, FileAccess.Read);
                var reader = new BinaryReader(fs);

                int dataCount = BinarySerializer.Read_Int32(reader);

                for (int i = 0; i < dataCount; ++i)
                {
                    BinarySerializer.Read_Int32(reader);
                    BinarySerializer.Read_Vector3(reader);
                }
                for (int i = 0; i < dataCount; ++i)
                {
                    FlyTeleportCfgData data = new FlyTeleportCfgData();
                    data.ReadData(reader);

                    _datasTable.Add(data.ID, data);

                    List<FlyTeleportCfgData> dataList = null;
                    if (_mapDatasTable.TryGetValue(data.MapName, out dataList))
                    {
                        dataList.Add(data);
                    }
                    else
                    {
                        dataList = new List<FlyTeleportCfgData>();
                        dataList.Add(data);
                        _mapDatasTable.Add(data.MapName, dataList);
                    }
                }
            }
            catch (IOException e)
            {
                Debug.LogError("Load PathGrid file failed: " + e.ToString());
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        public static void Save()
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(FileUtils.FlyTeleportConfigBinaryPath, FileMode.Create, FileAccess.Write);
                var writer = new BinaryWriter(fs);
                //数量
                BinarySerializer.Write_Int32(writer, _datasTable.Count);

                var iter = _datasTable.GetEnumerator();
                while (iter.MoveNext())
                {
                    BinarySerializer.Write_Int32(writer, iter.Current.Value.ID);
                    BinarySerializer.Write_Vector3(writer, iter.Current.Value.PathNodes[iter.Current.Value.PathNodes.Count - 1].Pos);
                }

                iter = _datasTable.GetEnumerator();
                while (iter.MoveNext())
                {
                    iter.Current.Value.WriteData(writer);
                }
            }
            catch (IOException e)
            {
                Debug.LogError("Save PathGrid data failed! " + e);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
    }
}