using MetroFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HSUCO_Cargo_Garage_Operation_Program.CustomUserControl
{
    public partial class AreaSettingControl : UserControl
    {
        private List<AreaSetting> mAreaSettings;

        public AreaSettingControl()
        {
            InitializeComponent();
        }
        private string GetAreaTypeString(EAreaType eAreaType)
        {
            string typeString = string.Empty;
            switch (eAreaType)
            {
                case EAreaType.None:
                    typeString = "이용 불가";
                    break;

                case EAreaType.Large:
                    typeString = "대형";
                    break;

                case EAreaType.OverSized:
                    typeString = "특대형";
                    break;
            }
            return typeString;
        }

        private ListViewItem CreateListViewItem(AreaSetting areaSetting)
        {
            ListViewItem listViewItem = new ListViewItem();
            listViewItem.Text = areaSetting.No.ToString();
            listViewItem.SubItems.Add(areaSetting.AreaName);
            string typeString = GetAreaTypeString(areaSetting.AreaType);

            listViewItem.SubItems.Add(typeString);
            return listViewItem;
        }

        private void metroButton_Read_Click(object sender, EventArgs e)
        {
            mAreaSettings = Database.GetAreaSettings();
            listView_Area.Items.Clear();
            foreach (AreaSetting areaSetting in mAreaSettings)
            {
                ListViewItem listViewItem = CreateListViewItem(areaSetting);

                listView_Area.Items.Add(listViewItem);
            }
        }

        private void metroButton_NewArea_Click(object sender, EventArgs e)
        {
            if (metroComboBox_NewAreaType.SelectedIndex == -1)
            {
                MetroMessageBox.Show(this, "구역 타입이 선택되지 않았습니다..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string areaName = metroTextBox_NewAreaName.Text.Trim();
            if (areaName == string.Empty)
            {
                MetroMessageBox.Show(this, "구역 이름은 공백일수없습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            bool result = Database.CheckDuplicateAreaSettingName(areaName);
            if (result)
            {
                MetroMessageBox.Show(this, "이미 있는 구역 이름 입니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AreaSetting areaSetting = new AreaSetting();
            areaSetting.AreaName = areaName;
            areaSetting.AreaType = (EAreaType)metroComboBox_NewAreaType.SelectedIndex;
            areaSetting.No = Database.SetAreaSetting(areaSetting);
            listView_Area.Items.Add(CreateListViewItem(areaSetting));
            mAreaSettings.Add(areaSetting);
            metroTextBox_NewAreaName.Text = string.Empty;
            MetroMessageBox.Show(this, "추가 하였습니다.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            metroTextBox_NewAreaName.Focus();
        }

        private void metroButton_ChangeAreaName_Click(object sender, EventArgs e)
        {
            string areaName = metroTextBox_ChangeAreaName.Text.Trim();
            if (areaName == string.Empty)
            {
                MetroMessageBox.Show(this, "구역 이름은 공백일수없습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int no = Convert.ToInt32(listView_Area.SelectedItems[0].Text);
            AreaSetting areaSetting = mAreaSettings.Single(x => x.No == no);
            if (areaName == areaSetting.AreaName)
            {
                MetroMessageBox.Show(this, "이름이 변경되지 않았습니다..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            bool result = Database.CheckDuplicateAreaSettingName(areaName);
            if (result)
            {
                MetroMessageBox.Show(this, "이미 있는 구역 이름 입니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            areaSetting.AreaName = areaName;
            Database.UpdateAreaSetting(areaSetting);
            for (int i = 0; i < mAreaSettings.Count; i++)
            {
                if (mAreaSettings[i].No == no)
                {
                    mAreaSettings[i] = areaSetting;
                }
            }
            listView_Area.SelectedItems[0].SubItems[1].Text = areaSetting.AreaName;
        }

        private void metroButton__ChangeAreaType_Click(object sender, EventArgs e)
        {
            int changeType = metroComboBox_ChangeAreaType.SelectedIndex;
            if (changeType == -1)
            {
                MetroMessageBox.Show(this, "구역 타입이 선택되지 않았습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            EAreaType eAreaType = (EAreaType)changeType;
            int no = Convert.ToInt32(listView_Area.SelectedItems[0].Text);
            AreaSetting areaSetting = mAreaSettings.Single(x => x.No == no);
            if (areaSetting.AreaType == eAreaType)
            {
                MetroMessageBox.Show(this, "구역 타입이 변경되지 않았습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            areaSetting.AreaType = eAreaType;
            Database.UpdateAreaSetting(areaSetting);
            for (int i = 0; i < mAreaSettings.Count; i++)
            {
                if (mAreaSettings[i].No == no)
                {
                    mAreaSettings[i] = areaSetting;
                }
            }
            listView_Area.SelectedItems[0].SubItems[2].Text = GetAreaTypeString(areaSetting.AreaType);

        }


        private void listView_Area_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (listView_Area.SelectedItems.Count < 1)
            {
                return;
            }
            int no = Convert.ToInt32(listView_Area.SelectedItems[0].Text);
            AreaSetting areaSetting = mAreaSettings.Single(x => x.No == no);
            metroTextBox_ChangeAreaName.Text = areaSetting.AreaName;
            metroComboBox_ChangeAreaType.SelectedIndex = Convert.ToInt32(areaSetting.AreaType);
        }
    }
}