package com.example.nhocg.myseftcalendar.datas.entities;

import android.os.Parcel;
import android.os.Parcelable;

import com.example.nhocg.myseftcalendar.helper.Constant;

import java.util.Calendar;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

/**
 * Created by nhocg on 9/20/2017.
 */

public class WeekLesson implements Parcelable {
    private int week_lesson_id = Constant.INT_NONE_VALUE;
    @Nonnull
    private String subject_id = Constant.STRING_EMPTY;
    private int repeat_id = Constant.INT_NONE_VALUE;
    @Nonnegative
    private int week_lesson_day_start = 0;
    @Nonnegative
    private int week_lesson_day_end = week_lesson_day_start;
    @Nonnegative
    private int week_lesson_day_index = 0;

    public WeekLesson() {
    }

    //region params constructor

    /**
     * Full params constructor
     */
    public WeekLesson(int week_lesson_id, @Nonnull String subject_id, int repeat_id, @Nonnegative int week_lesson_day_start,
                      @Nonnegative int week_lesson_day_end, @Nonnegative int week_lesson_day_index) {
        if (week_lesson_id > 0) {
            this.week_lesson_id = week_lesson_id;
        } else {
            this.week_lesson_id = Constant.INT_NONE_VALUE;
        }
        this.subject_id = subject_id;
        if (repeat_id > 0) {
            this.repeat_id = repeat_id;
        } else {
            this.repeat_id = Constant.INT_NONE_VALUE;
        }
        if (week_lesson_day_start >= 0) {
            this.week_lesson_day_start = week_lesson_day_start;
        }else {
            this.week_lesson_day_start = 0;
        }
        if (!(week_lesson_day_end >= this.week_lesson_day_start)) {
            this.week_lesson_day_end = week_lesson_day_end;
        }else {
            this.week_lesson_day_end = this.week_lesson_day_start;
        }
        if (week_lesson_day_index >= Calendar.SUNDAY && week_lesson_day_index <= Calendar.SATURDAY) {
            this.week_lesson_day_index = week_lesson_day_index;
        }
    }

    /**
     * Constructor without week lesson id
     */
    public WeekLesson(@Nonnull String subject_id, int repeat_id, @Nonnegative int week_lesson_day_start, @Nonnegative int week_lesson_day_end,
                      @Nonnegative int week_lesson_day_index) {
        this(Constant.INT_NONE_VALUE, subject_id, repeat_id, week_lesson_day_start, week_lesson_day_end, week_lesson_day_index);
    }
    //endregion

    //region getter, setter
    public int getWeek_lesson_id() {
        return week_lesson_id;
    }

    public void setWeek_lesson_id(int week_lesson_id) {
        if (week_lesson_id > 0) {
            this.week_lesson_id = week_lesson_id;
        } else {
            this.week_lesson_id = Constant.INT_NONE_VALUE;
        }
    }

    @Nonnull
    public String getSubject_id() {
        return subject_id;
    }

    public void setSubject_id(@Nonnull String subject_id) {
        this.subject_id = subject_id;
    }

    public int getRepeat_id() {
        return repeat_id;
    }

    public void setRepeat_id(int repeat_id) {
        if (repeat_id > 0) {
            this.repeat_id = repeat_id;
        } else {
            this.repeat_id = Constant.INT_NONE_VALUE;
        }
    }

    @Nonnegative
    public int getWeek_lesson_day_start() {
        return week_lesson_day_start;
    }

    public void setWeek_lesson_day_start(@Nonnegative int week_lesson_day_start) {
        if (week_lesson_day_start >= 0) {
            this.week_lesson_day_start = week_lesson_day_start;
            if (!(this.week_lesson_day_end >= this.week_lesson_day_start)) {
                this.week_lesson_day_end = this.week_lesson_day_start;
            }
        }
    }

    @Nonnegative
    public int getWeek_lesson_day_end() {
        return week_lesson_day_end;
    }

    public void setWeek_lesson_day_end(@Nonnegative int week_lesson_day_end) {
        if (week_lesson_day_end >= this.week_lesson_day_start) {
            this.week_lesson_day_end = week_lesson_day_end;
        }
    }

    @Nonnegative
    public int getWeek_lesson_day_index() {
        return week_lesson_day_index;
    }

    public void setWeek_lesson_day_index(@Nonnegative int week_lesson_day_index) {
        if (week_lesson_day_index >= Calendar.SUNDAY && week_lesson_day_index <= Calendar.SATURDAY) {
            this.week_lesson_day_index = week_lesson_day_index;
        }
    }
    //endregion

    //region another

    @Override
    public String toString() {
        return "WeekLesson{" +
                "week_lesson_id=" + week_lesson_id +
                ", subject_id='" + subject_id + '\'' +
                ", repeat_id=" + repeat_id +
                ", week_lesson_day_start=" + week_lesson_day_start +
                ", week_lesson_day_end=" + week_lesson_day_end +
                ", week_lesson_day_index=" + week_lesson_day_index +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof WeekLesson)) return false;

        WeekLesson that = (WeekLesson) o;

        if (week_lesson_id != that.week_lesson_id) return false;
        if (repeat_id != that.repeat_id) return false;
        if (week_lesson_day_start != that.week_lesson_day_start) return false;
        if (week_lesson_day_end != that.week_lesson_day_end) return false;
        if (week_lesson_day_index != that.week_lesson_day_index) return false;
        return subject_id.equals(that.subject_id);
    }

    @Override
    public int hashCode() {
        int result = week_lesson_id;
        result = 31 * result + subject_id.hashCode();
        result = 31 * result + repeat_id;
        result = 31 * result + week_lesson_day_start;
        result = 31 * result + week_lesson_day_end;
        result = 31 * result + week_lesson_day_index;
        return result;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.week_lesson_id);
        dest.writeString(this.subject_id);
        dest.writeInt(this.repeat_id);
        dest.writeInt(this.week_lesson_day_start);
        dest.writeInt(this.week_lesson_day_end);
        dest.writeInt(this.week_lesson_day_index);
    }

    protected WeekLesson(Parcel in) {
        this.week_lesson_id = in.readInt();
        this.subject_id = in.readString();
        this.repeat_id = in.readInt();
        this.week_lesson_day_start = in.readInt();
        this.week_lesson_day_end = in.readInt();
        this.week_lesson_day_index = in.readInt();
    }

    public static final Creator<WeekLesson> CREATOR = new Creator<WeekLesson>() {
        @Override
        public WeekLesson createFromParcel(Parcel source) {
            return new WeekLesson(source);
        }

        @Override
        public WeekLesson[] newArray(int size) {
            return new WeekLesson[size];
        }
    };
    //endregion

}