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

namespace CLIP.eForm.Consent.UI
{
    /// <summary>
    /// 이미지 뷰어 관련 컨트롤 클래스
    /// </summary>
    /// <remarks>
    /// <p>[설계자]</p>
    /// <p>  클립소프트 연구소 홍지철 (jchong@clipsoft.co.kr)</p>
    /// <p>[원본 작성자]</p>
    /// <p>  클립소프트 연구소 홍지철 (jchong@clipsoft.co.kr)</p>
    /// <p>[수정 작성자]</p>
    /// <p>  </p>
    /// <p>----------------------------------------------------------------------------------------</p>
    /// <p>[HISTORY]</p>
    /// <p> 2016-07-01 : 최초작성</p>
    /// <p>----------------------------------------------------------------------------------------</p>
    /// </remarks>  
    public partial class ThumbnailCtrl : UserControl
    {
        public ThumbnailCtrl()
        {
            InitializeComponent();
        }

        Dictionary<int,PictureBox> pics = new Dictionary<int,PictureBox>();

        public void UpdateThumbnailImages(List<Image> images)
        {
            this.currentImageIndex = 0;
            pics.Clear();

            List<Control> removeTarget = new List<Control>();

            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is PictureBox)
                {
                    removeTarget.Add(ctrl);
                }
            }

            this.Controls.Clear();

            foreach (Control ctrl in removeTarget)
            {
                ctrl.Dispose();
            }

            this.Size = new Size(50 * images.Count + 20 * images.Count + 5, 80);
            int index = 0;
            foreach (Image img in images)
            {
                PictureBox pic = new PictureBox();
                pic.Size = new Size(50, 70);
                pic.Location = new Point(index * 70 + 3, 3);
                pic.SizeMode = PictureBoxSizeMode.StretchImage;
                this.Controls.Add(pic);
                pics.Add(index, pic);
                pic.Image = img;
                pic.Tag = index;
                pic.Click += Pic_Click;
                index++;
            }

            this.Invalidate();
        }

        /// <summary>
        /// 썸네일 이미지 클릭 이벤트
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void Pic_Click(object sender, EventArgs e)
        {
            PictureBox picBox = sender as PictureBox;

            if (picBox == null)
            {
                return;
            }

            if (picBox.Tag == null)
            {
                return;
            }

            int index = (int)picBox.Tag;

            if (SelectImageChanged != null)
            {
                EventArgs eventArgs = new EventArgs();
                SelectImageChanged.Invoke(index);
            }
        }

        public delegate void IndexChangeHandler(int index);

        public event IndexChangeHandler SelectImageChanged = null;

        public int currentImageIndex = 0;

        internal void SetCurrentImageIndex(int currentImageIndex)
        {
            this.currentImageIndex = currentImageIndex;
            this.Invalidate();
        }

        private bool IsInDesignMode
        {
            get
            {
                return DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if(IsInDesignMode)
            {
                return;
            }

            if (pics.Count < 1)
            {
                return;
            }

            using (Pen pen = new Pen(Color.Blue, 2.0f))
            {
                Rectangle rect = new Rectangle(pics[this.currentImageIndex].Location, pics[this.currentImageIndex].Size);
                rect.Inflate(2, 2);
                e.Graphics.DrawRectangle(pen, rect);
            }
        }
    }
}
