using ExcelToCsvToDbCommon.Class;
using KssSmaPlaLib.Forms;
using KssSmaPlaLib.Ini;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReiaSanTool.Gamen
{
    public partial class NewMainForm : Form
    {
        private string _iniPath = string.Empty;

        public NewMainForm()
        {
            InitializeComponent();
        }

        private void NewMainForm_Load(object sender, EventArgs e)
        {
            SetTitleWithIniPath();
            FormHelper.LoadFormBounds(this);
            DgvHelper.Initialize(IniDgv);
            DgvHelper.DgvLoadColWidth(IniDgv);
            ContextMenuHelper.SetFont(this, ContextMenu);
        }

        private void SetTitleWithIniPath()
        {
            FormHelper.SetTitle(this);
            if (!string.IsNullOrEmpty(_iniPath))
            {
                this.Text = this.Text + " - " + _iniPath;
            }
        }

        private void NewMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormHelper.SaveFormBounds(this);
            DgvHelper.DgvSaveColWidth(IniDgv);
        }

        private void ContextMenu_Opening(object sender, CancelEventArgs e)
        {
            SaveMenu.Enabled = !string.IsNullOrEmpty(_iniPath);
            ContextMenuHelper.DispVersion(VersionMenu);
        }

        private void MenuFontMenu_Click(object sender, EventArgs e)
        {
            ContextMenuHelper.SelectMenuFont(this, ContextMenu);
        }

        private void TitleMenu_Click(object sender, EventArgs e)
        {
            ContextMenuHelper.SetFormTitle(this);
        }

        private void OpenMenu_Click(object sender, EventArgs e)
        {
            var iniPath = IniHelper.OpenIni();
            if (string.IsNullOrEmpty(iniPath)) return;

            LoadGrid(ColumnDefinition.Load(iniPath, "ExcelToCsvPoSpIni"));
            _iniPath = iniPath;
            SetTitleWithIniPath();
        }

        private void LoadGrid(List<ColumnDefinition> cdList)
        {
            IniDgv.Rows.Clear();
            for (int cnt = 0; cnt < cdList.Count; cnt++)
            {
                var rowIndex = IniDgv.Rows.Add();
                IniDgv.Rows[rowIndex].Cells["colNo"].Value = cnt + 1;
                IniDgv.Rows[rowIndex].Cells["colName"].Value = cdList[cnt].LogicalName;
                IniDgv.Rows[rowIndex].Cells["colType"].Value = cdList[cnt].TypeName;
                IniDgv.Rows[rowIndex].Cells["colNaiyo"].Value = cdList[cnt].Naiyo;
                IniDgv.Rows[rowIndex].Cells["colComment"].Value = cdList[cnt].Comment;
                IniDgv.Rows[rowIndex].Tag = cdList[cnt];
            }
        }

        private void RowMoveMenu_DropDownOpening(object sender, EventArgs e)
        {
            RowUpMenu.Enabled = false;
            RowDownMenu.Enabled = false;
            if (IniDgv.SelectedRows.Count == 0) return;

            RowUpMenu.Enabled = IniDgv.SelectedRows[0].Index != 0;
            RowDownMenu.Enabled = IniDgv.SelectedRows[0].Index != IniDgv.Rows.Count - 1;
        }

        private void RowUpMenu_Click(object sender, EventArgs e)
        {
            if (IniDgv.SelectedRows.Count == 0) return;

            DgvHelper.MoveRow(IniDgv, IniDgv.SelectedRows[0].Index, true);
            NoFurinaoshi();
        }

        private void RowDownMenu_Click(object sender, EventArgs e)
        {
            if (IniDgv.SelectedRows.Count == 0) return;

            DgvHelper.MoveRow(IniDgv, IniDgv.SelectedRows[0].Index, false);
            NoFurinaoshi();
        }
        private void NoFurinaoshi()
        {
            for (int cnt = 0; cnt < IniDgv.Rows.Count; cnt++)
            {
                IniDgv.Rows[cnt].Cells["colNo"].Value = cnt + 1;
            }
        }

        private void SaveMenu_Click(object sender, EventArgs e)
        {
            // 一旦Col行をすべて削除
            for (var cnt = 1; cnt <= 999; cnt++)
                IniHelper.WriteValue(_iniPath, "ExcelToCsvPoSpIni", $"Col{cnt:D3}", null);

            for (var cnt = 0; cnt < IniDgv.Rows.Count; cnt++)
            {
                var cd = IniDgv.Rows[cnt].Tag as ColumnDefinition;
                if (cd is null) continue;
                IniHelper.WriteValue(_iniPath, "ExcelToCsvPoSpIni", $"Col{cnt + 1:D3}", cd.IniRaw());
            }

            ToastNotifier.Show(this, "保存しました。", Color.SeaGreen);
        }

        private void NewMainForm_KeyDown(object sender, KeyEventArgs e)
        {
            // Enabled制御をしておく
            ContextMenu_Opening(null, null);
            RowMoveMenu_DropDownOpening(null, null);

            // ショートカット実行！（本来はメニューを開いているときか効かない！）
            if (ContextMenuHelper.ExecuteShortcutWithEnabledCheck(ContextMenu.Items, e.KeyData))
            {
                e.Handled = true;
            }
        }

        private void ExcelViewMenu_Click(object sender, EventArgs e)
        {
            using(var frm=new SelectExcelColumnForm(
                ""))
        }
    }
}
