using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FPJ0000.Project
{
    public partial class ctlLayout : Control
    {
        public ctlLayout()
        {
            InitializeComponent();

            items = new List<itemdata>();

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // Redraw when resized
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.Resize += arLabel_Resize;
            if (fontNo == null) fontNo = new Font("맑은 고딕", 12, FontStyle.Bold);
            if (fontName == null) fontName = new Font("맑은 고딕", 10, FontStyle.Bold);
            if (fontDue == null) fontDue = new Font("Consolas", 10, FontStyle.Bold);
        }


        public class itemdata
        {
            public string duedate { get; set; }
            public string champion { get; set; }
            public string body { get; set; }
            public int no { get; set; }
            public Color bodyColor { get; set; } = Color.Transparent;
            public int project { get; set; }
            public RectangleF rect { get; set; }
            public string reserve { get; set; }

        }

        public List<itemdata> items;
        public Font fontNo { get; set; }
        public Font fontName { get; set; }
        public Font fontDue { get; set; }

        public delegate void itemclickhandler(itemdata item, MouseEventArgs e);
        public event itemclickhandler itemclick;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            var item = items.Where(t => t.rect.Contains(e.Location)).FirstOrDefault();
            if (item != null)
            {
                if (itemclick != null)
                    itemclick(item, e);
            }
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);


        }

        Boolean brect = true;
        protected override void OnPaint(PaintEventArgs pe)
        {
            if (items == null  || items.Any()==false )
            {
                return;
            }
            base.OnPaint(pe);

            //우측에 문
            //좌측에 사용자
            //중앙에 가로로 작업영역 10개
            var rectL = new RectangleF(DisplayRectangle.Left + Padding.Left,
                DisplayRectangle.Top + Padding.Top,
                DisplayRectangle.Width * 0.1f - (Padding.Left + Padding.Right),
                DisplayRectangle.Height - (Padding.Top + Padding.Bottom));

            var rectR = new RectangleF(DisplayRectangle.Left + (DisplayRectangle.Width * 0.9f) + Padding.Left,
                DisplayRectangle.Top + Padding.Top,
                DisplayRectangle.Width * 0.1f - (Padding.Left + Padding.Right),
                DisplayRectangle.Height - (Padding.Top + Padding.Bottom));

            var lw = DisplayRectangle.Width * 0.1f;
            var rect = new RectangleF(DisplayRectangle.Left + Padding.Left + lw + Padding.Left,
                DisplayRectangle.Top + Padding.Top,
                DisplayRectangle.Width - (Padding.Left + Padding.Right) * 2 - lw * 2,
                rectR.Height);

            pe.Graphics.DrawRectangle(Pens.White, rectL.Left, rectL.Top, rectL.Width, rectL.Height);
            pe.Graphics.DrawRectangle(Pens.Gold, rectR.Left, rectR.Top, rectR.Width, rectR.Height);
            //pe.Graphics.DrawRectangle(new Pen(Color.Pink,5), rect.Left, rect.Top, rect.Width, rect.Height);

            pe.Graphics.DrawString("DESK", this.Font, Brushes.White, rectL, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }); ;
            pe.Graphics.DrawString("DOOR", this.Font, Brushes.White, rectR, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }); ;

            //var fNo = new Font("Consolas", 20, FontStyle.Bold);
            //내부그리드 그리기 2*5 배열이나.. 1칸이 총 2x2 칸으로 구성되니 2배로 한다.
            var cnt = 1;
            var itemp = 12;
            var colcount = 4;
            var rowcount = 3;
            var itemw = (rect.Width / colcount) - (itemp * (colcount - 1));
            var itemh = (rect.Height / rowcount) - (itemp * (rowcount - 1));
            for (int i = 0; i < colcount; i++)
            {
        //        if (i >= 11) continue;
                for (int j = 0; j < rowcount; j++)
                {
                    var r = new RectangleF(
                        rect.Left + (i * itemw) + ((i + 1) * itemp),
                        rect.Top + (j * itemh) + ((j + 1) * itemp),
                        itemw,
                        itemh);

                    if (cnt >= items.Count) continue;

                    var data = items[cnt - 1];

                    var no = j * colcount + i + 1;
                    if (cnt >= 12) continue;
                 
                    if(no ==11)
                    {
                        cnt += 1;
                        continue;
                    }
                    pe.Graphics.DrawRectangle(Pens.White, r.Left, r.Top, r.Width, r.Height);

                    var rno = new RectangleF(r.Left, r.Top, 50f, 50f);
                    var rnm = new RectangleF(rno.Right + 5, r.Top, r.Width - rno.Width - 5, rno.Height);

                    if (string.IsNullOrEmpty(data.reserve))
                    {
                        var rdu = new RectangleF(r.Left, r.Bottom - 50f, r.Width, 50f);
                        var rbd = new RectangleF(r.Left + 5, rno.Bottom + 5, r.Width - 5 * 2, r.Height - rno.Height - 10 - rdu.Height);

                        pe.Graphics.FillRectangle(Brushes.White, rno);
                        pe.Graphics.DrawRectangle(Pens.White, rno.Left, rno.Top, rno.Width, rno.Height);
                        pe.Graphics.DrawString(no.ToString(), fontNo, Brushes.Black, rno, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        pe.Graphics.DrawRectangle(Pens.White, rnm.Left, rnm.Top, rnm.Width, rnm.Height);
                        pe.Graphics.DrawString(data.champion, fontName, Brushes.White, rnm, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        //pe.Graphics.DrawRectangle(Pens.White, rbd.Left, rbd.Top, rbd.Width, rbd.Height);

                        data.rect = r;

                        pe.Graphics.FillRectangle(new SolidBrush(data.bodyColor), rdu);
                        if (string.IsNullOrEmpty(data.duedate))
                            pe.Graphics.DrawString("만료일없음", fontDue, Brushes.White, rdu, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        else
                            pe.Graphics.DrawString(data.duedate, fontDue, Brushes.White, rdu, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        pe.Graphics.DrawRectangle(Pens.White, rdu.Left, rdu.Top, rdu.Width, rdu.Height);



                        pe.Graphics.DrawString(data.body, this.Font, new SolidBrush(this.ForeColor), rbd, new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near });

                    }
                    else
                    {
                        var rdu = new RectangleF(r.Left, r.Bottom - 105f, r.Width, 50f);
                        var rbd = new RectangleF(r.Left + 5, rno.Bottom + 5, r.Width - 5 * 2, r.Height - rno.Height - 10 - rdu.Height - 50);
                        var rrv = new RectangleF(r.Left + 5, rdu.Bottom + 5, rdu.Width - 10, 50f - 5);

                        pe.Graphics.FillRectangle(Brushes.White, rno);
                        pe.Graphics.DrawRectangle(Pens.White, rno.Left, rno.Top, rno.Width, rno.Height);
                        pe.Graphics.DrawString(no.ToString(), fontNo, Brushes.Black, rno, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        pe.Graphics.DrawRectangle(Pens.White, rnm.Left, rnm.Top, rnm.Width, rnm.Height);
                        pe.Graphics.DrawString(data.champion, fontName, Brushes.White, rnm, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        //pe.Graphics.DrawRectangle(Pens.White, rbd.Left, rbd.Top, rbd.Width, rbd.Height);

                        data.rect = r;

                        pe.Graphics.FillRectangle(new SolidBrush(data.bodyColor), rdu);
                        if (string.IsNullOrEmpty( data.duedate ))
                            pe.Graphics.DrawString("만료일없음", fontDue, Brushes.White, rdu, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        else
                            pe.Graphics.DrawString(data.duedate, fontDue, Brushes.White, rdu, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        pe.Graphics.DrawRectangle(Pens.White, rdu.Left, rdu.Top, rdu.Width, rdu.Height);
                        pe.Graphics.DrawString(data.body, this.Font, new SolidBrush(this.ForeColor), rbd, new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near });

                        pe.Graphics.FillRectangle(Brushes.SkyBlue, rrv);
                        pe.Graphics.DrawString("다음예약 => " + data.reserve, fontNo, Brushes.Black, rrv, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        pe.Graphics.DrawRectangle(Pens.DimGray, rrv.Left, rrv.Top, rrv.Width, rrv.Height);


                    }




                    cnt += 1;

                }
            }
            // fNo.Dispose();
            if (brect) brect = false;
        }



        void arLabel_Resize(object sender, EventArgs e)
        {
            brect = true;
            Invalidate();
        }
    }
}
