//
//  FireSpreadAction.cpp
//  eagle0_engine
//
//  Created by Dan Crosby on 1/6/19.
//  Copyright © 2019 none. All rights reserved.
//

#include "FireSpreadAction.hpp"

#include <utility>

#include "src/main/cpp/net/eagle0/shardok/library/PercentileRollOdds.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/fb_helpers/TileModifierHelpers.hpp"

namespace shardok {
FireSpreadAction::FireSpreadAction(
        const Coords& coords,
        net::eagle0::shardok::storage::fb::TileModifier existingModifier,
        PercentileRollOdds odds)
    : coords(coords),
      existingModifier(std::move(existingModifier)),
      fireSpreadOdds(std::move(odds)) {}

auto FireSpreadAction::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    const auto fireSpreadRoll = generator->Percentile();

    if (PercentileRollSucceeds(fireSpreadOdds, fireSpreadRoll)) {
        ActionResult resultProto;
        *resultProto.mutable_target_coords() = ToCoordsProto(coords);
        *resultProto.mutable_odds() = fireSpreadOdds;
        resultProto.mutable_roll()->set_value(fireSpreadRoll);

        auto* tmc = resultProto.add_changed_tile_modifiers();
        *tmc->mutable_modifiers() = fb::ToTileModifierProto(existingModifier);
        tmc->mutable_modifiers()->mutable_fire();
        *tmc->mutable_coords() = ToCoordsProto(coords);

        resultProto.set_type(net::eagle0::shardok::common::FIRE_SPREAD);

        return {resultProto};
    }

    return {};
}

auto FireSpreadAction::GetFireSpreadOdds() const -> const PercentileRollOdds& {
    return fireSpreadOdds;
}

auto FireSpreadAction::GetCoords() const -> const Coords& { return coords; }
}  // namespace shardok
