package com.ssj.service.kmt.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.kmt.dto.exercise.ExerciseBookTypeDto;
import com.ssj.service.kmt.dto.exercise.ExerciseBookYearDto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ExerciseBookDto extends ExerciseBookBaseDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private String exerciseBookId;

    private String grade;

    private String subject;

    private int semester;

    private ExerciseBookBaseDto version;

    private int versionType;

    private int index;

    private int bookType;

    private int bookYear;

    private int aiRollbackImport;

    public int getAiRollbackImport() {
        return aiRollbackImport;
    }

    public void setAiRollbackImport(int aiRollbackImport) {
        this.aiRollbackImport = aiRollbackImport;
    }

    private List<ExerciseBookYearDto> bookYearList = new ArrayList<>();

    private List<ExerciseBookUnitDto> units = new ArrayList<>();

    public String getExerciseBookId() {
        return exerciseBookId;
    }

    public void setExerciseBookId(String exerciseBookId) {
        this.exerciseBookId = exerciseBookId;
    }

    public ExerciseBookDto (){

    }

    public ExerciseBookDto(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public List<ExerciseBookUnitDto> getUnits() {
        Collections.reverse(this.units);
        return this.units;
    }

    public void setUnits(List<ExerciseBookUnitDto> units) {
        this.units = units;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public int getSemester() {
        return semester;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }

    public ExerciseBookBaseDto getVersion() {
        return version;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public void setVersion(ExerciseBookBaseDto version) {
        this.version = version;
    }

    @JSONField(serialize=false)
    public int getVersionType() {
        return versionType;
    }

    public void setVersionType(int versionType) {
        this.versionType = versionType;
    }

    @JSONField(serialize=false)
    public int getBookType() {
        return bookType;
    }

    public void setBookType(int bookType) {
        this.bookType = bookType;
    }

    @JSONField(serialize=false)
    public int getBookYear() {
        return bookYear;
    }

    public void setBookYear(int bookYear) {
        this.bookYear = bookYear;
    }

    public boolean isContainBookYear (int bookYear){
        boolean result = false;
        for (ExerciseBookYearDto bookYearDto : this.bookYearList){
            if (bookYearDto.getBookYear() == bookYear){
                result = true;
                break;
            }
        }
        return result;
    }

    public void setBookYearList(List<ExerciseBookYearDto> bookYearList) {
        this.bookYearList = bookYearList;
    }
    public List<ExerciseBookYearDto> getBookYearList (){
        return bookYearList;
    }

    public void addBookYear (int bookYear, int bookType, String exerciseBookId){
        ExerciseBookYearDto dto = new ExerciseBookYearDto(bookYear);
        dto.addBookType(bookType, exerciseBookId);
        this.bookYearList.add(dto);
    }

    public void addUnit(String unitId, String unitName, String courseId, String courseName){
        boolean isExist = false;

        //新建课时对象
        ExerciseBookBaseDto course = new ExerciseBookBaseDto();
        course.setId(courseId);
        course.setName(courseName);

        for (ExerciseBookUnitDto unit : this.units){
            if (unit.getId().equals(unitId)){
                isExist = true;
                unit.addPeriod(course);
            }
        }

        //如果单元不存在，需要新增单元
        if (!isExist){
            ExerciseBookUnitDto unit = new ExerciseBookUnitDto();
            unit.setId(unitId);
            unit.setName(unitName);
            unit.addPeriod(course);
            this.units.add(0, unit);
        }
    }
}
