using System;
using System.Drawing;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    public sealed partial class FrmLogConsole : Form
    {
        //Source code: https://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
        private static bool _gIsShown;

        public FrmLogConsole()
        {
            InitializeComponent();

            logListView.Scrollable = true;
            logListView.View = View.Details;
            //Information for drawing header columns and sub-items in ListView:
            //http://stackoverflow.com/questions/561798/how-do-i-align-text-for-a-single-subitem-in-a-listview-using-c
            var header = new ColumnHeader
            {
                Text = @"Advertising Profit Control " + Application.ProductVersion + @" Debug Console",
                Name = "LogConsoleHeader",
                Width = logListView.Width
            };
            logListView.Columns.Add(header);
        }

        static FrmLogConsole()
        {
            GetStaticInstance.FormClosing += LogConsole_FormClosing;
            //Set the maximum and minimum size of the form.
            GetStaticInstance.MaximumSize = new Size(900, 900);
            GetStaticInstance.MinimumSize = new Size(400, 400);
        }

        public new void Show()
        {
            if (_gIsShown)
            {
                base.Show();
            }
            else
            {
                base.Show();
                _gIsShown = true;
            }
        }

        public new void Hide()
        {
            if (!_gIsShown) return;
            base.Hide();
            _gIsShown = false;
        }

        public enum Level
        {
            Critical = 0,
            Error = 1,
            Warning = 2,
            Info = 3,
            Verbose = 4,
            Debug = 5
        };

        public void WriteToLog(Level level, string message)
        {
            Color color;

            switch (level)
            {
                case Level.Critical:
                    color = Color.White;
                    break;
                case Level.Error:
                    color = Color.Red;
                    break;
                case Level.Warning:
                    color = Color.Goldenrod;
                    break;
                case Level.Info:
                    color = Color.Green;
                    break;
                case Level.Verbose:
                    color = Color.Blue;
                    break;
                case Level.Debug:
                    color = Color.Black;
                    break;
                default:
                    color = Color.Black;
                    break;
            }
            var index = logListView.Items.Count;
            try
            {
                message = $"{level}: {message}";
                if (level != Level.Info)
                {
                    GlobalClasses.WriteToLog(DateTime.Now + ": " + message + Environment.NewLine);   
                }
                logListView.Items.Add(message);
                logListView.Items[index].ForeColor = color;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            switch (level)
            {
                case Level.Critical:
                    logListView.Items[index].BackColor = Color.Red;
                    break;
                case Level.Error:
                    logListView.Items[index].ForeColor = Color.Maroon;
                    break;
                default:
                    logListView.Items[index].BackColor = Color.WhiteSmoke;
                    break;
            }
        }

        private static void LogConsole_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            GetStaticInstance.Hide();
            _gIsShown = false;
        }

        public static FrmLogConsole GetStaticInstance { get; } = new FrmLogConsole();

        public bool IsVisable => _gIsShown;
    }
}
