using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Project
{
    public static class MessageWindow
    {
        public enum eWindowPosition
        {
            center = 0,
            top = 1,
            bottom = 2
        }
        public enum eWindowType
        {
            information,
            error,
            attention,
        }
        public enum eMsgNo : byte
        {
            Normal = 0,
            RunMsg,
            DemoMsg,
            ErrorMsg,
            EmgButton,  //171214
            RobotStop,//171214
            RetryMsg,
            RemoteMsg,
        }
        private static Dictionary<eMsgNo, Dialog.fMsgWindow> msgBuffer = new Dictionary<eMsgNo, Dialog.fMsgWindow>();
        public static void ShowMsg(string m, eMsgNo id, int width = 1200, int height = 300, eWindowType msgtype = eWindowType.information, Boolean useClose = true, eWindowPosition position = eWindowPosition.center)
        {
            var pform = findForm(id);
            if (pform == null)
            {
                var f = new Dialog.fMsgWindow(m);
                f.TopMost = true;
                f.ID = id;
                f.Disposed += (s1, e1) => { CloseMsg(id); };
                f.Width = width;
                f.Height = height;

                if (msgtype == eWindowType.attention)
                {
                    f.BackColor = Color.Orange;
                    f.arLabel1.ForeColor = Color.Orange;
                }
                else if (msgtype == eWindowType.error)
                {
                    f.BackColor = Color.Red;
                    f.arLabel1.ForeColor = Color.Red;
                }
                else
                {
                    f.BackColor = Color.Blue;
                    f.arLabel1.ForeColor = Color.DeepSkyBlue;
                }

                if (!useClose) f.EnableUserClose = false;
                if (position == eWindowPosition.center)
                    f.StartPosition = FormStartPosition.CenterScreen;
                else if (position == eWindowPosition.top)
                {
                    f.StartPosition = FormStartPosition.Manual;
                    f.Left = (Screen.PrimaryScreen.Bounds.Width - f.Width) / 2;
                    f.Top = 0;
                }
                else if (position == eWindowPosition.bottom)
                {
                    f.StartPosition = FormStartPosition.Manual;
                    f.Left = (Screen.PrimaryScreen.Bounds.Width - f.Width) / 2;
                    f.Top = Screen.PrimaryScreen.Bounds.Height - f.Height;
                }

                msgBuffer.Add(id, f);
                f.Show();
            }
            else
            {
                pform.SetText(m);
                if (pform.WindowState == FormWindowState.Minimized) pform.WindowState = FormWindowState.Normal;
                if (!pform.Visible) pform.Visible = true;
            }
        }

        private static Dialog.fMsgWindow findForm(eMsgNo id)
        {
            var items = msgBuffer.Where(t => t.Key == id);
            if (items.Count() < 1) return null;
            return items.First().Value;
        }

        public static Boolean ExistForm(eMsgNo id)
        {
            var items = msgBuffer.Where(t => t.Key == id);
            if (items.Count() < 1) return false;
            else return true;
        }

        public delegate void VisibleHandler(Boolean value);
        public static void VisibleAll(Boolean value)
        {
            if (msgBuffer.Count < 1) return;

            #region "invoke"
            if (msgBuffer.ElementAt(0).Value.InvokeRequired)
            {
                msgBuffer.ElementAt(0).Value.BeginInvoke(new VisibleHandler(VisibleAll), new object[] { value });
                return;
            }
            #endregion

            foreach (var form in msgBuffer)
            {
                form.Value.Visible = value;
            }
        }

        public static void CloseAll()
        {

            for (int i = msgBuffer.Count; i > 0; i--)
            {
                var form = msgBuffer.ElementAt(i - 1);
                form.Value.Close();
            }
            msgBuffer.Clear();
        }
        public static void CloseMsg(eMsgNo id)
        {
            var pform = findForm(id);
            if (pform != null)
            {
                pform.Close();
                msgBuffer.Remove(id);
            }
        }
    }

}
