package com.liquidnet.service.candy.util;

import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.candy.dto.CandyCouponRuleDto;
import com.liquidnet.service.candy.dto.CandyUserCouponBasicDto;
import com.liquidnet.service.candy.vo.CandyCouponRulesVo;
import com.liquidnet.service.candy.vo.CandyCouponVo;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

public class CouponBaseUtil {


    /**
     * 获取 基础券vo
     *
     * @param dtoItem CandyUserCouponBasicDto
     * @return
     */
    public static CandyCouponVo getBaseCouponUserVo(CandyUserCouponBasicDto dtoItem) {
        LocalDateTime now = LocalDateTime.now();
        CandyCouponVo vo = CandyCouponVo.getNew();
        List<CandyCouponRulesVo> rulesVoList = ObjectUtil.getCandyCouponRulesVos();
        vo.setTitle(dtoItem.getTitle());
        vo.setExpireAt(DateUtil.format(dtoItem.getExpireAt(), DateUtil.Formatter.yyyy_MM_dd));
        vo.setValFace(dtoItem.getValFace());
        vo.setValOver(dtoItem.getValOver());
        vo.setValMinus(dtoItem.getValMinus());
        vo.setBindType(dtoItem.getBindType());
        vo.setLabel(dtoItem.getLabel());
        vo.setNotice(dtoItem.getNotice());
        vo.setUcouponId(dtoItem.getUcouponId());
        vo.setUsedAt(DateUtil.format(dtoItem.getUsedAt(), DateUtil.Formatter.yyyy_MM_dd));
        vo.setUsedFor(dtoItem.getUsedFor());
        //适用范围 规则
        for (CandyCouponRuleDto ruleItem : dtoItem.getUseRules()) {
            CandyCouponRulesVo voRules = CandyCouponRulesVo.getNew();
            voRules.setUseScope(ruleItem.getUseScope());
            voRules.setBusiId(ruleItem.getBusiId());
            voRules.setBusiName(ruleItem.getBusiName());
            rulesVoList.add(voRules);
        }
        //券类别
        if (dtoItem.getBusiType().equals(3)) {
            vo.setCouType(101);
        } else {
            vo.setCouType(dtoItem.getCouType());
        }
        vo.setState(getCouponState(now, dtoItem.getExpireAt(), dtoItem.getState()));
        vo.setUseRules(rulesVoList);
        return vo;
    }

    /**
     * 获取 基础券vo
     *
     * @param dtoItem CandyUserCouponBasicDto
     * @return
     */
    public static CandyCouponVo getPerformanceCouponUserVo(CandyUserCouponBasicDto dtoItem,
                                                           BigDecimal priceTotal,
                                                           String performanceId,
                                                           String timeId,
                                                           String ticketId) {
        boolean isTarget = false;
        LocalDateTime now = LocalDateTime.now();
        CandyCouponVo vo = CandyCouponVo.getNew();
        List<CandyCouponRulesVo> rulesVoList = ObjectUtil.getCandyCouponRulesVos();
        vo.setTitle(dtoItem.getTitle());
        vo.setExpireAt(DateUtil.format(dtoItem.getExpireAt(), DateUtil.Formatter.yyyy_MM_dd));
        vo.setValFace(dtoItem.getValFace());
        vo.setValOver(dtoItem.getValOver());
        vo.setValMinus(dtoItem.getValMinus());
        vo.setBindType(dtoItem.getBindType());
        vo.setLabel(dtoItem.getLabel());
        vo.setNotice(dtoItem.getNotice());
        vo.setUcouponId(dtoItem.getUcouponId());
        vo.setUsedAt(DateUtil.format(dtoItem.getUsedAt(), DateUtil.Formatter.yyyy_MM_dd));
        vo.setUsedFor(dtoItem.getUsedFor());
        //适用范围 规则
        for (CandyCouponRuleDto ruleItem : dtoItem.getUseRules()) {
            CandyCouponRulesVo voRules = CandyCouponRulesVo.getNew();
            voRules.setUseScope(ruleItem.getUseScope());
            voRules.setBusiId(ruleItem.getBusiId());
            voRules.setBusiName(ruleItem.getBusiName());
            rulesVoList.add(voRules);
            switch (ruleItem.getUseScope()) {
                case 90://演出
                    isTarget = isTargetCoupon(ruleItem.getBusiId(), performanceId, priceTotal, dtoItem.getValOver(), dtoItem.getValMinus());
                    break;
                case 91://场次
                    isTarget = isTargetCoupon(ruleItem.getBusiId(), timeId, priceTotal, dtoItem.getValOver(), dtoItem.getValMinus());
                    break;
                case 92://票
                    isTarget = isTargetCoupon(ruleItem.getBusiId(), ticketId, priceTotal, dtoItem.getValOver(), dtoItem.getValMinus());
                    break;
                default:
                    isTarget = false;
                    break;
            }
        }
        //券类别
        if (dtoItem.getBusiType().equals(3)) {
            vo.setCouType(101);
        } else {
            vo.setCouType(dtoItem.getCouType());
        }
        int state = getCouponState(now, dtoItem.getExpireAt(), dtoItem.getState());
        if (isTarget) {
            vo.setState(state);
        } else {
            vo.setState(state == 1 ? 21 : state);//不在目标范围 如果可用改为不可用21
        }
        vo.setUseRules(rulesVoList);
        return vo;
    }

