using JWH;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DDUtilityApp
{
    public partial class FrmMain : Form
    {

        public object SelectedObject { get; set; } = null;

        public Dictionary<string, object> Buttons { get; set; } = new Dictionary<string, object>();

        public FrmMain()
        {
            InitializeComponent();

            this.SetLayout();
            this.SetEventHandler();
        }

        public FrmMain(Dictionary<string, object> args) : this()
        {
            this.Buttons = args;
            this.SetButtons();
        }

        protected void SetLayout()
        {
        }

        protected void SetEventHandler()
        {
            this.Load += FrmMain_Load;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.SetButtons();
        }

        /// <summary>
        /// 동적으로 버튼 생성
        /// </summary>
        private void SetButtons()
        {
            try
            {
                int btnWidth = 200;
                int btnHeight = 32;
                int tabIndex = 0;

                List<Button> lstControls = new List<Button>();
                foreach(string name in this.Buttons.Keys)
                {
                    object obj = this.Buttons[name];

                    Button button = new Button();
                    button.Text = name;
                    button.Size = new Size(btnWidth, btnHeight);
                    button.Dock = DockStyle.Top;
                    button.TabIndex = tabIndex++;
                    button.Tag = obj;
                    button.Click += Button_Click;
                    lstControls.Add(button);
                }

                Button btnClose = new Button();
                btnClose.Text = "Close";
                btnClose.Size = new Size(btnWidth, btnHeight);
                btnClose.Dock = DockStyle.Top;
                btnClose.TabIndex = tabIndex++;
                btnClose.Click += BtnClose_Click;
                lstControls.Add(btnClose);

                lstControls.Reverse();
                foreach (Button button in lstControls)
                {
                    this.Controls.Add(button);

                    if (button.Name == "btnClose")
                        this.CancelButton = button;
                }

                this.AutoSize = true;
                this.Padding = new Padding(10);
                this.Size = new Size(this.Width, btnClose.Bounds.Bottom);
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            this.SelectedObject = button.Tag;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }

}
