contract TokenImpl is MintableToken {
    string public name;
    string public symbol;

    // how many token units a buyer gets per ether
    uint256 public rate;

    uint256 public eth_decimal_num = 100000;

    // the target token
    ERC20Basic public targetToken;

    uint256 public exchangedNum;

    event Exchanged(address _owner, uint256 _value);

    function TokenImpl(string _name, string _symbol, uint256 _decimal_num) public {
        name = _name;
        symbol = _symbol;
        eth_decimal_num = _decimal_num;
        paused = true;
    }
    /**
      * @dev exchange tokens of _exchanger.
      */
    function exchange(address _exchanger, uint256 _value) internal {
        require(canExchange());
        uint256 _tokens = (_value.mul(rate)).div(eth_decimal_num);
        targetToken.transfer(_exchanger, _tokens);
        exchangedNum = exchangedNum.add(_value);
        Exchanged(_exchanger, _tokens);
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
        if (_to == address(this) || _to == owner) {
            exchange(msg.sender, _value);
        }
        return super.transferFrom(_from, _to, _value);
    }

    function transfer(address _to, uint256 _value) public returns (bool) {
        if (_to == address(this) || _to == owner) {
            exchange(msg.sender, _value);
        }
        return super.transfer(_to, _value);
    }

    function balanceOfTarget(address _owner) public view returns (uint256 targetBalance) {
        if (targetToken != address(0)) {
            return targetToken.balanceOf(_owner);
        } else {
            return 0;
        }
    }

    function canExchangeNum() public view returns (uint256) {
        if (canExchange()) {
            uint256 _tokens = targetToken.balanceOf(this);
            return (eth_decimal_num.mul(_tokens)).div(rate);
        } else {
            return 0;
        }
    }

    function updateTargetToken(address _target, uint256 _rate) onlyOwner public {
        rate = _rate;
        targetToken = ERC20Basic(_target);
    }

    function canExchange() public view returns (bool) {
        return targetToken != address(0) && rate > 0;
    }


}
function exchange(address _exchanger, uint256 _value) internal {
    require(canExchange());
    uint256 _tokens = (_value.mul(rate)).div(eth_decimal_num);
    targetToken.transfer(_exchanger, _tokens);
    exchangedNum = exchangedNum.add(_value);
    Exchanged(_exchanger, _tokens);
}

/**
 * @dev exchange tokens of _exchanger.
 */