using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CK.SCP.Models;
using CK.SCP.Models.Enums;
using CK.SCP.Utils;
using DevComponents.DotNetBar;

namespace CK.SCP.GrupUniApi
{
    public partial class FormMain : Office2007Form
    {

        private Timer _timer;
        private IApi _iApi;

        public FormMain()
        {
            InitializeComponent();

            //            StringRedir r = new StringRedir(ref textBox1);
            StringRedir r = new StringRedir(ref listLog, true);
            Console.SetOut(r);
            try
            {
                Init();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private static void InitGlobalCache()
        {
            var db = EntitiesFactory.CreateScpInstance();
            ScpCache.Refresh(db);
        }



        private void Init()
        {
            //            txtDomain.Text = ScpCache.Config.QAD域;
            //            txtSite.Text = ScpCache.Config.QAD地点;
            //            var sofewareName = $"{FileHelper.GetApplicationPath()}/{Application.ProductName}.exe";
            //            var fi = new FileInfo(sofewareName);
            //            txtVersion.Text = $@"Version {Assembly.LoadFrom(sofewareName).GetName().Version}{fi.LastWriteTime:(MMddHHmm)}";
            ProjectName projectName;
            Enum.TryParse(ScpCache.Config.项目名称, true, out projectName);

            switch (projectName)
            {
                case ProjectName.锦州锦恒:
                    _iApi = new QadOdbcApi();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
//            BtnErpGet.Visible = false;
//            BtnErpPut.Visible = false;
            BtnErpGet.Count = GlobalVar.DurationConfig.ERP接口接收;
            BtnErpPut.Count = GlobalVar.DurationConfig.ERP接口发送;

            BtnErpGet.InitCount = GlobalVar.DurationConfig.ERP接口接收;
            BtnErpPut.InitCount = GlobalVar.DurationConfig.ERP接口发送;


            _timer = new Timer { Interval = 1000 };
            _timer.Tick += _timer_Tick;
            btnStart.Enabled = true;
            btnStop.Enabled = false;

        }


        private int _refreshInterval = 10 * 60;
        private void _timer_Tick(object sender, EventArgs e)
        {
            BtnErpGet.RunOnce();
            BtnErpPut.RunOnce();
            RefreshGlobalCache();
        }

        private void RefreshGlobalCache()
        {
            if (_refreshInterval > 0)
            {
                _refreshInterval--;
            }
            else
            {
                InitGlobalCache();
                _refreshInterval = 10 * 60;
                Console.WriteLine("开始更新缓存数据!");
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            try
            {
                InitGlobalCache();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listLog.SelectedItems.Count == 0) return;
            var str = listLog.SelectedItems[0].SubItems[1].Text;
            txtLog.Text = str;
        }


        #region Button

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            Console.WriteLine(@"系统启动");
            BtnErpGet.Start();
            BtnErpPut.Start();

            _timer.Start();
            btnStart.Enabled = false;
            btnStop.Enabled = true;

        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            BtnErpGet.Stop();
            BtnErpPut.Stop();

            _timer.Stop();
            btnStart.Enabled = true;
            btnStop.Enabled = false;

            Console.WriteLine(@"系统停止");
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                InitGlobalCache();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listLog.Items.Clear();
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            try
            {
                var form = new PopupSetting();
                var dr = form.ShowDialog(this);
                if (dr != DialogResult.OK) return;
                Init();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
        }

        #endregion



        private void BtnErpGet_RunOnceExecute()
        {
            _iApi.Get();
        }

        private void BtnErpPut_RunOnceExecute()
        {
            _iApi.Put();
        }

    }
}