using Itrycn_Project2;
using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Itrycn_Project2
{
    public partial class FrmScan : RySkins.SKinForm
    {
        /// <summary>
        /// 最大日志数量
        /// </summary>
        private int MaxLogsCount { get; set; } = 1000;
        public FrmScan()
        {
            InitializeComponent();
            //配置软件信息
            OlvGameName.AspectGetter = delegate (object x) { return ((ItemInfo)x).Name; };
            OlvAddTime.AspectGetter = delegate (object x) { return ((ItemInfo)x).AddTime.ToDateTimeStr(); };
            OlvLogTime.AspectGetter = delegate (object x) { return ((LogInfo)x).LogTime.ToDateTimeStr(); };
            OlvLogText.AspectGetter = delegate (object x) { return ((LogInfo)x).LogText; };
            LvLogs.FormatRow += delegate (object x, BrightIdeasSoftware.FormatRowEventArgs e) {
                LogInfo item = (LogInfo)e.Model;
                if (item.Color != Color.Black)
                {
                    e.Item.ForeColor = item.Color;
                }
            };
        }
        private void FrmStart_Shown(object sender, EventArgs e)
        {
        }

        private void FrmStart_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void FrmStart_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="log"></param>
        public void AddLog(string log)
        {
            AddLog(log, "", Color.Black);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="log"></param>
        /// <param name="color"></param>
        public void AddLog(string log, Color color)
        {
            AddLog(log, "", color);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="log"></param>
        /// <param name="tag"></param>
        /// <param name="color"></param>
        public void AddLog(string log, string tag, Color color)
        {
            try
            {
                this.Invoke(new Action(() =>
                {
                    var count = LvLogs.GetItemCount();
                    if (count > MaxLogsCount)
                    {
                        LvLogs.RemoveObject(LvLogs.GetModelObject(count - 1));
                    }
                    LvLogs.InsertObjects(0, new LogInfo[] { new LogInfo() { LogTime = DateTime.Now, LogText = log, LogTag = tag, Color = color } });
                    OlvLogTime.Text = "时间(" + LvLogs.GetItemCount() + ")";
                }));
            }
            catch { }
        }
        /// <summary>
        /// 显示状态
        /// </summary>
        /// <param name="state"></param>
        private void ShowState(string state)
        {
            this.Invoke(new Action(() =>
            {
                TsState.Text = "状态:" + state; 
            }));
        }
        bool IsRunning = false;
#pragma warning disable CS0414 // 字段“FrmScan.IsExit”已被赋值，但从未使用过它的值
        bool IsExit = false;
#pragma warning restore CS0414 // 字段“FrmScan.IsExit”已被赋值，但从未使用过它的值
        private void BtnScan_Click(object sender, EventArgs e)
        {
            if (IsRunning) { IsExit = true; return; }
            IsRunning = true;
            BtnScan.Text = "停止";
            LvCbResult.ClearObjects();
            LvLogs.ClearObjects();
            TabLogs.Text = "日志(" + LvLogs.GetItemCount() + ")";
            TsState.Text = "状态:正在加载数据...";
            OlvGameName.Text = "游戏名[" + LvCbResult.GetItemCount() + "]";
            Thread th = new Thread(Start);
            th.Start();
            void Start()
            {
                List<ItemInfo> list = new List<ItemInfo>();
                //RyWeb.QuickWeb ry3h3 = new RyWeb.QuickWeb();
                //将扫描的代码写在下面

                //↑将扫描的代码写在上面
                IsExit = false;
                IsRunning = false;
                this.Invoke(new Action(() =>
                {
                    //LvCbResult.AddObjects(list);
                    OlvGameName.Text = "游戏名[" + LvCbResult.GetItemCount() + "]";
                    TsState.Text = "状态:扫描完毕,共发现" + LvCbResult.GetItemCount() + "个.";
                    BtnScan.Text = "扫描";
                }));
            }
        }
        private void 复制日志信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LvLogs.SelectedObject == null) { return; }
            var item = (LogInfo)LvLogs.SelectedObject;
            RyFiles.CopyToClip(item.LogText);
        }
        /// <summary>
        /// 日志信息
        /// </summary>
        class LogInfo
        {
            /// <summary>
            /// 日志时间
            /// </summary>
            public DateTime LogTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 日志内容
            /// </summary>
            public string LogText { get; set; }
            /// <summary>
            /// 日志Tag
            /// </summary>
            public string LogTag { get; set; }
            /// <summary>
            /// 日志文字颜色
            /// </summary>
            public Color Color { get; set; } = Color.Black;
        }
        class ItemInfo
        {
            public string Name { get; set; } = "";
            public DateTime AddTime { get; set; }
        }
        private void 导出日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LvLogs.CopyObjectsToClipboard();
        }
    }
}
