#include "panasonic_heatpump_number.h"

namespace esphome {
namespace panasonic_heatpump {
static const char* const TAG = "panasonic_heatpump.number";

void PanasonicHeatpumpNumber::dump_config() {
  LOG_NUMBER("", "Panasonic Heatpump Number", this);
  delay(10);
}

void PanasonicHeatpumpNumber::control(float value) {
  int value_int = static_cast<int>(round(value));
  switch (this->id_) {
  case NumberIds::CONF_SET5:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 38);
    break;
  case NumberIds::CONF_SET6:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 39);
    break;
  case NumberIds::CONF_SET7:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 40);
    break;
  case NumberIds::CONF_SET8:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 41);
    break;
  case NumberIds::CONF_SET11:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 42);
    break;
  case NumberIds::CONF_SET15:
    this->parent_->set_command_byte(PanasonicCommand::setPlus1(value_int), 45);
    break;
  case NumberIds::CONF_SET16_01:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 75);
    break;
  case NumberIds::CONF_SET16_02:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 76);
    break;
  case NumberIds::CONF_SET16_03:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 77);
    break;
  case NumberIds::CONF_SET16_04:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 78);
    break;
  case NumberIds::CONF_SET16_05:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 79);
    break;
  case NumberIds::CONF_SET16_06:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 80);
    break;
  case NumberIds::CONF_SET16_07:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 81);
    break;
  case NumberIds::CONF_SET16_08:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 82);
    break;
  case NumberIds::CONF_SET16_09:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 86);
    break;
  case NumberIds::CONF_SET16_10:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 87);
    break;
  case NumberIds::CONF_SET16_11:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 88);
    break;
  case NumberIds::CONF_SET16_12:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 89);
    break;
  case NumberIds::CONF_SET16_13:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 90);
    break;
  case NumberIds::CONF_SET16_14:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 91);
    break;
  case NumberIds::CONF_SET16_15:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 92);
    break;
  case NumberIds::CONF_SET16_16:
    this->parent_->set_command_curve(PanasonicCommand::setPlus128(value_int), 93);
    break;
  case NumberIds::CONF_SET18:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 84);
    break;
  case NumberIds::CONF_SET19:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 94);
    break;
  case NumberIds::CONF_SET20:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 99);
    break;
  case NumberIds::CONF_SET21:
    this->parent_->set_command_byte(PanasonicCommand::setPlus1(value_int), 104);
    break;
  case NumberIds::CONF_SET22:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 105);
    break;
  case NumberIds::CONF_SET23:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 106);
    break;
  case NumberIds::CONF_SET27:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 59);
    break;
  case NumberIds::CONF_SET29:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 83);
    break;
  case NumberIds::CONF_SET36:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 65);
    break;
  case NumberIds::CONF_SET37:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 66);
    break;
  case NumberIds::CONF_SET38:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 68);
    break;
  case NumberIds::CONF_SET46:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 85);
    break;
  case NumberIds::CONF_SET47:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 95);
    break;
  case NumberIds::CONF_SET48:
    this->parent_->set_command_byte(PanasonicCommand::setPlus128(value_int), 96);
    break;
  default:
    return;
  };

  this->publish_state(state);
  this->keep_state_ = KEEP_STATE;
}

void PanasonicHeatpumpNumber::publish_new_state(const std::vector<uint8_t>& data) {
  if (this->keep_state_ > 0) {
    this->keep_state_--;
    return;
  }
  if (data.empty())
    return;

  float new_state;
  switch (this->id_) {
  case NumberIds::CONF_SET5:
    new_state = PanasonicDecode::getByteMinus128(data[38]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET6:
    new_state = PanasonicDecode::getByteMinus128(data[39]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET7:
    new_state = PanasonicDecode::getByteMinus128(data[40]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET8:
    new_state = PanasonicDecode::getByteMinus128(data[41]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET11:
    new_state = PanasonicDecode::getByteMinus128(data[42]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET15:
    new_state = PanasonicDecode::getByteMinus1(data[45]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_01:
    new_state = PanasonicDecode::getByteMinus128(data[75]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_02:
    new_state = PanasonicDecode::getByteMinus128(data[76]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_03:
    new_state = PanasonicDecode::getByteMinus128(data[77]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_04:
    new_state = PanasonicDecode::getByteMinus128(data[78]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_05:
    new_state = PanasonicDecode::getByteMinus128(data[79]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_06:
    new_state = PanasonicDecode::getByteMinus128(data[80]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_07:
    new_state = PanasonicDecode::getByteMinus128(data[81]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_08:
    new_state = PanasonicDecode::getByteMinus128(data[82]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_09:
    new_state = PanasonicDecode::getByteMinus128(data[86]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_10:
    new_state = PanasonicDecode::getByteMinus128(data[87]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_11:
    new_state = PanasonicDecode::getByteMinus128(data[88]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_12:
    new_state = PanasonicDecode::getByteMinus128(data[89]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_13:
    new_state = PanasonicDecode::getByteMinus128(data[90]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_14:
    new_state = PanasonicDecode::getByteMinus128(data[91]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_15:
    new_state = PanasonicDecode::getByteMinus128(data[92]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET16_16:
    new_state = PanasonicDecode::getByteMinus128(data[93]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET18:
    new_state = PanasonicDecode::getByteMinus128(data[84]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET19:
    new_state = PanasonicDecode::getByteMinus128(data[94]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET20:
    new_state = PanasonicDecode::getByteMinus128(data[99]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET21:
    new_state = PanasonicDecode::getByteMinus1(data[104]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET22:
    new_state = PanasonicDecode::getByteMinus128(data[105]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET23:
    new_state = PanasonicDecode::getByteMinus128(data[106]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET27:
    new_state = PanasonicDecode::getByteMinus128(data[59]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET29:
    new_state = PanasonicDecode::getByteMinus128(data[83]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET36:
    new_state = PanasonicDecode::getByteMinus128(data[65]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET37:
    new_state = PanasonicDecode::getByteMinus128(data[66]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET38:
    new_state = PanasonicDecode::getByteMinus128(data[68]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET46:
    new_state = PanasonicDecode::getByteMinus128(data[85]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET47:
    new_state = PanasonicDecode::getByteMinus128(data[95]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  case NumberIds::CONF_SET48:
    new_state = PanasonicDecode::getByteMinus128(data[96]);
    if (this->has_state() && this->state == new_state)
      return;
    break;
  default:
    return;
  };

  this->publish_state(new_state);
}
}  // namespace panasonic_heatpump
}  // namespace esphome
