package com.ssj.service.conch.schoolmaster.dto;


import com.ssj.bean.kmt.kmt.domain.KmtCourse;
import com.ssj.framework.weixin.util.DateUtil;

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

/**
 * @author chchen
 * @date 2018/9/29 14:31
 */
public class CourseDateDataDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private String date;

    /**
     * 当天课程状态， 1已上课，2过时未上课，3未上课
     */
    private int type;

    private List<CourseTimeDto> courses;

    public CourseDateDataDto (){
        this.courses = new ArrayList<>();
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public List<CourseTimeDto> getCourses() {
        return courses;
    }

    public void setCourses(List<CourseTimeDto> courses) {
        this.courses = courses;
    }

    public void addCourses (KmtCourse kc, String currentCourseId){
        CourseTimeDto course = new CourseTimeDto();
        course.setCourseId(kc.getId());
        course.setStartTime(kc.getStartTime());
        course.setEndTime(kc.getEndTime());

        //是否是当前那一节课
        if(kc.getId().equals(currentCourseId)){
            course.setStatus(1);
        }

        if (kc.getPlanStatus() > 3){
            this.type = 1;
            course.setType(1);
        } else if ((kc.getCourseTime() + " " + kc.getStartTime() +":00")
                .compareTo(DateUtil.getCurrentDateString("yyyy-MM-dd HH:mm:ss")) <= 0){
            this.type = 2;
            course.setType(2);
        } else {
            this.type = 3;
            course.setType(3);
        }

        if (this.courses.size() > 1){
            //多个元素
            boolean endFlag = true;
            for (int i = 1; i < this.courses.size(); i++){
                if (this.courses.get(i - 1).getStartTime().compareTo(course.getStartTime()) >= 0
                        && this.courses.get(i).getStartTime().compareTo(course.getStartTime()) < 0){
                    this.courses.add(i, course);
                    endFlag = false;
                }
            }
            if (endFlag){
                this.courses.add(course);
            }
        } else if (this.courses.size() > 0){
            //有一个元素时
            if (this.courses.get(0).getStartTime().compareTo(course.getStartTime()) <= 0){
                this.courses.add(course);
            } else {
                this.courses.add(0, course);
            }
        } else {
            //没有元素时直接添加
            this.courses.add(course);
        }
    }
}
