package cn.reghao.tnb.content.app.vod.model.po;

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.web.db.BaseObject;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.api.constant.PostScope;
import cn.reghao.tnb.content.api.constant.VideoStatus;
import cn.reghao.tnb.content.api.dto.VideoPublishSbt;
import cn.reghao.tnb.content.api.dto.spider.CrawledVideo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 视频稿件
 *
 * @author reghao
 * @date 2021-12-06 17:52:01
 */
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
public class VideoPost extends BaseObject<Integer> {
    @NotBlank
    private String videoId;
    @NotBlank
    @Size(min = 1, max = 64, message = "视频名字不能超过 64 个字符")
    private String title;
    @NotNull
    @Size(max = 10_000, message = "视频描述不能超过 10_000 个字符")
    private String description;
    @NotNull
    private Integer categoryPid;
    @NotNull
    private Integer categoryId;
    @NotNull
    private Integer duration;
    @NotNull
    private Boolean horizontal;
    @NotBlank
    private String coverUrl;
    @NotNull
    private Integer scope;
    @NotNull
    private Integer status;
    @NotNull
    private LocalDateTime publishAt;
    @NotNull
    private Long publishBy;

    public VideoPost(String videoId, String title, VideoFile videoInfo) {
        this.videoId = videoId;
        this.title = title;
        this.description = "";
        this.categoryPid = 0;
        this.categoryId = 0;
        this.duration = videoInfo.getDuration();
        this.horizontal = videoInfo.getHorizontal();
        this.coverUrl = "";
        this.scope = PostScope.PRIVATE.getCode();
        this.status = VideoStatus.censor.getCode();
        this.publishAt = DateTimeConverter.localDateTime(System.currentTimeMillis());
        this.publishBy = UserContext.getUserId();
    }

    @Deprecated
    public VideoPost(CrawledVideo video, long publishBy) {
        this.videoId = video.getVideoId();
        this.title = video.getTitle();
        this.description = video.getDescription();
        this.categoryPid = video.getCategoryPid();
        this.categoryId = video.getCategoryId();
        this.scope = PostScope.PUBLIC.getCode();
        this.status = VideoStatus.noVideoFile.getCode();
        this.horizontal = video.getHorizontal();
        this.duration = video.getDuration();
        this.coverUrl = video.getCoverUrl();
        this.publishAt = DateTimeConverter.localDateTime(video.getPubDate());
        this.publishBy = publishBy;
    }

    public void update(VideoPublishSbt video, String coverUrl, long publishAt) {
        this.title = video.getTitle();
        this.description = video.getDescription() != null ? video.getDescription() : "";
        this.categoryPid = video.getCategoryPid();
        this.categoryId = video.getCategoryId();
        this.coverUrl = coverUrl;
        this.scope = video.getScope();
        this.status = VideoStatus.publish.getCode();
        this.publishAt = DateTimeConverter.localDateTime(publishAt);
    }
}
