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

using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.CreditCSVData {


    class CreditCSVDataDaito : CreditCSVData {

        public String parking_name = "";
        public String developer = "";
        public String adjust_type = "";
        public String fee_type = "";
        public DateTime reception_datetime = DateTime.Now;
        public int amount = 0;
        public String reception_no = "";
        public String response_no = "";
        public String error_code1 = "";
        public String error_code2 = "";

        public enum ColName {
            SpotID = 100,
            ParkingName = 0,
            Developer,
            AdjustType,
            FeeType,
            ReceptionDatetime,
            Amount,
            ReceptionNo,
            ResponseNo,
            ErrorCode1,
            ErrorCode2,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class DaitoCreitDataStore : CreditCSVDataStore {

        private static readonly String table_name_ = "creditcard_download_data_daito";


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


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

        }

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

            table.Columns.Add($"{CreditCSVDataDaito.ColName.SpotID}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.ParkingName}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.Developer}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.AdjustType}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.FeeType}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.ReceptionDatetime}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.Amount}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.ReceptionNo}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.ResponseNo}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.ErrorCode1}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.ErrorCode2}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.UpdStaff}");
            table.Columns.Add($"{CreditCSVDataDaito.ColName.RegStaff}");

            foreach (CreditCSVDataDaito data in list) {
                var row = table.NewRow();
                row[$"{CreditCSVDataDaito.ColName.SpotID}"] = data.spot_id;
                row[$"{CreditCSVDataDaito.ColName.ParkingName}"] = data.parking_name;
                row[$"{CreditCSVDataDaito.ColName.Developer}"] = data.developer;
                row[$"{CreditCSVDataDaito.ColName.AdjustType}"] = data.adjust_type;
                row[$"{CreditCSVDataDaito.ColName.FeeType}"] = data.fee_type;
                row[$"{CreditCSVDataDaito.ColName.ReceptionDatetime}"] = data.reception_datetime;
                row[$"{CreditCSVDataDaito.ColName.Amount}"] = data.amount;
                row[$"{CreditCSVDataDaito.ColName.ReceptionNo}"] = data.reception_no;
                row[$"{CreditCSVDataDaito.ColName.ResponseNo}"] = data.response_no;
                row[$"{CreditCSVDataDaito.ColName.ErrorCode1}"] = data.error_code1;
                row[$"{CreditCSVDataDaito.ColName.ErrorCode2}"] = data.error_code2;
                row[$"{CreditCSVDataDaito.ColName.UpdStaff}"] = data.upd_staff;
                row[$"{CreditCSVDataDaito.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"+CreditCSVDataDaito.ColName.ReceptionDatetime.ToString(), from.ToString("yyyy-MM-dd")),
                ("To"+CreditCSVDataDaito.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;
        }

    }
}
