package com.qmth.themis.business.dto.response;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
import com.qmth.themis.business.cache.bean.ExamCacheBean;
import com.qmth.themis.business.cache.bean.ExamCourseCacheBean;
import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.ExamModeEnum;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Objects;

/**
 * @Description: 考试场次候考dto
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/8/3
 */
public class TEExamActivityWaitDto implements Serializable {

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(name = "考试场次id")
    private Long id;//考试id

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(name = "考生id")
    private Long examStudentId;//考生id

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(name = "考场id")
    private Long examActivityId;//考场id

    @ApiModelProperty(name = "科目代码")
    private String courseCode;//科目代码

    @ApiModelProperty(name = "科目名称")
    private String courseName;//科目名称

    @ApiModelProperty(name = "提前多长时间开始候考(时间戳)")
    private Long prepareTime;

    @ApiModelProperty(name = "最短考试时长")
    private Long minStartTime;

    @ApiModelProperty(name = "最大考试时长")
    private Long maxStartTime;

    @ApiModelProperty(name = "强制交卷时间")
    private Long maxFinishTime;

    @ApiModelProperty(name = "最短考试时长，相当于考试冻结时间")
    private Integer minDurationSeconds;//最短考试时长，相当于考试冻结时间(分钟)

    @ApiModelProperty(name = "最大考试时长")
    private Integer maxDurationSeconds;//最大考试时长

    @ApiModelProperty(name = "剩余考试次数")
    private Integer leftExamCount;//剩余考试次数

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(name = "考试id")
    private Long examId;//考试id

    @ApiModelProperty(name = "考试场次结束时间")
    private Long finishTime;

    @ApiModelProperty(name = "是否启用")
    private Integer enable;

    public TEExamActivityWaitDto() {

    }

    public TEExamActivityWaitDto(ExamCacheBean ec, ExamActivityCacheBean examActivityCacheBean, ExamStudentCacheBean examStudentCacheBean, Long examStudentId, ExamCourseCacheBean examCourseCacheBean,
                                 Long startTime, Long endTime, Integer openingSeconds, Integer minDurationSeconds, Integer maxDurationSeconds, Integer forceFinish) {
        this.id = examActivityCacheBean.getId();
        this.examStudentId = examStudentId;
        this.courseCode = examStudentCacheBean.getCourseCode();
        this.courseName = examStudentCacheBean.getCourseName();
        if (Objects.nonNull(examActivityCacheBean.getPrepareSeconds())) {
            this.prepareTime = startTime - (examActivityCacheBean.getPrepareSeconds() * 1000);
        } else if (Objects.nonNull(ec.getPrepareSeconds())) {
            this.prepareTime = startTime - (ec.getPrepareSeconds() * 1000);
        }
        if (Objects.nonNull(startTime)) {
            this.minStartTime = startTime;
        } else {
            this.minStartTime = ec.getStartTime();
        }
        if (Objects.nonNull(openingSeconds)) {
            openingSeconds = openingSeconds.intValue() == 0 ? SystemConstant.DEFAULT_OPENING_SECONDS : openingSeconds;
            this.maxStartTime = startTime + (openingSeconds * 1000);
        } else if (Objects.nonNull(ec.getMaxDurationSeconds())) {
            Integer openingSecondsTemp = ec.getOpeningSeconds();
            openingSecondsTemp = Objects.nonNull(openingSecondsTemp) && openingSecondsTemp.intValue() == 0 ? SystemConstant.DEFAULT_OPENING_SECONDS : openingSecondsTemp;
            this.maxStartTime = startTime + (openingSecondsTemp * 1000);
        }
        if (Objects.equals(ec.getMode(), ExamModeEnum.ANYTIME) || (Objects.equals(ec.getMode(), ExamModeEnum.TOGETHER) && forceFinish == 0)) {
            this.maxFinishTime = null;
        } else {
            this.maxFinishTime = Objects.nonNull(endTime) ? endTime : examActivityCacheBean.getFinishTime();
        }
        this.minDurationSeconds = minDurationSeconds;
        this.maxDurationSeconds = Objects.nonNull(maxDurationSeconds) ? maxDurationSeconds : ec.getMaxDurationSeconds();
        this.leftExamCount = (ec.getExamCount().intValue() - examStudentCacheBean.getAlreadyExamCount().intValue()) < 0 ? 0 : ec.getExamCount().intValue() - examStudentCacheBean.getAlreadyExamCount().intValue();
    }

    public Integer getEnable() {
        return enable;
    }

    public void setEnable(Integer enable) {
        this.enable = enable;
    }

    public Long getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(Long finishTime) {
        this.finishTime = finishTime;
    }

    public Long getExamActivityId() {
        return examActivityId;
    }

    public void setExamActivityId(Long examActivityId) {
        this.examActivityId = examActivityId;
    }

    public Long getExamId() {
        return examId;
    }

    public void setExamId(Long examId) {
        this.examId = examId;
    }

    public Integer getLeftExamCount() {
        return leftExamCount;
    }

    public void setLeftExamCount(Integer leftExamCount) {
        this.leftExamCount = leftExamCount;
    }

    public Long getPrepareTime() {
        return prepareTime;
    }

    public void setPrepareTime(Long prepareTime) {
        this.prepareTime = prepareTime;
    }

    public Long getMinStartTime() {
        return minStartTime;
    }

    public void setMinStartTime(Long minStartTime) {
        this.minStartTime = minStartTime;
    }

    public Long getMaxStartTime() {
        return maxStartTime;
    }

    public void setMaxStartTime(Long maxStartTime) {
        this.maxStartTime = maxStartTime;
    }

    public Long getMaxFinishTime() {
        return maxFinishTime;
    }

    public void setMaxFinishTime(Long maxFinishTime) {
        this.maxFinishTime = maxFinishTime;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getExamStudentId() {
        return examStudentId;
    }

    public void setExamStudentId(Long examStudentId) {
        this.examStudentId = examStudentId;
    }

    public String getCourseCode() {
        return courseCode;
    }

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public Integer getMinDurationSeconds() {
        return minDurationSeconds;
    }

    public void setMinDurationSeconds(Integer minDurationSeconds) {
        this.minDurationSeconds = minDurationSeconds;
    }

    public Integer getMaxDurationSeconds() {
        return maxDurationSeconds;
    }

    public void setMaxDurationSeconds(Integer maxDurationSeconds) {
        this.maxDurationSeconds = maxDurationSeconds;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TEExamActivityWaitDto that = (TEExamActivityWaitDto) o;
        return id.equals(that.id) && examStudentId.equals(that.examStudentId) && courseCode.equals(that.courseCode) && courseName.equals(that.courseName) && prepareTime.equals(that.prepareTime) && minStartTime.equals(that.minStartTime) && maxStartTime.equals(that.maxStartTime) && maxFinishTime.equals(that.maxFinishTime) && minDurationSeconds.equals(that.minDurationSeconds) && maxDurationSeconds.equals(that.maxDurationSeconds) && leftExamCount.equals(that.leftExamCount) && examId.equals(that.examId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, examStudentId, courseCode, courseName, prepareTime, minStartTime, maxStartTime, maxFinishTime, minDurationSeconds, maxDurationSeconds, leftExamCount, examId);
    }
}
