using System;
using System.Collections.Generic;

namespace Model
{
    public class Log
    {
        private readonly log4net.ILog LOG;
        private List<string> history;
        private const int MAX_LINES = 50;

        public Log(string name)
        {
            LOG = log4net.LogManager.GetLogger(name);
            history = new List<string>();
        }

        public System.Windows.Forms.TextBox LogBox { set; get; }

        public void Info(string s)
        {
            LOG.Info(s);
        }

        public void Debug(string s)
        {
            LOG.Debug(s);
        }

        public void Warn(string s)
        {
            LOG.Warn(s);
        }

        public void Error(string s, Exception ex)
        {
            LOG.Error(s, ex);
        }

        public void UI_Display(string s)
        {
            if (LogBox == null) return;
            string text = string.Format("[{0:HH:mm:ss}] {1}", DateTime.Now, s);
            if (history.Count >= MAX_LINES)
                history.RemoveAt(0);
            history.Add(text);

            LogBox.Invoke(new Action(() =>
            {
                LogBox.Text = string.Join("\r\n", history);
                LogBox.ScrollToCaret();
            }));

        }

        public void UI_Clear()
        {
            if (LogBox == null) return;
            history.Clear();
            LogBox.Invoke(new Action(() => { LogBox.Text = ""; }));
        }

    }
}