contract ERC20Supplier is
  Pausable,
  Destructible
{
  using SafeMath for uint;

  ERC20 public token;
  TradingWalletMapping public tradingWalletMapping;

  address public wallet;
  address public reserve;

  uint public rate;
  uint public rateDecimals;
  uint public numberOfZeroesFromLastDigit;

  event LogWithdrawAirdrop(
    address indexed _from,
    address indexed _token,
    uint amount
  );
  event LogReleaseTokensTo(
    address indexed _from,
    address indexed _to,
    uint _amount
  );
  event LogSetWallet(address indexed _wallet);
  event LogSetReserve(address indexed _reserve);
  event LogSetToken(address indexed _token);
  event LogSetRate(uint _rate);
  event LogSetRateDecimals(uint _rateDecimals);
  event LogSetNumberOfZeroesFromLastDigit(
    uint _numberOfZeroesFromLastDigit
  );

  event LogSetTradingWalletMapping(address _tradingWalletMapping);
  event LogBuyForTradingWallet(
    address indexed _tradingWallet,
    address indexed _token,
    uint _amount
  );

  /**
   * @dev Contract constructor.
   * @param _wallet Where the received ETH are transfered.
   * @param _reserve From where the ERC20 token are sent to the purchaser.
   * @param _token Deployed ERC20 token address.
   * @param _rate Purchase rate, how many ERC20 for the given ETH.
   * @param _tradingWalletMappingAddress Trading wallet adress.
   * @param _rateDecimals Define the decimals precision for the given ERC20.
   * @param _numberOfZeroesFromLastDigit Define the number of last characters to transform in zero.
   */
  constructor(
    address _wallet,
    address _reserve,
    address _token,
    uint _rate,
    address _tradingWalletMappingAddress,
    uint _rateDecimals,
    uint _numberOfZeroesFromLastDigit
  )
    public
  {
    require(_wallet != address(0), "_wallet == address(0)");
    require(_reserve != address(0), "_reserve == address(0)");
    require(_token != address(0), "_token == address(0)");
    require(
      _tradingWalletMappingAddress != 0,
      "_tradingWalletMappingAddress == 0"
    );
    wallet = _wallet;
    reserve = _reserve;
    token = ERC20(_token);
    rate = _rate;
    tradingWalletMapping = TradingWalletMapping(_tradingWalletMappingAddress);
    rateDecimals = _rateDecimals;
    numberOfZeroesFromLastDigit = _numberOfZeroesFromLastDigit;
  }

  function() public payable {
    releaseTokensTo(msg.sender);
  }

  /**
   * @dev Set wallet.
   * @param _wallet Where the ETH are redirected.
   */
  function setWallet(address _wallet)
    public
    onlyOwner
    returns (bool)
  {
    require(_wallet != address(0), "_wallet == 0");
    require(_wallet != wallet, "_wallet == wallet");
    wallet = _wallet;
    emit LogSetWallet(wallet);
    return true;
  }

  /**
   * @dev Set ERC20 reserve.
   * @param _reserve Where ERC20 are stored.
   */
  function setReserve(address _reserve)
    public
    onlyOwner
    returns (bool)
  {
    require(_reserve != address(0), "_reserve == 0");
    require(_reserve != reserve, "_reserve == reserve");
    reserve = _reserve;
    emit LogSetReserve(reserve);
    return true;
  }

  /**
   * @dev Set ERC20 token.
   * @param _token ERC20 token address.
   */
  function setToken(address _token)
    public
    onlyOwner
    returns (bool)
  {
    require(_token != address(0), "_token == 0");
    require(_token != address(token), "_token == token");
    token = ERC20(_token);
    emit LogSetToken(token);
    return true;
  }

  /**
   * @dev Set rate.
   * @param _rate Multiplier, how many ERC20 for the given ETH.
   */
  function setRate(uint _rate)
    public
    onlyOwner
    returns (bool)
  {
    require(_rate != rate, "_rate == rate");
    require(_rate != 0, "_rate == 0");
    rate = _rate;
    emit LogSetRate(rate);
    return true;
  }

   /**
   * @dev Set rate precision.
   * @param _rateDecimals The precision to represent the quantity of ERC20.
   */
  function setRateDecimals(uint _rateDecimals)
    public
    onlyOwner
    returns (bool)
  {
    rateDecimals = _rateDecimals;
    emit LogSetRateDecimals(rateDecimals);
    return true;
  }

  /**
   * @dev Set truncate from position.
   * @param _numberOfZeroesFromLastDigit The position target (eg. 1254321000000000000 => 1254000000000000000 it's position tokenDecimal - 3).
   */
  function setNumberOfZeroesFromLastDigit(uint _numberOfZeroesFromLastDigit)
    public
    onlyOwner
    returns (bool)
  {
    numberOfZeroesFromLastDigit = _numberOfZeroesFromLastDigit;
    emit LogSetNumberOfZeroesFromLastDigit(numberOfZeroesFromLastDigit);
    return true;
  }

  /**
   * @dev Eventually withdraw airdropped token.
   * @param _token ERC20 address to be withdrawed.
   */
  function withdrawAirdrop(ERC20 _token)
    public
    onlyOwner
    returns(bool)
  {
    require(address(_token) != 0, "_token address == 0");
    require(
      _token.balanceOf(this) > 0,
      "dropped token balance == 0"
    );
    uint256 airdroppedTokenAmount = _token.balanceOf(this);
    _token.transfer(msg.sender, airdroppedTokenAmount);
    emit LogWithdrawAirdrop(msg.sender, _token, airdroppedTokenAmount);
    return true;
  }

  /**
   * @dev Set TradingWalletMapping contract instance address.
   * @param _tradingWalletMappingAddress It's actually the Eidoo Exchange SC address.
   */
  function setTradingWalletMappingAddress(
    address _tradingWalletMappingAddress
  )
    public
    onlyOwner
    returns(bool)
  {
    require(
      _tradingWalletMappingAddress != address(0),
      "_tradingWalletMappingAddress == 0");
    require(
      _tradingWalletMappingAddress != address(tradingWalletMapping),
      "_tradingWalletMappingAddress == tradingWalletMapping"
    );
    tradingWalletMapping = TradingWalletMapping(_tradingWalletMappingAddress);
    emit LogSetTradingWalletMapping(tradingWalletMapping);
    return true;
  }

  /**
   * @dev Function to deposit buyed tokens directly to an Eidoo trading wallet SC.
   */
  function buyForTradingWallet()
    public
    payable
    whenNotPaused
    returns(bool)
  {
    uint amount = getAmount(msg.value);
    address _tradingWallet = tradingWalletMapping.retrieveWallet(msg.sender);
    require(
      _tradingWallet != address(0),
      "no tradingWallet associated"
    );
    require(
      token.transferFrom(reserve, address(this), amount),
      "transferFrom reserve to ERC20Supplier failed"
    );
    if (token.allowance(address(this), _tradingWallet) != 0){
      require(
        token.approve(_tradingWallet, 0),
        "approve tradingWallet to zero failed"
      );
    }
    require(
      token.approve(_tradingWallet, amount),
      "approve tradingWallet failed"
    );
    emit LogBuyForTradingWallet(_tradingWallet, token, amount);
    wallet.transfer(msg.value);
    return TradingWallet(_tradingWallet).depositERC20Token(token, amount);
  }

  /**
   * @dev Function to truncate.
   * @param _amount The amount of ERC20 to truncate.
   * @param _numberOfZeroesFromLastDigit The position target (eg. 1254321000000000000 => 1254000000000000000 it's position tokenDecimal - 3).
   */
  function truncate(
    uint _amount,
    uint _numberOfZeroesFromLastDigit
  )
    public
    pure
    returns (uint)
  {
    return (_amount
      .div(10 ** _numberOfZeroesFromLastDigit))
      .mul(10 ** _numberOfZeroesFromLastDigit
    );
  }

  /**
   * @dev Function to calculate the number of tokens to receive.
   * @param _value The number of WEI to convert in ERC20.
   */
  function getAmount(uint _value)
    public
    view
    returns(uint)
  {
    uint amount = (_value.mul(rate).div(10 ** rateDecimals));
    uint result = truncate(amount, numberOfZeroesFromLastDigit);
    return result;
  }

  /**
   * @dev Release purchased ERC20 to the buyer.
   * @param _receiver Where the ERC20 are transfered.
   */
  function releaseTokensTo(address _receiver)
    internal
    whenNotPaused
    returns (bool)
  {
    uint amount = getAmount(msg.value);
    wallet.transfer(msg.value);
    require(
      token.transferFrom(reserve, _receiver, amount),
      "transferFrom reserve to _receiver failed"
    );
    return true;
  }
}
/**
 * @title ERC20Supplier.
 * @author Eidoo SAGL.
 * @dev Distribute a fixed amount of ERC20 based on a rate from a ERC20 reserve to a _receiver for ETH.
 * Received ETH are redirected to a wallet.
 */
function setWallet(address _wallet)
  public
  onlyOwner
  returns (bool)
{
  require(_wallet != address(0), "_wallet == 0");
  require(_wallet != wallet, "_wallet == wallet");
  wallet = _wallet;
  emit LogSetWallet(wallet);
  return true;
}

/**
 * @dev Set wallet.
 * @param _wallet Where the ETH are redirected.
 */