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

#include "FireOutAction.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 {
FireOutAction::FireOutAction(
        Coords coords,
        net::eagle0::shardok::storage::fb::TileModifier existingModifier,
        PercentileRollOdds odds)
    : coords(coords),
      existingModifier(existingModifier),
      fireOutOdds(std::move(odds)) {}

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

    ActionResult resultProto;
    *resultProto.mutable_target_coords() = ToCoordsProto(coords);
    *resultProto.mutable_odds() = fireOutOdds;
    resultProto.mutable_roll()->set_value(fireOutRoll);

    if (PercentileRollSucceeds(fireOutOdds, fireOutRoll)) {
        auto* tmc = resultProto.add_changed_tile_modifiers();
        *tmc->mutable_modifiers() = fb::ToTileModifierProto(existingModifier);
        tmc->mutable_modifiers()->clear_fire();
        *tmc->mutable_coords() = ToCoordsProto(coords);

        resultProto.set_type(net::eagle0::shardok::common::FIRE_OUT_NATURALLY);
    } else {
        resultProto.set_type(net::eagle0::shardok::common::FIRE_CONTINUES);
    }

    return {resultProto};
}

auto FireOutAction::GetFireOutOdds() const -> const PercentileRollOdds& { return fireOutOdds; }
}  // namespace shardok
