using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Station;
using SVGViewer.Models.Info;
using SVGViewer.Models.Svg;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace NEIntelligentControl2.Service.Station
{
    /// <summary>
    /// 场站信息维护
    /// </summary>
    public class StationManager
    {
        private List<StationInfo> _StationInfos;    // 场站信息
        private WEBHelper _WEBHelper;
        private string _Url;
        private Dictionary<string, StationInfo> _Stations;
        private List<StationInfo> _BoostStationInfos;
        private List<ShapeInfo> _BoostStationShapes;
        private Dictionary<string, ShapeInfo> _CustomizeShapesDic;
        public StationManager(WEBHelper web)
        {
            _WEBHelper = web;
            try
            {
#if (DEBUG)
                _Url = ConfigurationManager.AppSettings["ServicePathDebug"];
#else
                _Url = ConfigurationManager.AppSettings["ServicePath"];
#endif
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取配置文件[ServicePath]出错！", ex);
            }
        }

        /// <summary>
        /// 获取场站信息
        /// </summary>
        /// <returns>场站信息</returns>
        public List<StationInfo> GetStationInfos()
        {
            try
            {
                if (_StationInfos == null || _StationInfos.Count <= 0)
                {
                    _StationInfos = _WEBHelper.HttpGetJSON<List<StationInfo>>($"{_Url}/api/station/info");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取场站信息出错！", ex);
            }
            return _StationInfos;
        }

        /// <summary>
        /// 获取单场站信息
        /// </summary>
        /// <param name="stationId">场站ID</param>
        /// <returns></returns>
        internal StationInfo GetStatonInfo(string stationId)
        {
            try
            {
                if (_Stations == null)
                {
                    _Stations = _StationInfos?.ToDictionary(s => s.Id, s => s);
                }
            }
            catch { }
            if (_Stations == null || !_Stations.ContainsKey(stationId)) return null;
            return _Stations[stationId];
        }

        /// <summary>
        /// 获取升压站图形信息
        /// </summary>

        internal List<ShapeInfo> GetBoostStationShapes()
        {
            if (_BoostStationShapes == null)
            {
                InitBoostStationInfos();
            }
            return _BoostStationShapes;
        }


        /// <summary>
        /// 获取升压站场站信息
        /// </summary>
        /// <returns></returns>
        internal List<StationInfo> GetBoostStationInfos()
        {
            if (_BoostStationInfos == null)
            {
                InitBoostStationInfos();
            }

            return _BoostStationInfos;
        }
        /// <summary>
        /// 移除升压站
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        internal string RemoveBoostStation(ShapeInfo shape)
        {
            try
            {
                var v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/remove?id={shape.Id}");
                return v;
            }
            catch(Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 移除二次图
        /// </summary>
        /// <param name="arg1">二次图信息</param>
        internal string RemoveSubStation(ShapeInfo arg1)
        {
            try
            {
                var v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/substation/remove?id={arg1.Id}");
                return v;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        private void InitBoostStationInfos()
        {
            try
            {
                var ls = _WEBHelper.HttpGetJSON<List<ShapeInfo>>($"{_Url}/api/boost_station/");
                _BoostStationShapes = ls;
                var sis = from shape in ls select new StationInfo() { Name = shape.Name, Id = shape.Id };
                _BoostStationInfos = sis.ToList();
                _BoostStationInfos.Insert(0, new StationInfo() { Name = "全部", Id = "ALL" });
            }
            catch
            {
                _BoostStationInfos = new List<StationInfo>();
                _BoostStationShapes = new List<ShapeInfo>();
            }
        }


        /// <summary>
        /// 根据ID获取升压站信息
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        internal async Task<ShapeInfo> GetNewStationInfo(string id)
        {
            return await Task.Run(getShapeInfo);

            ShapeInfo getShapeInfo()
            {
                try
                {
                    var si = _WEBHelper.HttpGetJSON<ShapeInfo>($"{_Url}/api/boost_station/booststation?id={id}");
                    return si;
                }
                catch { }
                return null;
            }
        }

        /// <summary>
        /// 获取二次图信息
        /// </summary>
        /// <param name="subId">二次图ID</param>
        /// <returns></returns>
        internal async Task<ShapeInfo> GetSubBoostStation(string subId)
        {
            return await Task.Run(getShapeInfo);

            ShapeInfo getShapeInfo()
            {
                try
                {
                    var si = _WEBHelper.HttpGetJSON<ShapeInfo>($"{_Url}/api/boost_station/substation?id={subId}");
                    return si;
                }
                catch { }
                return null;
            }
        }


        /// <summary>
        /// 获取所有二次图
        /// </summary>
        /// <returns></returns>
        internal async Task<List<ShapeInfo>> GetSubStations()
        {
            return await Task.Run(getShapeInfo);

            List<ShapeInfo> getShapeInfo()
            {
                try
                {
                    var si = _WEBHelper.HttpGetJSON<List<ShapeInfo>>($"{_Url}/api/boost_station/substation/all");
                    return si;
                }
                catch { }
                return null;
            }
        }

        /// <summary>
        /// 获取自定义控件
        /// </summary>
        internal List<ShapeInfo> GetCustomizeShapes()
        {
            try
            {
                var ls = _WEBHelper.HttpGetJSON<List<ShapeInfo>>($"{_Url}/api/boost_station/customize");
                _CustomizeShapesDic = ls?.ToDictionary(c => c.Id, c => c);
                return ls;
            }
            catch { }
            return new List<ShapeInfo>();
        }

        internal Dictionary<string,ShapeInfo> GetCustomizeShapesDic()
        {
            if (_CustomizeShapesDic == null)
            {
                _CustomizeShapesDic = GetCustomizeShapes()?.ToDictionary(c => c.Id, c => c);
            }
            return _CustomizeShapesDic;
        }

        /// <summary>
        /// 添加升压站
        /// </summary>
        /// <param name="info">图形信息</param>
        internal string AddOrUpdateStation(ShapeInfo info, Windows.BoostStation.BoostStationEditType editType)
        {
            try
            {
                switch (editType)
                {
                    case Windows.BoostStation.BoostStationEditType.BoostStationEdit:        // 升压站编辑
                        return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/update", info);
                        case Windows.BoostStation.BoostStationEditType.BoostStationAdd:     // 升压站添加
                        return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/add", info);
                    case Windows.BoostStation.BoostStationEditType.SubStationAdd:           // 二次图添加
                        return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/substation/add", info);
                    case Windows.BoostStation.BoostStationEditType.SubStationEdit:          // 二次图编辑
                        return _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/substation/update", info);
                    default:return "操作类型错误！";
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 添加或移除自定义控件
        /// </summary>
        /// <param name="info">图形信息</param>
        /// <param name="isadd">是否添加</param>
        internal string AddOrRemoveCustomizeShape(ShapeInfo info, string type)
        {
            try
            {
                string v = "";
                switch (type)
                {
                    case "add":
                        v = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/customize/add", info);
                        break;
                    case "remove":
                        v = _WEBHelper.HttpGetString($"{_Url}/api/boost_station/customize/remove?id={info.Id}");
                        break;
                    case "edit":
                        v = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/customize/update", info);
                        break;
                    default:return v;
                }
                return v;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 将导出的svg发送到服务器
        /// </summary>
        /// <param name="svg">svg字符串</param>
        /// <param name="info">svg信息</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        internal string OutputSvgInfo(object svg, SvgInfo info)
        {
            try
            {
                var s = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/svg", svg);
                if (s != "success")
                {
                    return s;
                }
                s = _WEBHelper.HttpPostBodyString($"{_Url}/api/boost_station/svginfo", info);
                return s;
            }
            catch (Exception e)
            {
                return $"导出svg出现错误：{e.Message}";
            }
        }
    }
}
