using JWH;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using static DDUtilityApp.GlobalVariable;

namespace DDUtilityApp
{
    public partial class FrmMain : Form
    {

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

        public List<DynamicControl> Buttons { get; set; } = new List<DynamicControl>();

        public FrmMain()
        {
            InitializeComponent();

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

        public FrmMain(List<DynamicControl> 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
            {
                eUserLevel userLevel = GlobalVariable.Instance.UserLevel;

                int btnWidth = 200;
                int btnHeight = 32;
                int tabIndex = 10;

                List<Button> lstControls = new List<Button>();
                foreach(DynamicControl obj in this.Buttons)
                {
                    Button button = new Button();
                    button.Text = obj.Name;
                    button.Size = new Size(btnWidth, btnHeight);
                    button.Dock = DockStyle.Top;
                    button.TabIndex = tabIndex++;
                    button.Tag = obj.Value;
                    button.Visible = obj.Visible;
                    if (obj.Visible == false && userLevel > eUserLevel.Normal) button.Visible = true;
                    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();
        }

    }

}
