//
// Created by Dan Crosby on 3/12/22.
//

#include "GameStateValidator.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/ShardokException.hpp"

namespace shardok {

using Unit = net::eagle0::shardok::storage::fb::Unit;

void Validate(const GameStateW &inState, const GameStateW &outState) {
    // validate control match
    for (const Unit *unit : *outState->units()) {
        if (unit->status() != net::eagle0::shardok::storage::fb::UnitStatus_NORMAL_UNIT) continue;
        if (unit->commanding_unit_id() != -1) {
            //           int cuid = unit->commanding_unit_id();
            //            const Unit *commander = outState->units()->Get(cuid);
            //            internalAssert(
            //                    commander->attached_hero().control_info()->controlled_unit_id() ==
            //                            unit->unit_id(),
            //                    "Controlled unit and controller unit must match");
        }
        if (unit->has_attached_hero() &&
            unit->attached_hero().control_info().controlled_unit_id() != -1) {
            //  int peonId = unit->attached_hero().control_info()->controlled_unit_id();
            //            const Unit *peon = outState->units()->Get(peonId);
            // FIXME: this doesn't work for AI turns
            //            internalAssert(
            //                    peon->commanding_unit_id() == unit->unit_id(),
            //                    "Controlled unit and controller unit must match");
        }
    }

    // validate no coming back from the resolved
    for (const Unit *inUnit : *inState->units()) {
        const auto inStatus = inUnit->status();
        if (inStatus != net::eagle0::shardok::storage::fb::UnitStatus_FLED_UNIT &&
            inStatus != net::eagle0::shardok::storage::fb::UnitStatus_DESTROYED_SUMMONED_UNIT &&
            inStatus != net::eagle0::shardok::storage::fb::UnitStatus_CAPTURED_UNIT &&
            inStatus != net::eagle0::shardok::storage::fb::UnitStatus_RETREATED_UNIT &&
            inStatus != net::eagle0::shardok::storage::fb::UnitStatus_OUTLAWED_UNIT)
            continue;

        const auto outStatus = outState->units()->Get(inUnit->unit_id())->status();
        internalAssert(
                outStatus == inStatus,
                "Can't come back from fled/destroyed/captured status");
    }
}

}  // namespace shardok
