using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MySql.Data.MySqlClient;


namespace CSVDownloader.Store.CreditCSVData {


    class CreditCSVDataZeus : CreditCSVData {

        public DateTime use_datetime = DateTime.Now;
        public String ip = "";
        public String device_no = "";
        public String receipt_no = "";
        public String certificate_no = "";
        public String status = "";
        public String error_message = "";
        public int amount = 0;
        public String card_no = "";
        public String expiration_date = "";
        public String brand = "";
        public String payment_type = "";
        public String jis_info = "";
        public String test = "";
        public String order_no = "";


        public enum ColName {
            SpotID = 100,
            UseDatetime = 0,
            IP,
            DeviceNo,
            ReceiptNo,
            CertificateNo,
            Status,
            ErrorMessage,
            Amount,
            CardNo,
            ExpirationDate,
            Brand,
            PaymentType,
            JISInfo,
            Test,
            OrderNo,
            UpdDate,
            UpdStaff,
            RegDate,
            RegStaff
        };
    }

    class ZeusCreditDataStore : CreditCSVDataStore {

        private static readonly String table_name_ = "creditcard_download_data_zeus";
        private String delete_sql_ = $"delete from {table_name_} where " +
            $"{CreditCSVDataZeus.ColName.UseDatetime} between " +
            $"@From{CreditCSVDataZeus.ColName.UseDatetime} and " +
            $"@To{CreditCSVDataZeus.ColName.UseDatetime};";


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

        }

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

            table.Columns.Add(CreditCSVDataZeus.ColName.SpotID.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.UseDatetime.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.IP.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.DeviceNo.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.ReceiptNo.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.CertificateNo.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.Status.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.ErrorMessage.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.Amount.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.CardNo.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.ExpirationDate.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.Brand.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.PaymentType.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.JISInfo.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.Test.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.OrderNo.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.UpdStaff.ToString());
            table.Columns.Add(CreditCSVDataZeus.ColName.RegStaff.ToString());

            foreach (CreditCSVDataZeus data in list) {
                var row = table.NewRow();
                row[CreditCSVDataZeus.ColName.SpotID.ToString()] = data.spot_id;
                row[CreditCSVDataZeus.ColName.UseDatetime.ToString()] = data.use_datetime;
                row[CreditCSVDataZeus.ColName.IP.ToString()] = data.ip;
                row[CreditCSVDataZeus.ColName.DeviceNo.ToString()] = data.device_no;
                row[CreditCSVDataZeus.ColName.ReceiptNo.ToString()] = data.receipt_no;
                row[CreditCSVDataZeus.ColName.CertificateNo.ToString()] = data.certificate_no;
                row[CreditCSVDataZeus.ColName.Status.ToString()] = data.status;
                row[CreditCSVDataZeus.ColName.ErrorMessage.ToString()] = data.error_message;
                row[CreditCSVDataZeus.ColName.Amount.ToString()] = data.amount;
                row[CreditCSVDataZeus.ColName.CardNo.ToString()] = data.card_no;
                row[CreditCSVDataZeus.ColName.ExpirationDate.ToString()] = data.expiration_date;
                row[CreditCSVDataZeus.ColName.Brand.ToString()] = data.brand;
                row[CreditCSVDataZeus.ColName.PaymentType.ToString()] = data.payment_type;
                row[CreditCSVDataZeus.ColName.JISInfo.ToString()] = data.jis_info;
                row[CreditCSVDataZeus.ColName.Test.ToString()] = data.test;
                row[CreditCSVDataZeus.ColName.OrderNo.ToString()] = data.order_no;
                row[CreditCSVDataZeus.ColName.UpdStaff.ToString()] = data.upd_staff;
                row[CreditCSVDataZeus.ColName.RegStaff.ToString()] = data.reg_staff;

                table.Rows.Add(row);
            }


            ret = BulkInsertData(table_name_, table);

            if (ret != Code.ResultCode.OK) {
                throw new Exception("登録失敗 Zeus");
            }

        }

        public override int Delete(DateTime from, DateTime to) {

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

            var delete_count = DeleteData(delete_sql_, bindlist);
            if (delete_count == -1) {
                throw new Exception("削除失敗 Zeus");
            }
            return delete_count;
        }

    }
}
