package com.miya.fastcashier.beans;

import android.text.TextUtils;

import com.sdy.miya.moblie.component.pay.platform.bean.BaseDO;
import com.sdy.miya.moblie.component.pay.platform.bean.QueryTradesByTimeResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ViewOrderStatisticsInfo extends BaseDO {

    private String beginDate;
    private String endDate;
    private StatisticBean totalStatistic;
    private List<StatisticBean> typeStatistic;

    public String getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public StatisticBean getTotalStatistic() {
        return totalStatistic;
    }

    public void setTotalStatistic(StatisticBean totalStatistic) {
        this.totalStatistic = totalStatistic;
    }

    public List<StatisticBean> getTypeStatistic() {
        return typeStatistic;
    }

    public void setTypeStatistic(List<StatisticBean> typeStatistic) {
        this.typeStatistic = typeStatistic;
    }

    public static class StatisticBean extends BaseDO {
        /**
         * 支付方式
         */
        private String payType;
        /**
         * 交易笔数
         */
        private int tradeCount;
        /**
         * 交易总金额
         */
        private int tradeTotalAmount;
        /**
         * 退款笔数
         */
        private int refundCount;
        /**
         * 退款总金额
         */
        private int refundTotalAmount;
        /**
         * 商户优惠
         */
        private int merchantDiscount;
        /**
         * 平台优惠
         */
        private int platformDiscount;
        /**
         * 退款商户优惠
         */
        private int refundMerchantDiscount;
        /**
         * 退款平台优惠
         */
        private int refundPlatformDiscount;

        public String getPayType() {
            return payType;
        }

        public void setPayType(String payType) {
            this.payType = payType;
        }

        public int getTradeCount() {
            return tradeCount;
        }

        public void setTradeCount(int tradeCount) {
            this.tradeCount = tradeCount;
        }

        public int getTradeTotalAmount() {
            return tradeTotalAmount;
        }

        public void setTradeTotalAmount(int tradeTotalAmount) {
            this.tradeTotalAmount = tradeTotalAmount;
        }

        public int getRefundCount() {
            return refundCount;
        }

        public void setRefundCount(int refundCount) {
            this.refundCount = refundCount;
        }

        public int getRefundTotalAmount() {
            return refundTotalAmount;
        }

        public void setRefundTotalAmount(int refundTotalAmount) {
            this.refundTotalAmount = refundTotalAmount;
        }

        public int getMerchantDiscount() {
            return merchantDiscount;
        }

        public void setMerchantDiscount(int merchantDiscount) {
            this.merchantDiscount = merchantDiscount;
        }

        public int getPlatformDiscount() {
            return platformDiscount;
        }

        public void setPlatformDiscount(int platformDiscount) {
            this.platformDiscount = platformDiscount;
        }

        public int getRefundMerchantDiscount() {
            return refundMerchantDiscount;
        }

        public void setRefundMerchantDiscount(int refundMerchantDiscount) {
            this.refundMerchantDiscount = refundMerchantDiscount;
        }

        public int getRefundPlatformDiscount() {
            return refundPlatformDiscount;
        }

        public void setRefundPlatformDiscount(int refundPlatformDiscount) {
            this.refundPlatformDiscount = refundPlatformDiscount;
        }
    }

    public static ViewOrderStatisticsInfo transfer(QueryTradesByTimeResponse queryTradesByTimeResponse, String beginDate, String endDate) {
        ViewOrderStatisticsInfo statisticsInfo = new ViewOrderStatisticsInfo();
        statisticsInfo.setBeginDate(beginDate);
        statisticsInfo.setEndDate(endDate);
        if (!TextUtils.isEmpty(queryTradesByTimeResponse.getBillInformation())) {
            String billInformation = queryTradesByTimeResponse.getBillInformation();
            String[] serial = billInformation.split(";");
            if (serial.length > 0) {
                statisticsInfo.typeStatistic = new ArrayList<>();
                for (int i = 0; i < serial.length; i++) {
                    if (!TextUtils.isEmpty(serial[i])) {
                        String[] values = serial[i].split(",");
                        StatisticBean statisticBean = new StatisticBean();
                        for (int j = 0; j < values.length; j++) {
                            valueToField(statisticBean, values[j], j);
                        }
                        statisticsInfo.typeStatistic.add(statisticBean);
                    }
                }
            }
        }
        statisticsInfo.totalStatistic = new StatisticBean();
        statisticsInfo.totalStatistic.payType = null;
        statisticsInfo.totalStatistic.tradeCount = queryTradesByTimeResponse.getTradeCount();
        statisticsInfo.totalStatistic.tradeTotalAmount = queryTradesByTimeResponse.getTradeTotalAmount();
        statisticsInfo.totalStatistic.refundCount = queryTradesByTimeResponse.getRefundCount();
        statisticsInfo.totalStatistic.refundTotalAmount = queryTradesByTimeResponse.getRefundTotalAmount();
        statisticsInfo.totalStatistic.merchantDiscount = queryTradesByTimeResponse.getMerchantDiscount();
        statisticsInfo.totalStatistic.platformDiscount = queryTradesByTimeResponse.getPlatformDiscount();
        statisticsInfo.totalStatistic.refundMerchantDiscount = queryTradesByTimeResponse.getRefundMerchantDiscount();
        statisticsInfo.totalStatistic.refundPlatformDiscount = queryTradesByTimeResponse.getRefundPlatformDiscount();
        return statisticsInfo;
    }

    private static void valueToField(StatisticBean statisticBean, String value, int index) {
        switch (index) {
            case 0:
                statisticBean.payType = value;
                break;
            case 1:
                statisticBean.tradeCount = toInt(value);
                break;
            case 2:
                statisticBean.tradeTotalAmount = toInt(value);
                break;
            case 3:
                statisticBean.refundCount = toInt(value);
                break;
            case 4:
                statisticBean.refundTotalAmount = toInt(value);
                break;
            case 5:
                statisticBean.merchantDiscount = toInt(value);
                break;
            case 6:
                statisticBean.platformDiscount = toInt(value);
                break;
            case 7:
                statisticBean.refundMerchantDiscount = toInt(value);
                break;
            case 8:
                statisticBean.refundPlatformDiscount = toInt(value);
                break;
        }
    }

    private static int toInt(String value) {
        if (TextUtils.isEmpty(value) && !TextUtils.isDigitsOnly(value)) {
            return 0;
        }
        return new BigDecimal(value).intValue();
    }


}
