#include "commands.h"
#include "td/telegram/td_api.h"
#include <cstring>
#include <spdlog/spdlog.h>
#include <string_view>
#include <vector>

void cmd::handle_regular_message(context *ctx, td_api::message &msg) {
  if (msg.content_->get_id() == td_api::messageText::ID) {
    std::string text = static_cast<td_api::messageText&>(*msg.content_).text_->text_;
    if (std::strncmp(text.c_str(), "/shorten ", 9) == 0) {
      std::string_view param(text.begin() + 9, text.end());
      size_t nextSpace = param.find(' ');
      if (nextSpace != std::string_view::npos)
        param.remove_suffix(param.size() - nextSpace);
      spdlog::info("Command /shorten received with parameter '{}'", param);
    } else if (std::strncmp(text.c_str(), "/shorten", 8) == 0) {
      std::string textRaw("usage: /shorten <url>");
      std::vector<td_api::object_ptr<td_api::textEntity>> empty;
      auto text = static_cast<td_api::object_ptr<td_api::InputMessageContent>>(td_api::make_object<td_api::inputMessageText>(td_api::make_object<td_api::formattedText>(textRaw, std::move(empty)), nullptr, false));
      ctx->tg->send_query(td_api::make_object<td_api::sendMessage>(msg.chat_id_, msg.message_thread_id_, nullptr, nullptr, nullptr, std::move(text)), {});
    }
  }
}