using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using VIZ.Framework.Core;
using log4net;

namespace VIZ.TVP.CBA.Module
{
    public class MainViewModel : ViewModelBase
    {

        private static ILog Log= LogManager.GetLogger(typeof(MainViewModel));   

        private static  MainViewModel _createInstance = null;

        public static MainViewModel CreateInstance
        {
            get
            {
                if(null==_createInstance)
                {
                    _createInstance = new MainViewModel();
                }
                return _createInstance;
            }
        }


       // public OnAirDataModel onAirDataModel = new OnAirDataModel();

        public MainViewModel()
        {
            //matchDate = DateTime.Today.ToShortDateString();
          //  ApplicationDomainEx.VizEngineModel = new VizEngineModel();
           // GetSchedule();
           // InitCommand();
            InitProperty();




        }

    //    private string matchDate;

    //    public string MatchDate
    //    {
    //        get { return matchDate; }
    //        set { matchDate = value; this.RaisePropertyChanged(nameof(MatchDate)); }

    //    }


    //    private string matchStatus;

    //    public string MatchStatus
    //    {
    //        get { return matchStatus; }
    //        set { matchStatus = value;this.RaisePropertyChanged(nameof(MatchStatus)); }
    //    }


    //    private ObservableCollection<string> matchItems;

    //    /// <summary>
    //    /// 下拉框绑定得赛程详细信息
    //    /// </summary>
    //    public ObservableCollection<string> MatchItems
    //    {
    //        get { return matchItems; }
    //        set { matchItems = value; this.RaisePropertyChanged(nameof(MatchItems)); }
    //    }



    //    private string selectMatchItems;

    //    /// <summary>
    //    /// 选中得赛程信息
    //    /// </summary>
    //    public string SelectMatchItems
    //    {
    //        get { return selectMatchItems; }
    //        set { selectMatchItems = value; this.RaisePropertyChanged(nameof(SelectMatchItems)); }  
    //    }

    //    /// <summary>
    //    /// 球员赛季数据
    //    /// </summary>
    //    public PlayerSeasonData PlayerSeasonData = new PlayerSeasonData();

    //    /// <summary>
    //    /// 队伍赛程信息
    //    /// </summary>
    //    public TeamSeasonData TeamSeasonData = new TeamSeasonData();


    //    //向字典中个人选手增加整个赛季的分数值
    //    public Dictionary<string, Dictionary<string, string>> CNAliasPlayerSportsDictionary
    //        = new Dictionary<string, Dictionary<string, string>>();

    //    /// <summary>
    //    /// 主队球员技术统计数据
    //    /// </summary>
    //  public  Dictionary<string, Dictionary<string, string>> MvpHomePlayerCompareNumDictionary
    //= new Dictionary<string, Dictionary<string, string>>();

    //    /// <summary>
    //    /// 客队球员技术统计数据
    //    /// </summary>
    //  public  Dictionary<string, Dictionary<string, string>> MvpVisitPlayerCompareNumDictionary
    //        = new Dictionary<string, Dictionary<string, string>>();



    //    //创建【球队】数据对比时的字典---主队球员数据与对比项的一一对应【主队-例如:得分--读取接口得分数据】
    //  public  Dictionary<string, string> HomeTeamCompareOptionDictionary = new Dictionary<string, string>();
    //    //创建【球队】数据对比时的字典---客队球员数据与对比项的一一对应【客队队-例如:得分--读取接口得分数据】
    //  public   Dictionary<string, string> VisitTeamCompareOptionDictionary = new Dictionary<string, string>();


    //  public  Dictionary<string, Dictionary<string, string>> CNAliasVisitSportsDictionary
    //= new Dictionary<string, Dictionary<string, string>>();

    //    //球员赛季数据
    //    public string Matchtypeid = "";
    //    //比赛赛程Id
    //    public string ScheduleID = "";

    //    /// <summary>
    //    /// 设置一个比赛赛程集合
    //    /// </summary>
    //    public Dictionary<string, string> SetMatchDict = new Dictionary<string, string>();

    //    VizEngineModel gobalVizEngineModel = new VizEngineModel();

    //    /// <summary>
    //    /// 获取比赛赛程
    //    /// </summary>

    //    public void GetSchedule()
    //    {
    //        MatchItems = new ObservableCollection<string>();
    //        string date = Convert.ToDateTime(matchDate).ToString("yyyy-MM-dd");

    //        var dayschedules = JsonModel.TomorrowMatch_Path(date);

    //        if (dayschedules == null) return;

    //        // matchData.AddRange(dayschedules.dayschedule);
    //        //MatchData = new ObservableCollection<Dayschedule>();

    //        //if(dayschedules!=null)
    //        //{
    //            dayschedules.dayschedule = dayschedules.dayschedule.OrderBy(a => Convert.ToDateTime(a.time)).ToList();
    //        //}

    //        SetMatchDict.Clear();
    //        MatchItems.Clear();

