package com.slodon.b2b2c.vo.video;

import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.core.i18n.Language;
import com.slodon.b2b2c.core.util.FileUrlUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.video.pojo.SvideoVideo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *
 * 封装视频列表VO对象
 * @author wuxy
 */
@Data
public class VideoVO implements Serializable {

    private static final long serialVersionUID = 1686516750075045940L;
    @ApiModelProperty("视频id")
    private Integer videoId;

    @ApiModelProperty("视频名称")
    private String videoName;

    @ApiModelProperty("视频地址")
    private String videoPath;

    @ApiModelProperty("封面图地址")
    private String videoImage;

    @ApiModelProperty("视频简介")
    private String introduction;

    @ApiModelProperty("作者id")
    private Integer authorId;

    @ApiModelProperty("会员名称")
    private String memberName;

    @ApiModelProperty("会员昵称")
    private String memberNickname;

    @ApiModelProperty("会员头像")
    private String memberAvatar;

    @ApiModelProperty("点赞数")
    private String likeNum;

    @ApiModelProperty("评论数")
    private String commentNum;

    @ApiModelProperty("分享数")
    private String shareNum;

    @ApiModelProperty("商品数")
    private Integer goodsNum;

    @ApiModelProperty("点击数")
    private String clickNum;

    @ApiModelProperty("标签id")
    private Integer labelId;

    @ApiModelProperty("标签名称")
    private String labelName;

    @ApiModelProperty("状态:1待审核，2正常，3拒绝，4禁止显示")
    private Integer state;

    @ApiModelProperty("状态:1待审核，2正常，3拒绝，4禁止显示")
    private String stateValue;

    @ApiModelProperty("禁止理由,拒绝理由")
    private String remark;

    @ApiModelProperty("创建时间(发布时间)")
    private Date createTime;

    @ApiModelProperty("视频类型:1-视频;2-图文")
    private Integer videoType;

    @ApiModelProperty("视频类型:1-视频;2-图文")
    private String videoTypeValue;

    @ApiModelProperty("正文内容")
    private String videoContent;

    @ApiModelProperty("图片数量")
    private Integer imageNum;

    @ApiModelProperty("图文图片列表")
    private List<String> imageList;

    public VideoVO(SvideoVideo svideoVideo) {
        this.videoId = svideoVideo.getVideoId();
        this.videoName = svideoVideo.getVideoName();
        this.videoPath = FileUrlUtil.getFileUrl(svideoVideo.getVideoPath(), null);
        this.videoImage = StringUtil.isEmpty(svideoVideo.getVideoThemeImage()) ? FileUrlUtil.getFileUrl(svideoVideo.getVideoImage(), null)
                : FileUrlUtil.getFileUrl(svideoVideo.getVideoThemeImage(), null);
        this.introduction = svideoVideo.getIntroduction();
        this.authorId = svideoVideo.getAuthorId();
        this.memberName = svideoVideo.getMemberName();
        this.memberNickname = svideoVideo.getMemberNickname();
        this.memberAvatar = svideoVideo.getMemberAvatar().startsWith("http") ? svideoVideo.getMemberAvatar() : FileUrlUtil.getFileUrl(svideoVideo.getMemberAvatar(), null);
        this.likeNum = svideoVideo.getLikeNum() < 10000 ? svideoVideo.getLikeNum().toString()
                : new BigDecimal(svideoVideo.getLikeNum()).divide(new BigDecimal(10000), 1, RoundingMode.FLOOR) + "w";
        this.commentNum = svideoVideo.getCommentNum() < 10000 ? svideoVideo.getCommentNum().toString()
                : new BigDecimal(svideoVideo.getCommentNum()).divide(new BigDecimal(10000), 1, RoundingMode.FLOOR) + "w";
        this.shareNum = svideoVideo.getShareNum() < 10000 ? svideoVideo.getShareNum().toString()
                : new BigDecimal(svideoVideo.getShareNum()).divide(new BigDecimal(10000), 1, RoundingMode.FLOOR) + "w";
        this.goodsNum = svideoVideo.getGoodsNum();
        this.clickNum = svideoVideo.getClickNum() < 10000 ? svideoVideo.getClickNum().toString()
                : new BigDecimal(svideoVideo.getClickNum()).divide(new BigDecimal(10000), 1, RoundingMode.FLOOR) + "w";
        this.labelId = svideoVideo.getLabelId();
        this.labelName = svideoVideo.getLabelName();
        this.state = svideoVideo.getState();
        this.stateValue = dealStateValue(svideoVideo.getState());
        this.remark = svideoVideo.getRemark();
        this.createTime = svideoVideo.getCreateTime();
        this.videoType = svideoVideo.getVideoType();
        this.videoTypeValue = dealVideoTypeValue(svideoVideo.getVideoType());
        this.videoContent = svideoVideo.getVideoContent();
        this.imageNum = svideoVideo.getVideoImage().split(",").length;
        if (svideoVideo.getVideoType() == VideoConst.SVIDEO_TYPE_2) {
            this.imageList = dealImages(svideoVideo.getVideoImage());
        }
    }

    public static String dealStateValue(Integer state) {
        String value = "";
        if (StringUtils.isEmpty(state)) {
            return null;
        }
        switch (state) {
            case VideoConst.STATE_1:
                value = "待审核";
                break;
            case VideoConst.STATE_2:
                value = "正常";
                break;
            case VideoConst.STATE_3:
                value = "审核失败";
                break;
            case VideoConst.STATE_4:
                value = "禁止";
                break;
            default:
                break;
        }
        //翻译
        value = Language.translate(value);
        return value;
    }

    public static String dealVideoTypeValue(Integer videoType) {
        String value = "";
        if (StringUtils.isEmpty(videoType)) {
            return null;
        }
        switch (videoType) {
            case VideoConst.SVIDEO_TYPE_1:
                value = "视频";
                break;
            case VideoConst.SVIDEO_TYPE_2:
                value = "图文";
                break;
            default:
                break;
        }
        //翻译
        value = Language.translate(value);
        return value;
    }

    public static List<String> dealImages(String images) {
        List<String> list = new ArrayList<>();
        if (StringUtil.isEmpty(images)) {
            return null;
        }
        List<String> picList = Arrays.asList(images.split(","));
        picList.forEach(image -> {
            if (!StringUtil.isEmpty(image)) {
                list.add(FileUrlUtil.getFileUrl(image, null));
            }
        });
        return list;
    }
}
