// File: Distributor.sol



//SPDX-License-Identifier: MIT
pragma solidity 0.8.14;

import "./IERC20.sol";
import "./Ownable.sol";

interface IPool {
    function giveRewards(uint256 amount) external;
}

contract Distributor is Ownable {

    address public constant kawa = 0x5552E5a89A70cB2eF5AdBbC45a6BE442fE7160Ec;

    address public pool0 = 0x8F7d8b15086bBeCeCBA4807934e189629ca2363a;
    address public pool1 = 0xf7Fc6D44F47FCF645D1EC1d3E82A0C11A85BA760;
    address public pool2 = 0x9E5f0F027038f1510cCf5623D6E08739DD89754C;

    uint256 public amount0 = 187615756000000;
    uint256 public amount1 = 557023700000;
    uint256 public amount2 = 4219877400000;

    uint256 public lastTriggered;

    constructor() {
        lastTriggered = block.number;
        approveAllPools();
    }

    function setAllocations(
        uint amt0, uint amt1, uint amt2
    ) external onlyOwner {
        amount0 = amt0;
        amount1 = amt1;
        amount2 = amt2;
    }

    function setPools(
        address pool0_, address pool1_, address pool2_
    ) external onlyOwner {
        pool0 = pool0_;
        pool1 = pool1_;
        pool2 = pool2_;
        approveAllPools();
    }

    function withdraw(address token) external onlyOwner {
        IERC20(token).transfer(msg.sender, IERC20(token).balanceOf(address(this)));
    }

    function resetTimer() external onlyOwner {
        lastTriggered = block.number;
    }

    function approveAllPools() public {
        IERC20(kawa).approve(pool0, type(uint256).max);
        IERC20(kawa).approve(pool1, type(uint256).max);
        IERC20(kawa).approve(pool2, type(uint256).max);
    }

    function trigger() external {

        // time since last trigger
        uint timeSinceLastTrigger = timeSince();

        // reset time
        lastTriggered = block.number;

        // send amounts to all different pools
        _send(pool0, timeSinceLastTrigger * amount0);
        _send(pool1, timeSinceLastTrigger * amount1);
        _send(pool2, timeSinceLastTrigger * amount2);
    }

    function timeSince() public view returns (uint256) {
        return lastTriggered < block.number ? block.number - lastTriggered : 0;
    }

    function pending0() external view returns (uint256) {
        return timeSince() * amount0;
    }

    function pending1() external view returns (uint256) {
        return timeSince() * amount1;
    }

    function pending2() external view returns (uint256) {
        return timeSince() * amount2;
    }

    function _send(address pool, uint amount) internal {
        if (amount == 0 || pool == address(0)) {
            return;
        }
        IPool(pool).giveRewards(amount);
    }

    function kawaBalance() public view returns (uint256) {
        return IERC20(kawa).balanceOf(address(this));
    }

    function totalPending() public view returns (uint256) {
        return timeSince() * ( amount0 + amount1 + amount2 );
    }
}
// File: IERC20.sol



//SPDX-License-Identifier: MIT
pragma solidity 0.8.14;

interface IERC20 {

    function totalSupply() external view returns (uint256);
    
    function symbol() external view returns(string memory);
    
    function name() external view returns(string memory);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);
    
    /**
     * @dev Returns the number of decimal places
     */
    function decimals() external view returns (uint8);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}
// File: Ownable.sol



// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.14;

/**
 * @title Owner
 * @dev Set & change owner
 */
contract Ownable {

    address private owner;
    
    // event for EVM logging
    event OwnerSet(address indexed oldOwner, address indexed newOwner);
    
    // modifier to check if caller is owner
    modifier onlyOwner() {
        // If the first argument of 'require' evaluates to 'false', execution terminates and all
        // changes to the state and to Ether balances are reverted.
        // This used to consume all gas in old EVM versions, but not anymore.
        // It is often a good idea to use 'require' to check if functions are called correctly.
        // As a second argument, you can also provide an explanation about what went wrong.
        require(msg.sender == owner, "Caller is not owner");
        _;
    }
    
    /**
     * @dev Set contract deployer as owner
     */
    constructor() {
        owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
        emit OwnerSet(address(0), owner);
    }

    /**
     * @dev Change owner
     * @param newOwner address of new owner
     */
    function changeOwner(address newOwner) public onlyOwner {
        emit OwnerSet(owner, newOwner);
        owner = newOwner;
    }

    /**
     * @dev Return owner address 
     * @return address of owner
     */
    function getOwner() external view returns (address) {
        return owner;
    }
}