using System;
using System.Collections.Generic;
using System.Data;

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.ElectronicMoneyDataStore {


    class DaitoElectronicMoneyData : ElectronicMoneyCSVData {

        public String parking_name = "";
        public DateTime reception_datetime = DateTime.Now;
        public String adjust_type = "";
        public int amount = 0;
        public String trade_id = "";
        public String adjust_result = "";

        public enum ColName {
            SpotID = 100,
            ParkingName = 0,
            ReceptionDatetime,
            AdjustType,
            Amount,
            TradeID,
            AdjustResult,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class DaitoElectronicMoneyDataStore : ElectronicMoneyCSVDataStore {

        private static readonly String table_name_ = "electronic_money_download_data_daito";

        private String delete_sql_ = $"delete from {table_name_} where " +
            $"{DaitoElectronicMoneyData.ColName.ReceptionDatetime.ToString()} between " +
            $"@From{DaitoElectronicMoneyData.ColName.ReceptionDatetime.ToString()} and " +
            $"@To{DaitoElectronicMoneyData.ColName.ReceptionDatetime.ToString()};";


        public DaitoElectronicMoneyDataStore(MySqlConnection conn) : base(conn) {

        }


        public override void Save(List<ElectronicMoneyCSVData> list) {
            Code.ResultCode ret;
            var table = new DataTable();

            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.SpotID}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.ParkingName}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.ReceptionDatetime}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.AdjustType}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.Amount}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.TradeID}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.AdjustResult}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.UpdDate}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.UpdStaff}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.RegDate}");
            table.Columns.Add($"{DaitoElectronicMoneyData.ColName.RegStaff}");


            foreach (DaitoElectronicMoneyData data in list) {
                var row = table.NewRow();

                row[$"{DaitoElectronicMoneyData.ColName.SpotID}"] = data.spot_id;
                row[$"{DaitoElectronicMoneyData.ColName.ParkingName}"] = data.parking_name;
                row[$"{DaitoElectronicMoneyData.ColName.ReceptionDatetime}"] = data.reception_datetime;
                row[$"{DaitoElectronicMoneyData.ColName.AdjustType}"] = data.adjust_type;
                row[$"{DaitoElectronicMoneyData.ColName.Amount}"] = data.amount;
                row[$"{DaitoElectronicMoneyData.ColName.TradeID}"] = data.trade_id;
                row[$"{DaitoElectronicMoneyData.ColName.AdjustResult}"] = data.adjust_result;
                row[$"{DaitoElectronicMoneyData.ColName.UpdDate}"] = data.upd_date;
                row[$"{DaitoElectronicMoneyData.ColName.UpdStaff}"] = data.upd_staff;
                row[$"{DaitoElectronicMoneyData.ColName.RegDate}"] = data.reg_date;
                row[$"{DaitoElectronicMoneyData.ColName.RegStaff}"] = data.reg_staff;


                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 Daito 電子マネー");
            }
        }
        public override int Delete(DateTime from, DateTime to) {

            var bindlist = new List<(String, object)> {
                ("From"+DaitoElectronicMoneyData.ColName.ReceptionDatetime.ToString(), from.ToString("yyyy-MM-dd")),
                ("To"+DaitoElectronicMoneyData.ColName.ReceptionDatetime.ToString(), to.AddDays(1).ToString("yyyy-MM-dd"))
            };

            var delete_count = DeleteData(delete_sql_, bindlist);
            if (delete_count == -1) {
                throw new Exception("削除失敗 大都電子マネー");
            }
            return delete_count;
        }

    }
}
