//
// Shardok-specific action adapter implementation
//

#include "ShardokAction.hpp"

#include <sstream>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-redundant-constexpr-static-def"
#include "src/main/protobuf/net/eagle0/shardok/common/command_type.pb.h"
#pragma clang diagnostic pop

namespace shardok {
namespace mcts {

ShardokAction::ShardokAction(const CommandProto& command, size_t index)
    : command_(command),
      commandIndex_(index) {}

ShardokAction::ShardokAction(CommandProto&& command, size_t index)
    : command_(std::move(command)),
      commandIndex_(index) {}

int ShardokAction::getType() const { return static_cast<int>(command_.type()); }

std::string ShardokAction::getDescription() const {
    std::stringstream ss;

    // Show player
    ss << "P" << static_cast<int>(command_.player()) << " ";

    ss << net::eagle0::shardok::common::CommandType_Name(command_.type());

    if (command_.has_actor()) { ss << " Unit:" << command_.actor().value(); }

    if (command_.has_target()) {
        const auto& coord = command_.target();
        ss << " @(" << coord.row() << "," << coord.column() << ")";
    }

    return ss.str();
}

int ShardokAction::getActorId() const {
    if (command_.has_actor()) { return command_.actor().value(); }
    return -1;
}

std::pair<int, int> ShardokAction::getTarget() const {
    if (command_.has_target()) {
        const auto& coord = command_.target();
        return std::make_pair(coord.row(), coord.column());
    }
    return std::make_pair(-1, -1);
}

std::unique_ptr<MCTSAction> ShardokAction::clone() const {
    return std::make_unique<ShardokAction>(command_, commandIndex_);
}

bool ShardokAction::equals(const MCTSAction& other) const {
    const auto* shardokOther = dynamic_cast<const ShardokAction*>(&other);
    if (!shardokOther) { return false; }

    return commandIndex_ == shardokOther->commandIndex_ &&
           command_.SerializeAsString() == shardokOther->command_.SerializeAsString();
}

}  // namespace mcts
}  // namespace shardok