contract TokenSale is Ownable, OperatorRole {
    using SafeMath for uint256;

    bool public finished = false;
    uint256 public dailyLimit = 100000 ether;
    mapping(uint256 => uint256) public dailyThroughput;

    IPCOToken public token;
    ITokenPool public pool;

    event TransactionId(uint128 indexed id);

    /**
     * Constructor
     * @dev contract depends on IPCO Token and Token Pool
     */
    constructor(address tokenAddress, address poolAddress) public {
        addOperator(msg.sender);
        token = IPCOToken(tokenAddress);
        pool = ITokenPool(poolAddress);
    }

    /**
     * @return Today's throughput of token, tracking both minted tokens and withdraws
     */
    function throughputToday() public view returns (uint256) {
        return dailyThroughput[currentDay()];
    }

    //
    // Limited functions for operators
    //

    function mint(address to, uint256 value, uint128 txId) public onlyOperator amountInLimit(value) {
        _mint(to, value, txId);
    }

    function mintToPool(uint128 account, uint256 value, uint128 txId) public onlyOperator amountInLimit(value) {
        _mintToPool(account, value, txId);
    }

    function withdraw(uint128 account, address to, uint256 value, uint128 txId) public onlyOperator amountInLimit(value) {
        _withdraw(account, to, value, txId);
    }

    function batchMint(address[] receivers, uint256[] values, uint128[] txIds) public onlyOperator amountsInLimit(values) {
        require(receivers.length > 0, "Batch can't be empty");
        require(receivers.length == values.length && receivers.length == txIds.length, "Invalid batch");
        for (uint i; i < receivers.length; i++) {
            _mint(receivers[i], values[i], txIds[i]);
        }
    }

    function batchMintToPool(uint128[] accounts, uint256[] values, uint128[] txIds) public onlyOperator amountsInLimit(values) {
        require(accounts.length > 0, "Batch can't be empty");
        require(accounts.length == values.length && accounts.length == txIds.length, "Invalid batch");
        for (uint i; i < accounts.length; i++) {
            _mintToPool(accounts[i], values[i], txIds[i]);
        }
    }

    function batchWithdraw(uint128[] accounts, address[] receivers, uint256[] values, uint128[] txIds) public onlyOperator amountsInLimit(values) {
        require(accounts.length > 0, "Batch can't be empty.");
        require(accounts.length == values.length && accounts.length == receivers.length && accounts.length == txIds.length, "Invalid batch");
        for (uint i; i < accounts.length; i++) {
            _withdraw(accounts[i], receivers[i], values[i], txIds[i]);
        }
    }

    //
    // Unrestricted functions for the owner
    //

    function unrestrictedMint(address to, uint256 value, uint128 txId) public onlyOwner {
        _mint(to, value, txId);
    }

    function unrestrictedMintToPool(uint128 account, uint256 value, uint128 txId) public onlyOwner {
        _mintToPool(account, value, txId);
    }

    function unrestrictedWithdraw(uint128 account, address to, uint256 value, uint128 txId) public onlyOwner {
        _withdraw(account, to, value, txId);
    }

    function addOperator(address operator) public onlyOwner {
        _addOperator(operator);
    }

    function removeOperator(address operator) public onlyOwner {
        _removeOperator(operator);
    }

    function replaceOperator(address operator, address newOperator) public onlyOwner {
        _removeOperator(operator);
        _addOperator(newOperator);
    }

    function setDailyLimit(uint256 newDailyLimit) public onlyOwner {
        dailyLimit = newDailyLimit;
    }

    /**
     * Concludes the sale - unpauses the token and renounces its ownership, effectively stopping minting indefinitely.
     * @dev theoretically sale can be run with an unpaused token
     */
    function finish() public onlyOwner {
        finished = true;
        if (token.paused()) token.unpause();
        pool.complete();
        token.renounceOwnership();
    }

    //
    // Internal functions
    //

    function _mint(address to, uint256 value, uint128 txId) internal {
        token.mint(to, value);
        emit TransactionId(txId);
    }

    function _mintToPool(uint128 account, uint256 value, uint128 txId) internal {
        token.mint(address(pool), value);
        pool.allocate(account, value);
        emit TransactionId(txId);
    }

    function _withdraw(uint128 account, address to, uint256 value, uint128 txId) internal {
        pool.withdraw(account, to, value);
        emit TransactionId(txId);
    }

    function _checkLimit(uint256 value) internal {
        uint256 newValue = throughputToday().add(value);
        require(newValue <= dailyLimit, "Amount to be minted exceeds day limit.");
        dailyThroughput[currentDay()] = newValue;
    }

    //
    // Modifiers
    //

    modifier amountInLimit(uint256 value) {
        _checkLimit(value);
        _;
    }

    modifier amountsInLimit(uint256[] values) {
        uint256 sum = 0;
        for (uint i; i < values.length; i++) {
            sum = sum.add(values[i]);
        }
        _checkLimit(sum);
        _;
    }

    //
    // Private helpers
    //

    function currentDay() private view returns (uint256) {
        // solium-disable-next-line security/no-block-members, zeppelin/no-arithmetic-operations
        return now / 1 days;
    }
}
function unrestrictedMint(address to, uint256 value, uint128 txId) public onlyOwner {
    _mint(to, value, txId);
}

//
// Unrestricted functions for the owner
//