contract Crowdsale is Ownable {
    
  using SafeMath for uint;
    

  MODICOIN public token = new MODICOIN();


  uint per_p_sale;
  
  uint per_sale;
  
  uint start_ico;
 
 uint rate;
uint256 public ini_supply;
  function Crowdsale() public {
    rate = 60000000 * 1 ether;
    
    ini_supply = 800000000 * 1 ether;
    
    uint256 ownerTokens = 200000000 * 1 ether;

    token.transfer(owner, ownerTokens);
  }

  uint public refferBonus = 7;
  function createTokens(address refferAddress)  payable public {

    uint tokens = rate.mul(msg.value).div(1 ether);
    uint refferGetToken = tokens.div(100).mul(refferBonus);
    token.transfer(msg.sender, tokens);
    token.transfer(refferAddress, refferGetToken);
    
  }
  function createTokensWithoutReffer()  payable public {

    uint tokens = rate.mul(msg.value).div(1 ether);
    token.transfer(msg.sender, tokens);
    
  }
  function refferBonusFunction(uint bonuseInpercentage) public onlyOwner{
      refferBonus=bonuseInpercentage;
  }
  function airdropTokens(address[] _recipient,uint TokenAmount) public onlyOwner {
    for(uint i = 0; i< _recipient.length; i++)
    {
          require(token.transfer(_recipient[i],TokenAmount));
    }
  }
  //Just in case, owner wants to transfer Tokens from contract to owner address
    function manualWithdrawToken(uint256 _amount) onlyOwner public {
        uint tokenAmount = _amount * (1 ether);
        token.transfer(msg.sender, tokenAmount);
      }
  function() external payable {
    uint160 refferAddress = 0;
    uint160 b = 0;

    if(msg.data.length == 0)
    {
        createTokensWithoutReffer();
    }
    else
    {
        for (uint8 i = 0; i < 20; i++) {
            refferAddress *= 256;
            b = uint160(msg.data[i]);
            refferAddress += (b);
        }
        createTokens(address(refferAddress));
    }
    forwardEherToOwner();
  }
  //Automatocally forwards ether from smart contract to owner address
    function forwardEherToOwner() internal {
        if (!owner.send(msg.value)) {
          revert();
        }
      }
    
}
function manualWithdrawToken(uint256 _amount) onlyOwner public {
    uint tokenAmount = _amount * (1 ether);
    token.transfer(msg.sender, tokenAmount);
  }

//Just in case, owner wants to transfer Tokens from contract to owner address