    //        foreach (var dayschedule in dayschedules.dayschedule)
    //        {
    //            if (dayschedule.Status == "1")
    //            {
    //                dayschedule.Status = "未开赛";
    //            }
    //            else if (dayschedule.Status == "2")
    //            {
    //                dayschedule.Status = "进行中";
    //            }
    //            else if (dayschedule.Status == "3")
    //            {
    //                dayschedule.Status = "进行中";
    //            }
    //            else if (dayschedule.Status == "4")
    //            {
    //                dayschedule.Status = "已结束";
    //            }
    //            else if (dayschedule.Status == "5")
    //            {
    //                dayschedule.Status = "延期";
    //            }

    //            if (!SetMatchDict.ContainsKey(dayschedule.time + dayschedule.HomeTeamName
    //                        + "VS" + dayschedule.VisitingTeamName))
    //            {
    //                SetMatchDict.Add(dayschedule.time + dayschedule.HomeTeamName
    //                            + "VS" + dayschedule.VisitingTeamName, dayschedule.ScheduleID);
    //            }

    //            Matchtypeid = dayschedule.ScheduleTypeID;

    //            MatchItems.Add(dayschedule.time + dayschedule.HomeTeamName
    //                        + "VS" + dayschedule.VisitingTeamName);

    //        }

    //    }


    //    /// <summary>
    //    /// 初始化命令
    //    /// </summary>
    //    private void InitCommand()
    //    {
    //        this.ConnectCommand = new VCommand(this.Connect);
    //        this.DisconnectCommand = new VCommand(this.Disconnect);
    //    }

        /// <summary>
        /// 初始化属性
        /// </summary>
        private void InitProperty()
        {
            ObservableCollection<VizEngineConnectionItem> items = new ObservableCollection<VizEngineConnectionItem>();
            string ip1 = "127.0.0.1";
            int port1 =6100;

            items.Add(new VizEngineConnectionItem(ip1, port1));

            string ip2 ="127.0.1.1";
            int port2 = 6100;

            items.Add(new VizEngineConnectionItem(ip2, port2));

            this.VizConnections = items;
            this.SelectedVizConnection = items[0];
        }


        #region IsConnectButtonEnabled -- 连接按钮是否可用

        private bool isConnectButtonEnabled = true;
        /// <summary>
        /// 连接按钮是否可用
        /// </summary>
        public bool IsConnectButtonEnabled
        {
            get { return isConnectButtonEnabled; }
            set { isConnectButtonEnabled = value; this.RaisePropertySaveChanged(nameof(IsConnectButtonEnabled)); }
        }

        #endregion

        #region VizConnections -- VIZ 连接集合

        private ObservableCollection<VizEngineConnectionItem> vizConnections;
        /// <summary>
        /// VIZ 连接集合
        /// </summary>
        public ObservableCollection<VizEngineConnectionItem> VizConnections
        {
            get { return vizConnections; }
            set { vizConnections = value; }
        }


        #endregion

        #region SelectedVizConnection -- 当前选中的VIZ连接

        private VizEngineConnectionItem selectedVizConnection;
        /// <summary>
        /// 当前选中的VIZ连接
        /// </summary>
        public VizEngineConnectionItem SelectedVizConnection
        {
            get { return selectedVizConnection; }
            set { selectedVizConnection = value; this.RaisePropertyChanged(nameof(SelectedVizConnection)); }
        }

        #endregion

        #region VizEngineModel -- VIZ引擎模型

        private VizEngineModel vizEngineModel = null;
            //ApplicationDomainEx.VizEngineModel;
        /// <summary>
        /// VIZ引擎模型
        /// </summary>
        public VizEngineModel VizEngineModel
        {
            get { return vizEngineModel; }
            set { vizEngineModel = value; this.RaisePropertyChanged(nameof(VizEngineModel)); }
        }


        private bool isConnected = false;


        public bool IsConnected
        {
            get { return isConnected; }

            set { isConnected = value;this.RaisePropertyChanged(nameof(IsConnected)); } 
        }


        #endregion

        // ===================================================================================
        // Command
        // ===================================================================================

        #region ConnectCommand -- 连接命令

        /// <summary>
        /// 连接命令
        /// </summary>
        public VCommand ConnectCommand { get; set; }

        /// <summary>
        /// 连接
        /// </summary>
        private void Connect()
        {
            this.IsConnectButtonEnabled = false;

            Task.Run(() =>
            {
            //    bool result = ApplicationDomainEx.VizEngineModel.Connection(this.SelectedVizConnection.IP, this.SelectedVizConnection.Port);
            //    IsConnected = ApplicationDomainEx.VizEngineModel.IsConnected;

            //    if (!result)
            //    {
            //        MessageBox.Show($"连接 {this.SelectedVizConnection?.IP}:{this.SelectedVizConnection?.Port} 失败");
            //    }

                this.IsConnectButtonEnabled = true;
            });
        }

        #endregion

        #region DisconnectCommand -- 断开命令

        /// <summary>
        /// 断开命令
        /// </summary>
        public VCommand DisconnectCommand { get; set; }

        /// <summary>
        /// 断开连接
        /// </summary>
        private void Disconnect()
        {
            //ApplicationDomainEx.VizEngineModel.Disconnect();
            //IsConnected = ApplicationDomainEx.VizEngineModel.IsConnected;
        }

        #endregion

    }
}
