contract TokenController is Ownable {
    using SafeMath for uint256;

    MintableToken public targetToken;
    address public votingAddr;
    address public tokensaleManagerAddr;

    State public state;

    enum State {
        Init,
        Tokensale,
        Public
    }

    /// @dev The deployer must change the ownership of the target token to this contract.
    /// @param _targetToken : The target token this contract manage the rights to mint.
    /// @return 
    constructor (
        MintableToken _targetToken
    ) public {
        targetToken = MintableToken(_targetToken);
        state = State.Init;
    }

    /// @dev Mint and distribute specified amount of tokens to an address.
    /// @param to An address that receive the minted tokens.
    /// @param amount Amount to mint.
    /// @return True if the distribution is successful, revert otherwise.
    function mint (address to, uint256 amount) external returns (bool) {
        /*
          being called from voting contract will be available in the future
          ex. if (state == State.Public && msg.sender == votingAddr) 
        */

        if ((state == State.Init && msg.sender == owner) ||
            (state == State.Tokensale && msg.sender == tokensaleManagerAddr)) {
            return targetToken.mint(to, amount);
        }

        revert();
    }

    /// @dev Change the phase from "Init" to "Tokensale".
    /// @param _tokensaleManagerAddr A contract address of token-sale.
    /// @return True if the change of the phase is successful, revert otherwise.
    function openTokensale (address _tokensaleManagerAddr)
        external
        onlyOwner
        returns (bool)
    {
        /* check if the owner of the target token is set to this contract */
        require(MintableToken(targetToken).owner() == address(this));
        require(state == State.Init);
        require(_tokensaleManagerAddr != address(0x0));

        tokensaleManagerAddr = _tokensaleManagerAddr;
        state = State.Tokensale;
        return true;
    }

    /// @dev Change the phase from "Tokensale" to "Public". This function will be
    ///      cahnged in the future to receive an address of voting contract as an
    ///      argument in order to handle the result of minting proposal.
    /// @return True if the change of the phase is successful, revert otherwise.
    function closeTokensale () external returns (bool) {
        require(state == State.Tokensale && msg.sender == tokensaleManagerAddr);

        state = State.Public;
        return true;
    }

    /// @dev Check if the state is "Init" or not.
    /// @return True if the state is "Init", false otherwise.
    function isStateInit () external view returns (bool) {
        return (state == State.Init);
    }

    /// @dev Check if the state is "Tokensale" or not.
    /// @return True if the state is "Tokensale", false otherwise.
    function isStateTokensale () external view returns (bool) {
        return (state == State.Tokensale);
    }

    /// @dev Check if the state is "Public" or not.
    /// @return True if the state is "Public", false otherwise.
    function isStatePublic () external view returns (bool) {
        return (state == State.Public);
    }
}
function openTokensale (address _tokensaleManagerAddr)
    external
    onlyOwner
    returns (bool)
{
    /* check if the owner of the target token is set to this contract */
    require(MintableToken(targetToken).owner() == address(this));
    require(state == State.Init);
    require(_tokensaleManagerAddr != address(0x0));

    tokensaleManagerAddr = _tokensaleManagerAddr;
    state = State.Tokensale;
    return true;
}

/// @dev Change the phase from "Init" to "Tokensale".
/// @param _tokensaleManagerAddr A contract address of token-sale.
/// @return True if the change of the phase is successful, revert otherwise.