using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace Project
{
    public partial class MessageWindow
    {
        public enum eWindowPosition
        {
            center = 0,
            top = 1,
            bottom = 2
        }
        public enum eWindowType
        {
            information,
            error,
            attention,
        }
        fMsgWindow msgwin;
        public MessageWindow()
        {
            msgwin = new fMsgWindow("");
            msgwin.VisibleChanged += msgwin_VisibleChanged;
        }
        public bool needClose = false;
        public Boolean needShow = false;
        private CMessageData msgBuffer = new CMessageData();
        public void setMessage(CMessageData msgData)
        {
            //마지막데잍와 동일하고 버퍼에 데이터가 있다면 처리하지 않는다.
            if (msgBuffer == msgData) return;
            //신규 메세지를 추가
            msgBuffer = msgData;
            //화면이 표시되어야하므로 플래그 설정
            needShow = true;
        }
         public void setMessage(string msg,string tag,
            eWindowType winType = eWindowType.error,
            int width = 900, int height = 500,
            Boolean enbClose = true,
            Font fontTitle = null,
            Font fontBody = null)
        {
            setMessage(new CMessageData()
            {
                Tag = tag,
                EnableClose = enbClose,
                FontContent = fontBody,
                FontTitle = fontTitle,
                Message = msg,
                WindowSize = new Size(width, height),
                WindowType = winType
            });
        }
        public void setMessage(string msg,
            eWindowType winType = eWindowType.error,
            int width = 900, int height = 500,
            Boolean enbClose = true,
            Font fontTitle = null,
            Font fontBody = null)
        {
            setMessage(new CMessageData()
            {
                EnableClose = enbClose,
                FontContent = fontBody,
                FontTitle = fontTitle,
                Message = msg,
                WindowSize = new Size(width, height),
                WindowType = winType
            });
        }
        /// 
        /// 메세지 버퍼의 내용을 폼으로 표시합니다.
        /// UI 쓰레드를 사용하므로 invoke 를 이용해 호출 하거나
        /// UI 쓰레드내에서 실행 하시기 바랍니다.
        /// 
        public void showMessage()
        {
            //개체가 없어졌다면 새로생성해서 사용한다
            if (msgwin == null || msgwin.Disposing || msgwin.IsDisposed)
            {
                msgwin = new fMsgWindow("");
                msgwin.VisibleChanged += msgwin_VisibleChanged;
            }
            //등록된 메세지 버퍼의 내용을 화면에 표시한다.
            msgwin.TopMost = true;
            // msgwin.Disposed += (s1, e1) => { CloseMsg(item.Key); };
            msgwin.Width = this.msgBuffer.WindowSize.Width;
            msgwin.Height = msgBuffer.WindowSize.Height;
            msgwin.BackColor = Color.FromArgb(200, 200, 200);
            msgwin.StartPosition = FormStartPosition.CenterScreen;
            msgwin.setMessage(msgBuffer.Message);
            //set font
            if (msgBuffer.FontTitle != null) msgwin.lbTitle.Font = msgBuffer.FontTitle;
            if (msgBuffer.FontContent != null)
            {
                msgwin.lb1.Font = msgBuffer.FontContent;
                msgwin.lb2.Font = msgBuffer.FontContent;
                msgwin.lb3.Font = msgBuffer.FontContent;
                msgwin.lb4.Font = msgBuffer.FontContent;
                msgwin.lb5.Font = msgBuffer.FontContent;
                msgwin.lb6.Font = msgBuffer.FontContent;
                msgwin.lb7.Font = msgBuffer.FontContent;
            }
            switch (msgBuffer.WindowType)
            {
                case eWindowType.attention:
                    msgwin.lbTitle.BackColor = Color.Gold;
                    msgwin.lbTitle.BackColor2 = Color.Khaki;
                    msgwin.lbTitle.ShadowColor = Color.FromArgb(150, 150, 150);
                    msgwin.lbTitle.ForeColor = Color.FromArgb(50, 50, 50);
                    break;
                case eWindowType.error:
                    msgwin.lbTitle.BackColor = Color.Tomato;
                    msgwin.lbTitle.BackColor2 = Color.Red;
                    msgwin.lbTitle.ShadowColor = Color.FromArgb(50, 50, 50);
                    msgwin.lbTitle.ForeColor = Color.WhiteSmoke;
                    break;
                default:
                    msgwin.lbTitle.BackColor = Color.DarkTurquoise;
                    msgwin.lbTitle.BackColor2 = Color.LightSkyBlue;
                    msgwin.lbTitle.ShadowColor = Color.FromArgb(50, 50, 50);
                    msgwin.lbTitle.ForeColor = Color.WhiteSmoke;
                    break;
            }
            if (!msgBuffer.EnableClose) msgwin.EnableUserClose = false;
            if (msgwin.Visible == false) msgwin.Show();
            else
            {
                msgwin.Visible = true;
                msgwin.Activate();
            }
            needShow = false;
            needClose = false; //닫히게되어잇다면 그것을 off해줘야 다시 닫히지 않음 200923
        }
        
        /// 
        /// sende는 CMessageData
        /// 
        public event EventHandler WindowClose;
        /// 
        /// sender는 CMessageData
        /// 
        public event EventHandler WindowOpen;
        void msgwin_VisibleChanged(object sender, EventArgs e)
        {
            var f = sender as Form;
            if (f.Visible == false)
            {
                if (WindowClose != null) WindowClose(msgBuffer, new EventArgs());
            }
            else
            {
                if (WindowOpen != null) WindowOpen(msgBuffer, new EventArgs());
            }
        }
        
        public Boolean Visible
        {
            get
            {
                if (msgwin == null || msgwin.Disposing || msgwin.IsDisposed) return false;
                return msgwin.Visible;
            }
            set
            {
                if (msgwin == null || msgwin.Disposing || msgwin.IsDisposed)
                {
                    needShow = false;
                    needClose = false;
                    return;
                }
                if (value == true)
                {
                    if (msgwin.Visible == false)
                    {
                        msgwin.Show();
                        msgwin.Activate();
                    }
                    else
                    {
                        msgwin.Activate();
                    }
                    needShow = false;
                }
                else
                {
                    msgwin.Visible = false;
                    needClose = false;
                }
            }
        }
    }
}