void OvenCtl::handle_config_state() {

}

void OvenCtl::check_max_duration() {
  if (disable_checks)
    return;
  if (time > profile.data[PI_TIME_MAX]) {
    error_condition |= E_TIME_MAX;
  }
}

void OvenCtl::check_Ts_duration_min() {
  if (disable_checks)
    return;
  Tl_time_end = time;
  if (time - Tl_time_start < profile.data[PI_TL_DURATION_MIN]) {
    error_condition |= E_TL_TOO_SHORT;
  }
}

void OvenCtl::check_Ts_duration_max() {
  if (disable_checks)
    return;
  if (time - Ts_time_start > profile.data[PI_TL_DURATION_MAX]) {
    error_condition |= E_TS_TOO_LONG;
  }
}


void OvenCtl::check_Tl_duration_min() {
  if (disable_checks)
    return;
  Tl_time_end = time;
  if (time - Tl_time_start < profile.data[PI_TL_DURATION_MIN]) {
    error_condition |= E_TL_TOO_SHORT;
  }
}

void OvenCtl::check_Tl_duration_max() {
  if (disable_checks)
    return;
  if (time - Tl_time_start > profile.data[PI_TL_DURATION_MAX]) {
    error_condition |= E_TL_TOO_LONG;
  }
}

void OvenCtl::check_Tp_duration_min() {
  Tp_time_end = time;
  if (time - Tp_time_start < profile.data[PI_TP_DURATION_MIN]) {
    error_condition |= E_TP_TOO_SHORT;
  }
}

void OvenCtl::check_Tp_duration_max() {
  if (disable_checks)
    return;
  if (time - Tp_time_start > profile.data[PI_TP_DURATION_MAX]) {
    error_condition |= E_TP_TOO_LONG;
  }
}

void OvenCtl::set_config_state() {
  profile.print_config_state_0();
}

void OvenCtl::set_start_state() {
  error_condition = 0;
  profile_state = START_STATE;
  get_temp();
  last_temperature = temperature;
  actual_dt = float(temperature) - last_temperature;
  set_temp(profile.data[PI_TS_MAX], profile.data[PI_TS_MAX], 0, profile.data[PI_TS_RAMP_UP_MAX]);
  lcd.clear();
}


void OvenCtl::set_preheat_state() {
//   Serial.println("Changing profile_state to PREHEAT_STATE");
  profile_state = PREHEAT_STATE;
}


void OvenCtl::set_ramp_up_state() {
  set_temp(profile.data[PI_TP_MIN], profile.data[PI_TP_DURATION_MAX], profile.data[PI_TP_RAMP_UP_MIN], profile.data[PI_TP_RAMP_UP_MAX]);
  profile_state = RAMP_UP_STATE;
}


void OvenCtl::set_tal_first_state() {
  profile_state = TAL_FIRST_STATE;
}


void OvenCtl::set_peak_state() {
  profile_state = PEAK_STATE;
}


void OvenCtl::set_tal_second_state() {
  set_temp(0, 25, -3, -6);
  profile_state = TAL_SECOND_STATE;
}


void OvenCtl::set_ramp_down_state() {
  profile_state = RAMP_DOWN_STATE;
}


void OvenCtl::set_end_state() {
  profile_state = END_STATE;
}


void OvenCtl::set_error_state() {
  if (profile_state != ERROR_STATE) {
    set_temp(0, 0, 0, 0);
    profile_state = ERROR_STATE;
  }
}


void OvenCtl::handle_start_state() {
  check_max_duration();
  if (temperature > profile.data[PI_TS_MIN]) {
    Ts_time_start = time;
    set_preheat_state();
  }
}


void OvenCtl::handle_preheat_state() {
  check_Ts_duration_max();
  check_max_duration();
  if (temperature > profile.data[PI_TS_MAX]) {
    check_Ts_duration_min();
    set_ramp_up_state();
  }
}


void OvenCtl::handle_ramp_up_state() {
  check_max_duration();
  if (temperature > profile.data[PI_TL]) {
    Tl_time_start = time;
    set_tal_first_state();
  }
}


void OvenCtl::handle_tal_first_state() {
  check_max_duration();
  check_Tl_duration_max();
  if (temperature > profile.data[PI_TP_MIN]) {
    Tp_time_start = time;
    set_peak_state();
  }
}


void OvenCtl::handle_peak_state() {
  check_Tl_duration_max();
  check_Tp_duration_max();
  if (time - Tp_time_start > profile.data[PI_TP_DURATION_MAX]) {
    check_Tp_duration_min();
    set_tal_second_state();
   }
}


void OvenCtl::handle_tal_second_state() {
  check_Tl_duration_max();
  if (temperature < profile.data[PI_TL]) {
    check_Tl_duration_min();
    set_ramp_down_state();
  }
}

void OvenCtl::handle_ramp_down_state() {
  if (temperature < profile.data[PI_TS_MIN]) {
    set_end_state();
  }
}


void OvenCtl::handle_end_state() {
}


void OvenCtl::handle_error_state() {
}

void OvenCtl::handle_profile_states() {
//   switch (profile_state) {
//       case START_STATE:
//         handle_start_state();
//         break;
//       case PREHEAT_STATE:
//         handle_preheat_state();
//         break;
//       case RAMP_UP_STATE:
//         handle_ramp_up_state();
//         break;
//       case TAL_FIRST_STATE:
//         handle_tal_first_state();
//         break;
//       case PEAK_STATE:
//         handle_peak_state();
//         break;
//       case TAL_SECOND_STATE:
//         Tl_time_end = time;
//         handle_tal_second_state();
//         break;
//       case RAMP_DOWN_STATE:
//         handle_ramp_down_state();
//         break;
//       case END_STATE:
//         handle_end_state();
//         break;
//       case ERROR_STATE:
//         handle_error_state();
//         break;
//       default:
//         break;
//   }
}