package com.liquidnet.service.candy.util;

import com.liquidnet.commons.lang.util.DateUtil;
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.time.LocalDateTime;
import java.util.List;

public class CouponBaseUtil {

    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;
    }

    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;
    }

    public static CandyUserCouponBasicDto getSingleDtoByUCouponId(List<CandyUserCouponBasicDto> dtoList, String uCouponId) {
        for (CandyUserCouponBasicDto dtoItem : dtoList) {
            if (dtoItem.getUcouponId().equals(uCouponId)) {
                return dtoItem;
            }
        }
        return null;
    }
}
