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 ExcelToCsvToDbCommon.Gamen
{
    public partial class ColKeyValueListForm : Form
    {
        public Dictionary<string, string> keyValuePairs;

        // フォーム側で保持
        private BindingList<ConfigItem> _bindingList;

        public ColKeyValueListForm(Dictionary<string, string> kvpDic)
        {
            InitializeComponent();

            if (kvpDic is null) kvpDic = new Dictionary<string, string>();

            _bindingList = new BindingList<ConfigItem>(
                kvpDic.Select(x => new ConfigItem { Key = x.Key, Value = x.Value }).ToList()
            );
            // バインド！これだけで画面と連動します
            KeyValueListDgv.DataSource = _bindingList;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            keyValuePairs = _bindingList
                .Where(x => !string.IsNullOrWhiteSpace(x.Key)) // 空のキーを除外
                .ToDictionary(x => x.Key, x => x.Value);
            DialogResult = DialogResult.OK;
        }
    }

    // キーと値を持つシンプルなクラスを用意
    public class ConfigItem
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}
