#include "mirage_protocol.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"

namespace esphome::remote_base {

static const char *const TAG = "remote.mirage";

constexpr uint32_t HEADER_MARK_US = 8360;
constexpr uint32_t HEADER_SPACE_US = 4248;
constexpr uint32_t BIT_MARK_US = 554;
constexpr uint32_t BIT_ONE_SPACE_US = 1592;
constexpr uint32_t BIT_ZERO_SPACE_US = 545;

constexpr unsigned int MIRAGE_IR_PACKET_BIT_SIZE = 120;
// Max data bytes in packet (excluding checksum)
constexpr size_t MIRAGE_MAX_DATA_BYTES = (MIRAGE_IR_PACKET_BIT_SIZE / 8);

void MirageProtocol::encode(RemoteTransmitData *dst, const MirageData &data) {
  char hex_buf[format_hex_pretty_size(MIRAGE_MAX_DATA_BYTES)];
  ESP_LOGI(TAG, "Transmit Mirage: %s", format_hex_pretty_to(hex_buf, data.data.data(), data.data.size()));
  dst->set_carrier_frequency(38000);
  dst->reserve(5 + ((data.data.size() + 1) * 2));
  dst->mark(HEADER_MARK_US);
  dst->space(HEADER_SPACE_US);
  dst->mark(BIT_MARK_US);
  uint8_t checksum = 0;
  for (uint8_t item : data.data) {
    this->encode_byte_(dst, item);
    checksum += (item >> 4) + (item & 0xF);
  }
  this->encode_byte_(dst, checksum);
}

void MirageProtocol::encode_byte_(RemoteTransmitData *dst, uint8_t item) {
  for (uint8_t b = 0; b < 8; b++) {
    if (item & (1UL << b)) {
      dst->space(BIT_ONE_SPACE_US);
    } else {
      dst->space(BIT_ZERO_SPACE_US);
    }
    dst->mark(BIT_MARK_US);
  }
}

optional<MirageData> MirageProtocol::decode(RemoteReceiveData src) {
  if (!src.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) {
    return {};
  }
  if (!src.expect_mark(BIT_MARK_US)) {
    return {};
  }
  size_t size = src.size() - src.get_index() - 1;
  if (size < MIRAGE_IR_PACKET_BIT_SIZE * 2)
    return {};
  size = MIRAGE_IR_PACKET_BIT_SIZE * 2;
  uint8_t checksum = 0;
  MirageData out;
  while (size > 0) {
    uint8_t data = 0;
    for (uint8_t b = 0; b < 8; b++) {
      if (src.expect_space(BIT_ONE_SPACE_US)) {
        data |= (1UL << b);
      } else if (!src.expect_space(BIT_ZERO_SPACE_US)) {
        return {};
      }
      if (!src.expect_mark(BIT_MARK_US)) {
        return {};
      }
      size -= 2;
    }
    if (size > 0) {
      checksum += (data >> 4) + (data & 0xF);
      out.data.push_back(data);
    } else if (checksum != data) {
      return {};
    }
  }
  return out;
}

void MirageProtocol::dump(const MirageData &data) {
  char hex_buf[format_hex_pretty_size(MIRAGE_MAX_DATA_BYTES)];
  ESP_LOGI(TAG, "Received Mirage: %s", format_hex_pretty_to(hex_buf, data.data.data(), data.data.size()));
}

}  // namespace esphome::remote_base
