package com.slodon.b2b2c.vo.promotion;

import com.slodon.b2b2c.core.constant.DrawConst;
import com.slodon.b2b2c.core.i18n.Language;
import com.slodon.b2b2c.core.util.FileUrlUtil;
import com.slodon.b2b2c.promotion.pojo.Draw;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 封装抽奖活动vo
 */
@Data
public class DrawVO implements Serializable {

    private static final long serialVersionUID = -6917240261839745164L;
    @ApiModelProperty("抽奖活动id")
    private Integer drawId;

    @ApiModelProperty("抽奖活动名称，最多6个字")
    private String drawName;

    @ApiModelProperty("活动开始时间")
    private Date startTime;

    @ApiModelProperty("活动结束时间")
    private Date endTime;

    @ApiModelProperty("抽奖消耗积分数（活动类型）")
    private Integer integralUse;

    @ApiModelProperty("活动规则类型，1-每人每天可抽奖n次，2-每人总共可抽奖n次")
    private Integer ruleType;

    @ApiModelProperty("活动规则次数")
    private Integer ruleNum;

    @ApiModelProperty("背景图")
    private String backgroundImage;

    @ApiModelProperty("正常可用时按钮图片")
    private String availableButtonImage;

    @ApiModelProperty("机会用尽时按钮图片")
    private String chanceOutButtonImage;

    @ApiModelProperty("是否开启虚拟中奖，0-不开启，1-开启，开启后，中奖名单将加入虚拟中奖数据")
    private Integer openVirtual;

    @ApiModelProperty("活动描述，最多100字")
    private String drawDescription;

    @ApiModelProperty("未中奖提示信息，最多6个字")
    private String losePrizeDescription;

    @ApiModelProperty("未中奖图片")
    private String losePrizeImage;

    @ApiModelProperty("抽奖活动类型，1-幸运抽奖，2-大转盘，3-刮刮卡，4-摇一摇，5-翻翻看")
    private Integer drawType;

    @ApiModelProperty("抽奖活动类型，1-幸运抽奖，2-大转盘，3-刮刮卡，4-摇一摇，5-翻翻看")
    private String drawTypeValue;

    @ApiModelProperty("综合中奖率")
    private Integer totalRate;

    @ApiModelProperty("活动状态(1-未开始;2-进行中;3-已结束)")
    private Integer state;

    @ApiModelProperty("活动状态(1-未开始;2-进行中;3-已结束)")
    private String stateValue;

    @ApiModelProperty("抽奖规则")
    private String ruleValue;

    @ApiModelProperty("抽奖人数")
    private Integer drawNum;

    @ApiModelProperty("中奖人数")
    private Integer prizeNum;

    public DrawVO(Draw draw) {
        drawId = draw.getDrawId();
        drawName = draw.getDrawName();
        startTime = draw.getStartTime();
        endTime = draw.getEndTime();
        integralUse = draw.getIntegralUse();
        ruleType = draw.getRuleType();
        ruleNum = draw.getRuleNum();
        ruleValue = dealRuleValue(draw.getRuleType(), draw.getRuleNum());
        backgroundImage = FileUrlUtil.getFileUrl(draw.getBackgroundImage(), null);
        availableButtonImage = FileUrlUtil.getFileUrl(draw.getAvailableButtonImage(), null);
        chanceOutButtonImage = FileUrlUtil.getFileUrl(draw.getChanceOutButtonImage(), null);
        openVirtual = draw.getOpenVirtual();
        drawDescription = draw.getDrawDescription();
        losePrizeDescription = draw.getLosePrizeDescription();
        losePrizeImage = FileUrlUtil.getFileUrl(draw.getLosePrizeImage(), null);
        drawType = draw.getDrawType();
        drawTypeValue = dealDrawTypeValue(draw.getDrawType());
        totalRate = draw.getTotalRate();
        state = dealState(draw.getStartTime(), draw.getEndTime());
        stateValue = dealStateValue(draw.getStartTime(), draw.getEndTime());
    }

    public static String dealRuleValue(Integer ruleType, Integer ruleNum) {
        String value = "";
        if (StringUtils.isEmpty(ruleType)) return null;
        if (StringUtils.isEmpty(ruleNum)) return null;
        if (ruleType == DrawConst.RULE_TYPE_1) {
            value = "每人每天可抽奖" + ruleNum + "次";
        } else if (ruleType == DrawConst.RULE_TYPE_2) {
            value = "每人总共可抽奖" + ruleNum + "次";
        }
        //翻译
        value = Language.translate(value);
        return value;
    }

    public static String dealDrawTypeValue(Integer drawType) {
        String value = "";
        if (StringUtils.isEmpty(drawType)) return null;
        switch (drawType) {
            case DrawConst.DRAW_TYPE_1:
                value = "幸运抽奖";
                break;
            case DrawConst.DRAW_TYPE_2:
                value = "大转盘";
                break;
            case DrawConst.DRAW_TYPE_3:
                value = "刮刮卡";
                break;
            case DrawConst.DRAW_TYPE_4:
                value = "摇一摇";
                break;
            case DrawConst.DRAW_TYPE_5:
                value = "翻翻看";
                break;
            default:
                return null;
        }
        //翻译
        value = Language.translate(value);
        return value;
    }

    public static Integer dealState(Date startTime, Date endTime) {
        Integer value = 0;
        Date now = new Date();
        if (now.before(startTime)) {
            value = DrawConst.STATE_1;
        } else if (now.after(startTime) && now.before(endTime)) {
            value = DrawConst.STATE_2;
        } else if (now.after(endTime)) {
            value = DrawConst.STATE_3;
        }
        return value;
    }

    public static String dealStateValue(Date startTime, Date endTime) {
        String value = "";
        Date now = new Date();
        if (now.before(startTime)) {
            value = "未开始";
        } else if (now.after(startTime) && now.before(endTime)) {
            value = "进行中";
        } else if (now.after(endTime)) {
            value = "已结束";
        }
        //翻译
        value = Language.translate(value);
        return value;
    }
}