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

namespace UIControl
{
	public partial class PrintDirection : UserControl
	{
		public PrintDirection()
		{
			InitializeComponent();
			// Set Optimized Double Buffer to reduce flickering
			this.SetStyle(ControlStyles.UserPaint, true);
			this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.SetStyle(ControlStyles.ContainerControl, false);
			this.SetStyle(ControlStyles.Selectable, true);
			this.Resize += Loader_Resize;
			BorderColor = Color.Black;
		}
		[DisplayName("AR_TITLEFONT")]
		public Font TitleFont { get; set; }
		Boolean bRemake = true;
		void Loader_Resize(object sender, EventArgs e)
		{
			if (this.Width < 16) this.Width = 16;
			if (this.Height < 16) this.Height = 16;
			bRemake = true;
		}
		public Color BorderColor { get; set; }

		List<RectangleF> rects = new List<RectangleF>();
		[DisplayName("AR_COLORS")]
		public Color[] colors { get; set; }
		[DisplayName("AR_TITLES")]
		public string[] titles { get; set; }

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			var disprect = new Rectangle(DisplayRectangle.Left, DisplayRectangle.Top, DisplayRectangle.Width - 1, DisplayRectangle.Height - 1); ;
			if (bRemake)
			{
				rects.Clear();
				var w = (disprect.Width - 2) / 3f;
				var h = (disprect.Height - 2) / 3f;
				for (int i = 2; i >= 0; i--)
				{
					for (int j = 0; j < 3; j++)
					{
						var rect = new RectangleF(j * w + 2, i * h + 2, w - 2, h - 2);
						rects.Add(rect);
					}
				}
			}
			for (int i = 0; i < rects.Count; i++)
			{
				var item = this.rects[i];

				if (this.colors != null && i < this.colors.Length)
				{
					var color = this.colors[i];
					if (color != Color.Transparent)
						e.Graphics.FillRectangle(new SolidBrush(color), item);
				}


				//테두리 그리기
				if (BorderColor != Color.Transparent)
					e.Graphics.DrawRect(item, BorderColor, 1);

				if (this.titles != null && i < this.titles.Length)
				{
					var title = this.titles[i];
					if (string.IsNullOrEmpty(title) == false)
					{
						using (var br = new SolidBrush(this.ForeColor))
						{
							if (i == 4 && TitleFont != null)
								e.Graphics.DrawString(title, this.TitleFont, br, rects[i], new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
							else
								e.Graphics.DrawString(title, this.Font, br, rects[i], new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
						}
					}
						
				}
			}



			//전체외곽
			//e.Graphics.DrawRect(disprect, Color.Black, 1);
		}
		public void SetColor(int idx, Color color)
		{
			this.colors[idx] = color;
		}
	}
}
