using System;
using System.Collections.Generic;
using System.Text;

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 static readonly String insert_sql = $"insert into {table_name_} (" +
            $"{CreditCSVDataZeus.ColName.SpotID}," +
            $"{CreditCSVDataZeus.ColName.UseDatetime}," +
            $"{CreditCSVDataZeus.ColName.IP}," +
            $"{CreditCSVDataZeus.ColName.DeviceNo}," +
            $"{CreditCSVDataZeus.ColName.ReceiptNo}," +
            $"{CreditCSVDataZeus.ColName.CertificateNo}," +
            $"{CreditCSVDataZeus.ColName.Status}," +
            $"{CreditCSVDataZeus.ColName.ErrorMessage}," +
            $"{CreditCSVDataZeus.ColName.Amount}," +
            $"{CreditCSVDataZeus.ColName.CardNo}," +
            $"{CreditCSVDataZeus.ColName.ExpirationDate}," +
            $"{CreditCSVDataZeus.ColName.Brand}," +
            $"{CreditCSVDataZeus.ColName.PaymentType}," +
            $"{CreditCSVDataZeus.ColName.JISInfo}," +
            $"{CreditCSVDataZeus.ColName.Test}," +
            $"{CreditCSVDataZeus.ColName.OrderNo}," +
            //$"{CSVDataZeus.ColName.UpdDate}," +
            $"{CreditCSVDataZeus.ColName.UpdStaff}," +
            //$"{CSVDataZeus.ColName.RegDate.ToString()}," +
            $"{CreditCSVDataZeus.ColName.RegStaff}" +
            $" ) values (" +
            $"@{CreditCSVDataZeus.ColName.SpotID}," +
            $"@{CreditCSVDataZeus.ColName.UseDatetime}," +
            $"@{CreditCSVDataZeus.ColName.IP}," +
            $"@{CreditCSVDataZeus.ColName.DeviceNo}," +
            $"@{CreditCSVDataZeus.ColName.ReceiptNo}," +
            $"@{CreditCSVDataZeus.ColName.CertificateNo}," +
            $"@{CreditCSVDataZeus.ColName.Status}," +
            $"@{CreditCSVDataZeus.ColName.ErrorMessage}," +
            $"@{CreditCSVDataZeus.ColName.Amount}," +
            $"@{CreditCSVDataZeus.ColName.CardNo}," +
            $"@{CreditCSVDataZeus.ColName.ExpirationDate}," +
            $"@{CreditCSVDataZeus.ColName.Brand}," +
            $"@{CreditCSVDataZeus.ColName.PaymentType}," +
            $"@{CreditCSVDataZeus.ColName.JISInfo}," +
            $"@{CreditCSVDataZeus.ColName.Test}," +
            $"@{CreditCSVDataZeus.ColName.OrderNo}," +
            //$"{CSVDataZeus.ColName.UpdDate}," +
            $"@{CreditCSVDataZeus.ColName.UpdStaff}," +
            //$"{CSVDataZeus.ColName.RegDate}," +
            $"@{CreditCSVDataZeus.ColName.RegStaff}" +
            $");";



        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(CreditCSVData obj) {
            var data = (CreditCSVDataZeus)obj;

            var bindlist = new List<(String, object)> {
                (CreditCSVDataZeus.ColName.SpotID.ToString(), data.spot_id),
                (CreditCSVDataZeus.ColName.UseDatetime.ToString(), data.use_datetime.ToString()),
                (CreditCSVDataZeus.ColName.IP.ToString(), data.ip),
                (CreditCSVDataZeus.ColName.DeviceNo.ToString(), data.device_no),
                (CreditCSVDataZeus.ColName.ReceiptNo.ToString(), data.receipt_no),
                (CreditCSVDataZeus.ColName.CertificateNo.ToString(), data.certificate_no),
                (CreditCSVDataZeus.ColName.Status.ToString(), data.status),
                (CreditCSVDataZeus.ColName.ErrorMessage.ToString(), data.error_message),
                (CreditCSVDataZeus.ColName.Amount.ToString(), data.amount),
                (CreditCSVDataZeus.ColName.CardNo.ToString(), data.card_no),
                (CreditCSVDataZeus.ColName.ExpirationDate.ToString(),   data.expiration_date),
                (CreditCSVDataZeus.ColName.Brand.ToString(), data.brand),
                (CreditCSVDataZeus.ColName.PaymentType.ToString(), data.payment_type),
                (CreditCSVDataZeus.ColName.JISInfo.ToString(), data.jis_info),
                (CreditCSVDataZeus.ColName.Test.ToString(),  data.test),
                (CreditCSVDataZeus.ColName.OrderNo.ToString(), data.order_no),
                (CreditCSVDataZeus.ColName.UpdStaff.ToString(), data.upd_staff),
                (CreditCSVDataZeus.ColName.RegStaff.ToString(), data.reg_staff)
            };

            var ret = InsertData(insert_sql, bindlist);
            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;
        }

    }
}
