using System;
using System.Windows.Forms;

namespace AIProofread.Controls
{
    public partial class FormLogger : Form
    {
        private ListView LogListView = new ListView();

        public FormLogger()
        {
            InitializeComponent();

            
            //LogListView.View = View.Details;
            //LogListView.FullRowSelect = true;
            //LogListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

        }

        private void InitLogListView()
        {
            var columnHeader1 = new ColumnHeader();
            var columnHeader2 = new ColumnHeader();
            var columnHeader3 = new ColumnHeader();

            columnHeader1.Text = "Tag";
            columnHeader1.Width = 67;
            columnHeader2.Text = "Time";
            columnHeader2.Width = 117;
            columnHeader3.Text = "Message";
            columnHeader3.Width = 480;


            LogListView.Columns.AddRange(new ColumnHeader[] {columnHeader1,columnHeader2,columnHeader3});
        }

        /// <summary>
        /// 显示最大日志数
        /// </summary>
        private const int MaxDisplayItemCount = 100;

        public void Log(string time, string tag, string message)
        {
            ShowLogInText(time, tag, message);
        }

        private void ShowLogInText(string time, string tag, string message)
        {
            if (LogText.InvokeRequired)
            {
                //LogText.Invoke(new AppendLogDel(AppendLog), args);
            }
            else
            {
                LogText.AppendText(string.Format("{0} {1} {2}",time,tag,message));
                LogText.AppendText(Environment.NewLine);
                LogText.ScrollToCaret();
                LogText.Update();
            }
        }

        private void ShowLogInList(string time, string tag, string message)
        {
            if (LogListView.InvokeRequired)
            {
                LogListView.Invoke(new Action(() => AddLogToList(time, tag, message)));
            }
            else
            {
                AddLogToList(time, tag, message);
            }
        }

        private void AddLogToList(string time, string tag, string message)
        {
            //
            ListViewItem insertItem = new ListViewItem(new string[] { tag, time, message });
            // 
            //insertItem.SubItems.Add(time);
            //insertItem.SubItems.Add(tag, Color.Violet, Color.White, LogListView.Font);
            //insertItem.SubItems.Add(message, Color.Blue, Color.White, LogListView.Font);
            //ListViewSubItem tagItem = new ListViewSubItem(insertItem,tag);
            //ListViewSubItem timeItem = new ListViewSubItem(insertItem,time, Color.Violet, Color.White, LogListView.Font);
            //ListViewSubItem messageItem = new ListViewSubItem(insertItem, time, Color.Blue, Color.White, LogListView.Font);

            //insertItem.SubItems.Add(tagItem);
            if (LogListView.Items.Count > MaxDisplayItemCount)
            {
                LogListView.Items.RemoveAt(MaxDisplayItemCount);
            }
            //ListViewItem lstItem = new ListViewItem(" " + DateTime.Now.ToString(), imageIndex);
            //lstItem.SubItems.Add(info);
            LogListView.Items.Insert(0, insertItem);
        }

        private void FormLogger_FormClosed(object sender, FormClosedEventArgs e)
        {
            Logger.LoggerForm = null;
        }

        private void TsmiClear_Click(object sender, EventArgs e)
        {
            LogText.Text = string.Empty;
        }
    }
}