    /**
     * @param busiId     适用id
     * @param targetId   目标id(例如 演出id，场次id，票种id)
     * @param priceTotal 待支付总金额
     * @param valOver    满减~满
     * @param valMinus   满减~减
     * @return
     */
    public static Boolean isTargetCoupon(String busiId,
                                         String targetId,
                                         BigDecimal priceTotal,
                                         BigDecimal valOver,
                                         BigDecimal valMinus) {
        boolean isTarget = false;

        if (busiId.equals(targetId)) {//判断id 对应

        }

        return isTarget;
    }

    /**
     * 获取券状态
     *
     * @param currentTime 当前服务器时间
     * @param expireAt    券过期时间
     * @param state       券状态
     * @return 判断过后的券状态
     */
    public static Integer getCouponState(LocalDateTime currentTime, LocalDateTime expireAt, Integer state) {
        //券状态
        Integer data;
        if (state.equals(1)) {
            if (currentTime.isAfter(expireAt)) {
                data = 3;
            } else {
                data = state;
            }
        } else {
            data = state;
        }
        return data;
    }

    /**
     * 根据券id获取Dto
     *
     * @param dtoList   CouponBaseDtoList
     * @param uCouponId 券唯一id
     * @return CandyUserCouponBasicDto
     */
    public static CandyUserCouponBasicDto getSingleDtoByUCouponId(List<CandyUserCouponBasicDto> dtoList, String uCouponId) {
        for (CandyUserCouponBasicDto dtoItem : dtoList) {
            if (dtoItem.getUcouponId().equals(uCouponId)) {
                return dtoItem;
            }
        }
        return null;
    }

    /**
     * 使用券
     *
     * @param dtoList   CouponBaseDtoList
     * @param uCouponId 券唯一id
     * @return List<CandyUserCouponBasicDto>
     */
    public static List<CandyUserCouponBasicDto> useCoupon(List<CandyUserCouponBasicDto> dtoList, String uCouponId) {
        boolean isHas = false;
        for (CandyUserCouponBasicDto dtoItem : dtoList) {
            if (dtoItem.getUcouponId().equals(uCouponId)) {
                if (dtoItem.getState().equals(1)) {
                    dtoItem.setState(5);
                    isHas = true;//券存在
                } else {
                    return null;//券不可用
                }
                break;
            }
        }
        if (!isHas) {
            return null;//券不存在
        }
        return dtoList;
    }

    /**
     * 回退券
     *
     * @param dtoList   CouponBaseDtoList
     * @param uCouponId 券唯一id
     * @return List<CandyUserCouponBasicDto>
     */
    public static List<CandyUserCouponBasicDto> backCoupon(List<CandyUserCouponBasicDto> dtoList, String uCouponId) {
        boolean isHas = false;
        for (CandyUserCouponBasicDto dtoItem : dtoList) {
            if (dtoItem.getUcouponId().equals(uCouponId)) {
                if (dtoItem.getState().equals(5)) {
                    dtoItem.setState(1);
                    isHas = true;//券存在
                } else {
                    return null;//券不可用
                }
                break;
            }
        }
        if (!isHas) {
            return null;//券不存在
        }
        return dtoList;
    }
}
