// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {Army} from "../src/RaidGeldStructs.sol";

library RaidGeldUtils {
    uint256 public constant PRECISION = 10000;
    uint256 constant PRICE_FACTOR = 11500;

    // base price * (0.00666) * 11 per each next unit
    uint256 constant MOLOCH_DENIER_PROFIT = 2533;
    uint256 constant APPRENTICE_PROFIT = 27863;
    uint256 constant ANOINTED_PROFIT = 306493;
    uint256 constant CHAMPION_PROFIT = 3371423;

    // each costs 10 times plus a bit more
    uint256 constant MOLOCH_DENIER_BASE_COST = 380000;
    uint256 constant APPRENTICE_BASE_COST = 3420000;
    uint256 constant ANOINTED_BASE_COST = 30096000;
    uint256 constant CHAMPION_BASE_COST = 255816000;

    function calculateUnitPrice(uint8 unit, uint16 currentLevel, uint16 units) internal pure returns (uint256) {
        require(unit <= 3, "No matching unit found");
        uint256 rollingPriceCalculation = MOLOCH_DENIER_BASE_COST;
        uint256 price = 0;
        if (unit == 1) {
            rollingPriceCalculation = APPRENTICE_BASE_COST;
        } else if (unit == 2) {
            rollingPriceCalculation = ANOINTED_BASE_COST;
        } else if (unit == 3) {
            rollingPriceCalculation = CHAMPION_BASE_COST;
        }

        // Each level costs 15% more than previous
        for (uint256 i = 0; i < currentLevel + units; i++) {
            if (i >= currentLevel) {
                price += rollingPriceCalculation;
            }
            rollingPriceCalculation = rollingPriceCalculation * PRICE_FACTOR / PRECISION;
        }
        return price;
    }

    function calculateProfitsPerSecond(Army memory army) internal pure returns (uint256) {
        // Each next unit scales progressivelly better

        uint256 moloch_denier_profit = army.moloch_denier.level * MOLOCH_DENIER_PROFIT;
        uint256 apprentice_profit = army.apprentice.level * APPRENTICE_PROFIT;
        uint256 anointed_profit = army.anointed.level * ANOINTED_PROFIT;
        uint256 champion_profit = army.champion.level * CHAMPION_PROFIT;

        return moloch_denier_profit + apprentice_profit + anointed_profit + champion_profit;
    }
}
