#include <Weer.h>
#include <Kamer.h>
#include <Verwarming.h>
#include <Mens.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

//WEER
Weer weer = Weer();

//KAMERS
Kamer woonkamer = Kamer();      //nr 1
Kamer keuken = Kamer();         //nr 2
Kamer slaapkamer1 = Kamer();    //nr 3
Kamer slaapkamer2 = Kamer();    //nr 4
Kamer badkamer = Kamer();       //nr 5
Kamer zolderkamer = Kamer();    //nr 6

//VERWARMINGEN
Verwarming v_woonkamer = Verwarming();      boolean v_woonkamer_on = false;
Verwarming v_keuken = Verwarming();         boolean v_keuken_on = false;
Verwarming v_slaapkamer1 = Verwarming();    boolean v_slaapkamer1_on = false;
Verwarming v_slaapkamer2 = Verwarming();    boolean v_slaapkamer2_on = false;
Verwarming v_badkamer = Verwarming();       boolean v_badkamer_on = false;
Verwarming v_zolderkamer = Verwarming();    boolean v_zolderkamer_on = false;

//MENSEN
Mens michel = Mens();
Mens annemiek = Mens();
Mens kenneth = Mens();

//TIMER
int timer = 0;


void setup()
{
    //WOONKAMER
    woonkamer.setTemp(15);
    woonkamer.setVolume(72);
    woonkamer.setSurfaceArea(45, 20); 
    v_woonkamer.setLed(0);
    v_woonkamer.setWattage(2000);
    v_woonkamer.setVolume(woonkamer.getVolume());
    
    //KEUKEN
    keuken.setTemp(15);
    keuken.setVolume(18);
    keuken.setSurfaceArea(15, 6); 
    v_keuken.setLed(1);
    v_keuken.setWattage(500);
    v_keuken.setVolume(keuken.getVolume());
    
    //SLAAPKAMER1
    slaapkamer1.setTemp(15);
    slaapkamer1.setVolume(50);
    slaapkamer1.setSurfaceArea(20, 3); 
    v_slaapkamer1.setLed(2);
    v_slaapkamer1.setWattage(500);
    v_slaapkamer1.setVolume(slaapkamer1.getVolume());
    
    //SLAAPKAMER2
    slaapkamer2.setTemp(15);
    slaapkamer2.setVolume(30);
    slaapkamer2.setSurfaceArea(15, 3);
    v_slaapkamer2.setLed(3);
    v_slaapkamer2.setWattage(500);
    v_slaapkamer2.setVolume(slaapkamer2.getVolume());
    
    //BADKAMER
    badkamer.setTemp(15);
    badkamer.setVolume(30);
    badkamer.setSurfaceArea(15, 2);
    v_badkamer.setLed(4);
    v_badkamer.setWattage(500);
    v_badkamer.setVolume(badkamer.getVolume());
    
    //ZOLDERKAMER
    zolderkamer.setTemp(15);
    zolderkamer.setVolume(20);
    zolderkamer.setSurfaceArea(22, 4);
    v_zolderkamer.setLed(5);
    v_zolderkamer.setWattage(500);
    v_zolderkamer.setVolume(zolderkamer.getVolume());
    
    
    //KENNETH
    int pattern1[80][3] = {  {0,4,400}, {410,5,430}, {450,1,510}  };
    kenneth.setPattern(pattern1, 80);
    
    //MICHEL
    int pattern2[80][3] = {  {0,3,350}, {360,5,380}, {390,1,400}  };
    michel.setPattern(pattern2, 80);
    
    //ANNEMIEK
    int pattern3[80][3] = {  {0,3,380}, {390,5,410}, {420,1,600}  };
    annemiek.setPattern(pattern3, 80);
    
    //SERIELE VERBINDING STARTEN
    Serial.begin(9600);
    Serial.println("\t");
    Serial.println("Serial connection started on 9600 baud");
    Serial.println("\t");
    
    //LCD WEERGAVE
    lcd.init();
    lcd.setBacklight(HIGH);
}

