#include "select.h"
#include "esphome/core/log.h"
#include <cstring>

namespace esphome {
namespace select {

static const char *const TAG = "select";

void Select::publish_state(const std::string &state) {
  auto index = this->index_of(state);
  const auto *name = this->get_name().c_str();
  if (index.has_value()) {
    this->set_has_state(true);
    this->state = state;
    ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", name, state.c_str(), index.value());
    this->state_callback_.call(state, index.value());
  } else {
    ESP_LOGE(TAG, "'%s': invalid state for publish_state(): %s", name, state.c_str());
  }
}

void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&callback) {
  this->state_callback_.add(std::move(callback));
}

bool Select::has_option(const std::string &option) const { return this->index_of(option).has_value(); }

bool Select::has_index(size_t index) const { return index < this->size(); }

size_t Select::size() const {
  const auto &options = traits.get_options();
  return options.size();
}

optional<size_t> Select::index_of(const std::string &option) const {
  const auto &options = traits.get_options();
  for (size_t i = 0; i < options.size(); i++) {
    if (strcmp(options[i], option.c_str()) == 0) {
      return i;
    }
  }
  return {};
}

optional<size_t> Select::active_index() const {
  if (this->has_state()) {
    return this->index_of(this->state);
  } else {
    return {};
  }
}

optional<std::string> Select::at(size_t index) const {
  if (this->has_index(index)) {
    const auto &options = traits.get_options();
    return std::string(options.at(index));
  } else {
    return {};
  }
}

const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }

}  // namespace select
}  // namespace esphome
