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

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.db.BaseObject;
import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
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 java.time.LocalDateTime;

/**
 * 视频稿件
 *
 * @author reghao
 * @date 2021-12-06 17:52:01
 */
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
public class VideoPost extends BaseObject<Integer> {
    private String videoId;
    private String title;
    private String description;
    private Integer categoryPid;
    private Integer categoryId;
    private int duration;
    private Boolean horizontal;
    private String coverUrl;
    private int scope;
    private int status;
    private LocalDateTime publishAt;
    private Long publishBy;

    public VideoPost(String videoId, VideoPublishSbt video, String coverUrl,
                     VideoFile videoInfo, int status, long publishAt) {
        this.videoId = videoId;
        this.title = video.getTitle();
        this.description = video.getDescription();
        this.categoryPid = video.getCategoryPid();
        this.categoryId = video.getCategoryId();
        this.duration = videoInfo.getDuration();
        this.horizontal = videoInfo.getHorizontal();
        this.coverUrl = coverUrl;
        this.scope = video.getScope();
        this.status = status;
        this.publishAt = DateTimeConverter.localDateTime(publishAt);
        this.publishBy = UserContext.getUser();
    }

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

    @Deprecated
    public VideoPost(CrawledVideo video, int categoryPid, int categoryId) {
        this.videoId = video.getVideoId();
        this.title = video.getTitle();
        this.description = video.getDescription();
        this.categoryPid = categoryPid;
        this.categoryId = categoryId;
        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 = video.getUserId();
    }

    public void update(VideoPublishSbt video, String coverUrl, long publishAt) {
        this.title = video.getTitle();
        this.description = 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);
    }
}