void loop()
{
    
    //WEEER
    weer.calcTemp(timer);
    double temp = weer.getTemp();
    
    
    //DEBUGGING
    Serial.println("\t");
    Serial.print("\t");
    Serial.println(timer);
    Serial.println("----------------------");
    Serial.print("Buiten:\t\t");
    Serial.println(temp,4);
    
    
    //MENSEN
    int timeToEnter_kenneth[2] = {kenneth.getTimeToEnter(timer)};
    int timeToEnter_michel[2] = {michel.getTimeToEnter(timer)};
    int timeToEnter_annemiek[2] = {annemiek.getTimeToEnter(timer)};
    
    int timeToLeave_kenneth[2] = {kenneth.getTimeToLeave(timer)};
    int timeToLeave_michel[2] = {michel.getTimeToLeave(timer)};
    int timeToLeave_annemiek[2] = {annemiek.getTimeToLeave(timer)};
    
    int room_enter_kenneth = timeToEnter_kenneth[1];
    int room_enter_annemiek = timeToEnter_annemiek[1];
    int room_enter_michel = timeToEnter_michel[1];
    
    int room_leave_kenneth = timeToLeave_kenneth[1];
    int room_leave_annemiek = timeToLeave_annemiek[1];
    int room_leave_michel = timeToLeave_michel[1];
    
    int enter_kenneth = timeToEnter_kenneth[0];
    int enter_michel = timeToEnter_michel[0];
    int enter_annemiek = timeToEnter_annemiek[0];
    
    int leave_kenneth = timeToLeave_kenneth[0];
    int leave_michel = timeToLeave_michel[0];
    int leave_annemiek = timeToLeave_annemiek[0];
    
    
    //WOONKAMER
    woonkamer.calcTemp(temp);
    double temp_woonkamer = woonkamer.getTemp();
    
    v_woonkamer.setTemp(temp_woonkamer);
    temp_woonkamer = v_woonkamer.getTemp();
    woonkamer.setTemp(temp_woonkamer);
    
    double heat_woonkamer = woonkamer.getHeatFlow();
    int toTemp_woonkamer = v_woonkamer.getTimeToTemp(heat_woonkamer, 21);
    
    if(room_enter_kenneth == 1 || room_enter_michel == 1 || room_enter_michel == 1){
        v_woonkamer_on = true;
    }
    
    if(room_leave_kenneth == 1 || room_leave_michel == 1 || room_leave_michel == 1){
        v_woonkamer_on = false;
    }
    
    if(v_woonkamer_on){
        v_woonkamer.keepTemp(21);
    }
    
    bool state_woonkamer = v_woonkamer.getState();
    
    Serial.print("Woonkamer:\t");
    Serial.println(temp_woonkamer,4);
    Serial.print("Verwarming:\t");
    
    if(state_woonkamer){
      Serial.println("On");
    }
    else {
      Serial.println("Off");
    }
    
    Serial.print("Tijd tot 21:\t");
    Serial.println(toTemp_woonkamer);
    
    
    //KEUKEN
    keuken.calcTemp(temp);
    double temp_keuken = keuken.getTemp();
    
    v_keuken.setTemp(temp_keuken);
    temp_keuken = v_keuken.getTemp();
    keuken.setTemp(temp_keuken);
    
    double heat_keuken = keuken.getHeatFlow();
    int toTemp_keuken = v_keuken.getTimeToTemp(heat_keuken, 21);
    
    if(room_enter_kenneth == 1 || room_enter_michel == 1 || room_enter_michel == 1){
        v_keuken_on = true;
    }
    
    if(room_leave_kenneth == 1 || room_leave_michel == 1 || room_leave_michel == 1){
        v_keuken_on = false;
    }
    
    if(v_keuken_on){
        v_keuken.keepTemp(21);
    }
    
    bool state_keuken = v_keuken.getState();
    
    Serial.print("Keuken:\t");
    Serial.println(temp_keuken,4);
    Serial.print("Verwarming:\t");
    
    if(state_keuken){
      Serial.println("On");
    }
    else {
      Serial.println("Off");
    }
    
    Serial.print("Tijd tot 21:\t");
    Serial.println(toTemp_keuken);
        
    
    //SLAAPKAMER1
    slaapkamer1.calcTemp(temp);
    double temp_slaapkamer1 = slaapkamer1.getTemp();
    
    v_slaapkamer1.setTemp(temp_slaapkamer1);
    temp_slaapkamer1 = v_slaapkamer1.getTemp();
    slaapkamer1.setTemp(temp_slaapkamer1);
    
    double heat_slaapkamer1 = slaapkamer1.getHeatFlow();
    int toTemp_slaapkamer1 = v_slaapkamer1.getTimeToTemp(heat_slaapkamer1, 20);
    
    if(room_enter_kenneth == 1 || room_enter_michel == 1 || room_enter_michel == 1){
        v_slaapkamer1_on = true;
    }
    
    if(room_leave_kenneth == 1 || room_leave_michel == 1 || room_leave_michel == 1){
        v_slaapkamer1_on = false;
    }
    
    if(v_slaapkamer1_on){
        v_slaapkamer1.keepTemp(20);
    }
    
    bool state_slaapkamer1 = v_slaapkamer1.getState();
    
    Serial.print("Slaapkamer1:\t");
    Serial.println(temp_slaapkamer1,4);
    Serial.print("Verwarming:\t");
    
    if(state_slaapkamer1){
      Serial.println("On");
    }
    else {
      Serial.println("Off");
    }
    
    Serial.print("Tijd tot 20:\t");
    Serial.println(toTemp_slaapkamer1);
    
    
    //SLAAPKAMER1
    slaapkamer2.calcTemp(temp);
    double temp_slaapkamer2 = slaapkamer2.getTemp();
    
    v_slaapkamer2.setTemp(temp_slaapkamer2);
    temp_slaapkamer2 = v_slaapkamer2.getTemp();
    slaapkamer2.setTemp(temp_slaapkamer2);
    
    double heat_slaapkamer2 = slaapkamer2.getHeatFlow();
    int toTemp_slaapkamer2 = v_slaapkamer2.getTimeToTemp(heat_slaapkamer2, 20);
    
    if(room_enter_kenneth == 1 || room_enter_michel == 1 || room_enter_michel == 1){
        v_slaapkamer2_on = true;
    }
    
    if(room_leave_kenneth == 1 || room_leave_michel == 1 || room_leave_michel == 1){
        v_slaapkamer2_on = false;
    }
    
    if(v_slaapkamer2_on){
        v_slaapkamer2.keepTemp(20);
    }
    
    bool state_slaapkamer2 = v_slaapkamer2.getState();
    
    Serial.print("Slaapkamer2:\t");
    Serial.println(temp_slaapkamer2,4);
    Serial.print("Verwarming:\t");
    
    if(state_slaapkamer2){
      Serial.println("On");
    }
    else {
      Serial.println("Off");
    }
    
    Serial.print("Tijd tot 20:\t");
    Serial.println(toTemp_slaapkamer2);
    
    
    //BADKAMER
    badkamer.calcTemp(temp);
    double temp_badkamer = badkamer.getTemp();
    
    v_badkamer.setTemp(temp_badkamer);
    temp_badkamer = v_badkamer.getTemp();
    slaapkamer1.setTemp(temp_slaapkamer1);
    
    double heat_badkamer = badkamer.getHeatFlow();
    int toTemp_badkamer = v_badkamer.getTimeToTemp(heat_badkamer, 23);
    
    if(room_enter_kenneth == 1 || room_enter_michel == 1 || room_enter_michel == 1){
        v_badkamer_on = true;
    }
    
    if(room_leave_kenneth == 1 || room_leave_michel == 1 || room_leave_michel == 1){
        v_badkamer_on = false;
    }
    
    if(v_badkamer_on){
        v_slaapkamer1.keepTemp(23);
    }
    
    bool state_badkamer = v_badkamer.getState();
    
    Serial.print("Badkamer:\t");
    Serial.println(temp_badkamer,4);
    Serial.print("Verwarming:\t");
    
    if(state_badkamer){
      Serial.println("On");
    }
    else {
      Serial.println("Off");
    }
    
    Serial.print("Tijd tot 23:\t");
    Serial.println(toTemp_badkamer);
    
    
    //ZOLDERKAMER
    zolderkamer.calcTemp(temp);
    double temp_zolderkamer = zolderkamer.getTemp();
    
    v_zolderkamer.setTemp(temp_zolderkamer);
    temp_zolderkamer = v_zolderkamer.getTemp();
    slaapkamer1.setTemp(temp_slaapkamer1);
    
    double heat_zolderkamer = zolderkamer.getHeatFlow();
    int toTemp_zolderkamer = v_zolderkamer.getTimeToTemp(heat_zolderkamer, 18);
    
    if(room_enter_kenneth == 1 || room_enter_michel == 1 || room_enter_michel == 1){
        v_zolderkamer_on = true;
    }
    
    if(room_leave_kenneth == 1 || room_leave_michel == 1 || room_leave_michel == 1){
        v_zolderkamer_on = false;
    }
    
    if(v_zolderkamer_on){
        v_slaapkamer1.keepTemp(18);
    }
    
    bool state_zolderkamer = v_zolderkamer.getState();
    
    Serial.print("Zolderkamer:\t");
    Serial.println(temp_zolderkamer,4);
    Serial.print("Verwarming:\t");
    
    if(state_zolderkamer){
      Serial.println("On");
    }
    else {
      Serial.println("Off");
    }
    
    Serial.print("Tijd tot 18:\t");
    Serial.println(toTemp_zolderkamer);
    
    
    //SCHERM
    lcd.setCursor(0,0);
    lcd.print("Timer: ");
    lcd.print(timer);
    lcd.setCursor(0,1);
    lcd.print("Buitentemp: ");
    lcd.print(temp);
    
    //TIMER
    ++timer;
    delay(1000);
}