package com.neusoft.smart.pos.member.core;

import com.alibaba.fastjson.JSONObject;
import com.neusoft.smart.pos.dto.promotion.bean.CalculateBean;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.member.constants.BonusCalculatorType;
import com.neusoft.smart.pos.member.constants.MemberErrorCode;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class OrderAmountDiscountCalculator extends BonusCalculator {

    private OrderAmountDiscountCalculator() {
    }

    private OrderAmountDiscountCalculator(Integer discountProportion) {
        this.discountProportion = discountProportion;
    }

    private Integer discountProportion;

    @Override
    protected JSONObject getPara() {
        JSONObject para = new JSONObject();
        para.put("discount", this.discountProportion);
        return para;
    }

    @Override
    protected BonusCalculatorType getType() {
        return BonusCalculatorType.ORDER_AMOUNT_DISCOUNT;
    }

    public static BonusCalculator.ConditionCreator getConditionCreator() {
        return new OrderAmountDiscountCalculator.ConditionCreator();
    }

    @Override
    public CalculateBean calculate(CalculateBean calculateBean) {
        BigDecimal discount = new BigDecimal(this.discountProportion/100);

        //根据折扣计算实际价格
        calculateBean.setActualPrice(calculateBean.getTotalPrice().multiply(discount).setScale(2, RoundingMode.UP));
        //根据折扣,计算每件商品的实际价格
        if (null != calculateBean.getOrderItems()) {
            BigDecimal finalDiscount = discount;
            calculateBean.getOrderPreferentialItems().forEach(item ->
                    item.setPreferentialUnitPrice(item.getUnitPrice().multiply(finalDiscount).setScale(2, RoundingMode.UP)));
        }
        return calculateBean;
    }

    private static class ConditionCreator extends BonusCalculator.ConditionCreator {

        @Override
        BonusCalculator createInstance(JSONObject para) {
            validateJsonObject(para);
            return new OrderAmountDiscountCalculator(para.getInteger("discount"));
        }

        @Override
        BonusCalculatorType getBonusCalculatorType() {
            return BonusCalculatorType.ORDER_AMOUNT_DISCOUNT;
        }

        @Override
        BonusCalculator createDefaultInstance() {
            OrderAmountDiscountCalculator result = new OrderAmountDiscountCalculator();
            result.discountProportion = 100;
            return result;
        }

        @Override
        void validateJsonObject(JSONObject jsonObject) {
            if (jsonObject.containsKey("discount")) {
                Object o = jsonObject.get("discount");
                if (o != null) {
                    if (o instanceof Integer) {
                        Integer discount = (Integer) o;
                        if (discount > 0 && discount < 100) {
                            return;
                        }
                    }
                }
            }
            throw new BusinessCommonException(MemberErrorCode.INVALID_DISCOUNT);
        }
    }

}
