using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using FastReport.Matrix;
using FastReport.Utils;

namespace FastReport.Controls
{
#if !DEBUG
    [DesignTimeVisible(false)]
#endif
    internal class MatrixStyleListBox : ListBox
    {
        private bool updating;
        private MatrixStyleSheet styles;

        public event EventHandler StyleSelected;

        public string Style
        {
            get
            {
                if (SelectedIndex < 1)
                    return "";
                return (string)Items[SelectedIndex];
            }
            set
            {
                updating = true;
                int i = Items.IndexOf(value);
                SelectedIndex = i != -1 ? i : 0;
                updating = false;
            }
        }

        public MatrixStyleSheet Styles
        {
            get { return styles; }
            set
            {
                styles = value;
                if (value != null)
                    UpdateItems();
            }
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            Graphics g = e.Graphics;
            if (e.Index >= 0)
            {
                string name = (string)Items[e.Index];
                int styleIndex = styles.IndexOf(name);
                if (styleIndex != -1)
                    name = Res.Get("ComponentsMisc,Matrix," + name);

                Image img = styleIndex == -1 ? this.GetImage(76) : Styles.GetStyleBitmap(styleIndex);
                this.DrawImageAndText(e, img, name);
            }
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            if (updating)
                return;
            if (StyleSelected != null)
                StyleSelected(this, EventArgs.Empty);
        }

        private void UpdateItems()
        {
            Items.Clear();
            Items.Add(Res.Get("Designer,Toolbar,Style,NoStyle"));
            foreach (StyleCollection s in styles)
            {
                Items.Add(s.Name);
            }
        }

        public MatrixStyleListBox()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            ItemHeight = 19;
            IntegralHeight = false;
            Size = new Size(150, 300);
        }
    }
}
