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

namespace Corp3s.Controls
{
    public partial class OptionBox : Control
    {
        private byte m_nCellCount;
        private Cell[] m_lstCells;

        public OptionBox()
        {
            InitializeComponent();
            CellCount = 1;
        }

        public void SetCheckedCell(int index, bool bChecked)
        {
            Cell curCell = Cells[index];
            if (curCell.Checked == bChecked)
                return;

            curCell.Checked = bChecked;
            Invalidate(curCell.CellBound);
        }

        public Cell[] Cells
        {
            get { return m_lstCells; }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {            
            base.OnPaint(pe);
            PaintOptionBox(pe.Graphics);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            CalculateCell();
        }

        public byte CellCount
        {
            get { return m_nCellCount; }
            set
            {
                if (m_nCellCount == value)
                    return;

                m_nCellCount = value;
                m_lstCells = new Cell[m_nCellCount];
                AllocateCell();
                CalculateCell();

                Invalidate();
            }
        }

        private void AllocateCell()
        {            
            m_lstCells = new Cell[m_nCellCount];
            for (int index = 0; index < m_nCellCount; index++)
                m_lstCells[index] = new Cell();
        }

        private void CalculateCell()
        {
            if (m_nCellCount == 0)
                return;

            int width = ClientSize.Width;
            int height = ClientSize.Height;
            int cellWidth = width / m_nCellCount;
            for (int index = 0; index < m_nCellCount; index++)
            {
                Cell curCell = m_lstCells[index];
                int x = (index * cellWidth) + (cellWidth - myImageList.ImageSize.Width) / 2;
                int y = (height - myImageList.ImageSize.Height) / 2;
                curCell.CellBound = new Rectangle(x, y, myImageList.ImageSize.Width, myImageList.ImageSize.Height);                 
            }
        }

        private void PaintOptionBox(Graphics g)
        {
            for (int index = 0; index < m_nCellCount; index++)
            {
                Cell curCell = m_lstCells[index];
                if (curCell.Checked == true)
                    g.DrawImage(myImageList.Images[1], curCell.CellBound.X, curCell.CellBound.Y);
                else
                    g.DrawImage(myImageList.Images[0], curCell.CellBound.X, curCell.CellBound.Y);
            }

        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            Cell findCell = FindCell(e.X, e.Y);
            if (findCell != null)
            {
                bool bChecked = findCell.Checked;
                ResetCellChecked();
                findCell.Checked = !bChecked;
                Invalidate();
            }

            base.OnMouseDown(e);
        }

        private void ResetCellChecked()
        {
            foreach (Cell curCell in m_lstCells)
                curCell.Checked = false;
        }

        private Cell FindCell(int x, int y)
        {
            foreach (Cell curCell in m_lstCells)
            {
                if (curCell.CellBound.Contains(x, y))
                    return curCell;
            }
            return null;
        }

        public class Cell
        {
            private bool m_Checked;
            private Rectangle m_cellBound;

            public bool Checked
            {
                get { return m_Checked; }
                set { m_Checked = value; }
            }

            public Rectangle CellBound
            {
                get { return m_cellBound; }
                set { m_cellBound = value; }
            }
        }
    }
}
