using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace ST.Library.UI.NodeEditor
{
    internal class FrmSTNodePropertySelect : Form
    {
        private STNodePropertyItem m_item;
        private int m_nItemHeight = 25;

        private static Type m_t_bool = typeof(bool);
        private Pen m_pen;
        private SolidBrush m_brush;
        private StringFormat m_sf;
        private Color m_clr_item_1 = Color.FromArgb(10, 0, 0, 0);// Color.FromArgb(255, 40, 40, 40);
        private Color m_clr_item_2 = Color.FromArgb(10, 255, 255, 255);// Color.FromArgb(255, 50, 50, 50);
        private object m_item_hover;

        public FrmSTNodePropertySelect(STNodePropertyItem item) {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            m_item = item;
            this.Size = item.RectangleR.Size;
            this.ShowInTaskbar = false;
            this.BackColor = item.Control.BackColor;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            m_pen = new Pen(item.Control.ItemSelectedColor, 1);
            m_brush = new SolidBrush(this.BackColor);
            m_sf = new StringFormat();
            m_sf.LineAlignment = StringAlignment.Center;
            m_sf.FormatFlags = StringFormatFlags.NoWrap;
        }

        private List<object> m_lst_item = new List<object>();

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            Point pt = m_item.Control.PointToScreen(m_item.RectangleR.Location);
            pt.Y += m_item.Control.ScrollOffset;
            this.Location = pt;
            if (m_item.PropertyInfo.PropertyType.IsEnum) {
                foreach (var v in Enum.GetValues(m_item.PropertyInfo.PropertyType)) m_lst_item.Add(v);
            } else if (m_item.PropertyInfo.PropertyType == m_t_bool) {
                m_lst_item.Add(true);
                m_lst_item.Add(false);
            } else {
                this.Close();
                return;
            }
            this.Height = m_lst_item.Count * m_nItemHeight;
            Rectangle rect = Screen.GetWorkingArea(this);
            if (this.Bottom > rect.Bottom) this.Top -= (this.Bottom - rect.Bottom);
            this.MouseLeave += (s, ea) => this.Close();
            this.LostFocus += (s, ea) => this.Close();
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            Rectangle rect_back = new Rectangle(0, 0, this.Width, m_nItemHeight);
            Rectangle rect_font = new Rectangle(10, 0, this.Width - 13, m_nItemHeight);
            int nIndex = 0;
            string strVal = m_item.Descriptor.GetStringFromValue();
            foreach (var v in m_lst_item) {
                m_brush.Color = nIndex++ % 2 == 0 ? m_clr_item_1 : m_clr_item_2;
                g.FillRectangle(m_brush, rect_back);
                if (v == m_item_hover) {
                    m_brush.Color = m_item.Control.ItemHoverColor;
                    g.FillRectangle(m_brush, rect_back);
                }
                if (v.ToString() == strVal) {
                    m_brush.Color = m_item.Control.ItemSelectedColor;
                    g.FillRectangle(m_brush, 4, rect_back.Top + 10, 5, 5);
                }
                m_brush.Color = m_item.Control.ForeColor;
                g.DrawString(v.ToString(), m_item.Control.Font, m_brush, rect_font, m_sf);
                rect_back.Y += m_nItemHeight;
                rect_font.Y += m_nItemHeight;
            }
            g.DrawRectangle(m_pen, 0, 0, this.Width - 1, this.Height - 1);
        }

        protected override void OnMouseMove(MouseEventArgs e) {
            base.OnMouseMove(e);
            int nIndex = e.Y / m_nItemHeight;
            if (nIndex < 0 || nIndex >= m_lst_item.Count) return;
            var item = m_lst_item[e.Y / m_nItemHeight];
            if (m_item_hover == item) return;
            m_item_hover = item;
            this.Invalidate();
        }

        protected override void OnMouseClick(MouseEventArgs e) {
            base.OnMouseClick(e);
            this.Close();
            int nIndex = e.Y / m_nItemHeight;
            if (nIndex < 0) return;
            if (nIndex > m_lst_item.Count) return;
            try {
                m_item.Descriptor.SetValue(m_lst_item[nIndex], null);
            } catch (Exception ex) {
                m_item.Descriptor.OnSetValueError(ex);
            }
        }
    }
}
