package com.example.vladimirsinicyn.thermostat.model;

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

public class DaySchedule implements Serializable {

    private ArrayList<TemperatureChange> changes;

    private int dayChanges = 0;
    private int nightChanges = 0;
    public DaySchedule() {

        changes = new ArrayList<TemperatureChange>();
        dayChanges++;
        nightChanges++;
        changes.add(WeekSchedule.firstDefaultTemperatureChange);
        changes.add(WeekSchedule.lastDefaultTemperatureChange);
    }
    
    public void clear() {

        changes = new ArrayList<TemperatureChange>();
    }
    
    public TemperatureChange findClosest(Time time) {

        int min = Integer.MAX_VALUE;
        int compareTime = time.toMinutes();
        TemperatureChange closest = null;

        for (int i = 0; i < changes.size(); i++) {

            int tempValue = changes.get(i).getTime().toMinutes() - compareTime;
            if (tempValue >= 0 && tempValue < min) {

                closest = changes.get(i);
                min = tempValue;
            }
        }

        return closest;
    }

    public void addChange(TemperatureChange change) throws Exception {

        if (find(change.getTime())!= null) {

            throw new Exception("Similar times mustn't present.");
        }

        if (change.getTargetCondition() == LightCondition.DAY) {

            dayChanges++;
        } else {
            nightChanges++;
        }

        if (dayChanges == 6) {
            dayChanges--;
            throw new Exception("Too many day changes.");
        }

        if (nightChanges == 6) {
            nightChanges--;
            throw new Exception("Too many night changes.");
        }

        changes.add(change);
    }

    public void deleteTimeChange(TemperatureChange change) throws Exception {

        if (change.getTargetCondition() == LightCondition.DAY) {
            dayChanges--;
        } else {
            nightChanges--;
        }

        for (int i = 0; i < changes.size(); i++) {
            if (changes.get(i).equals(change)) {

                changes.remove(changes.get(i));
                break;
            }
        }
    }

    public TemperatureChange find(Time time) {

        for (int i = 0; i < changes.size(); i++) {
            if (time.equals(changes.get(i).getTime())) {
                return changes.get(i);
            }
        }

        return null;
    }

    public void print() {

        for (int i = 0; i < changes.size(); i++) {
            System.out.println("light condition: " + changes.get(i).getTargetCondition() + ", time: " + changes.get(i).getTime().toString());
        }
    }

    public ArrayList<TemperatureChange> getChanges() {

        return changes;
    }
}

@@ -1860,61 +1860,47 @@
     
-if (change.getTargetCondition() == LightCondition.DAY
+for (int i = 0; i %3C changes.size(); i++
 ) %7B%0A
@@ -1915,39 +1915,46 @@
     
-dayC
+if (c
 hanges
---;%0A        %7D else
+.get(i).equals(change))
  %7B%0A
+%0A
     
@@ -1957,38 +1957,58 @@
             
-nightChanges--
+    changes.remove(changes.get(i))
 ;%0A        %7D%0A
@@ -2005,27 +2005,24 @@
         
-%7D%0A%0A
         
 for (int
@@ -2017,101 +2017,109 @@
     
-for (int i = 0; i %3C changes.size(); i++) %7B%0A            if (changes.get(i).equals(change))
+if (change.getType() == ChangeType.DAY) %7B%0A                    dayChanges--;%0A                %7D else
  %7B%0A
-%0A
     
@@ -2134,39 +2134,45 @@
     
-changes.remove(changes.get(i));
+    nightChanges--;%0A                %7D
 %0A   

Update DaySchedule.java