contract StrikeTokenCrowdsale is Ownable, Pausable {
    using SafeMath for uint256;

    StrikeToken public token = new StrikeToken();

    // start and end times
    uint256 public startTimestamp = 1575158400;
    uint256 public endTimestamp = 1577750400;
    uint256 etherToWei = 10**18;

    // address where funds are collected and tokens distributed
    address public hardwareWallet = 0xDe3A91E42E9F6955ce1a9eDb23Be4aBf8d2eb08B;
    address public restrictedWallet = 0xDe3A91E42E9F6955ce1a9eDb23Be4aBf8d2eb08B;
    address public additionalTokensFromCommonPoolWallet = 0xDe3A91E42E9F6955ce1a9eDb23Be4aBf8d2eb08B;

    mapping (address => uint256) public deposits;
    uint256 public numberOfPurchasers;

    // Percentage bonus tokens given in Token Sale, on a daily basis
    uint256[] public bonus = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    uint256 public rate = 4800; // 4800 DST is one Ether

    // amount of raised money in wei
    uint256 public weiRaised = 0;
    uint256 public tokensSold = 0;
    uint256 public advisorTokensGranted = 0;
    uint256 public commonPoolTokensGranted = 0;

    uint256 public minContribution = 100 * 1 finney;
    uint256 public hardCapEther = 30000;
    uint256 hardcap = hardCapEther * etherToWei;

    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
    event MainSaleClosed();

    uint256 public weiRaisedInPresale  = 0 ether;

    bool private frozen = false;

    function freeze() public onlyOwner{
      frozen = true;
    }
    function unfreeze() public onlyOwner{
      frozen = false;
    }

    modifier whenNotFrozen() {
        require(!frozen);
        _;
    }
    modifier whenFrozen() {
        require(frozen);
        _;
    }

    function setHardwareWallet(address _wallet) public onlyOwner {
        require(_wallet != 0x0);
        hardwareWallet = _wallet;
    }

    function setRestrictedWallet(address _restrictedWallet) public onlyOwner {
        require(_restrictedWallet != 0x0);
        restrictedWallet = _restrictedWallet;
    }

    function setAdditionalTokensFromCommonPoolWallet(address _wallet) public onlyOwner {
        require(_wallet != 0x0);
        additionalTokensFromCommonPoolWallet = _wallet;
    }

    function setHardCapEther(uint256 newEtherAmt) public onlyOwner{
        require(newEtherAmt > 0);
        hardCapEther = newEtherAmt;
        hardcap = hardCapEther * etherToWei;
    }

    constructor() public  {
        require(startTimestamp >= now);
        require(endTimestamp >= startTimestamp);
    }

    // check if valid purchase
    modifier validPurchase {
        require(now >= startTimestamp);
        require(now < endTimestamp);
        require(msg.value >= minContribution);
        require(frozen == false);
        _;
    }

    // @return true if crowdsale event has ended
    function hasEnded() public constant returns (bool) {
        if (now > endTimestamp)
            return true;
        return false;
    }

    // low level token purchase function
    function buyTokens(address beneficiary) public payable validPurchase {
        require(beneficiary != 0x0);

        uint256 weiAmount = msg.value;

        // Check if the hardcap has been exceeded
        uint256 weiRaisedSoFar = weiRaised.add(weiAmount);
        require(weiRaisedSoFar + weiRaisedInPresale <= hardcap);

        if (deposits[msg.sender] == 0) {
            numberOfPurchasers++;
        }
        deposits[msg.sender] = weiAmount.add(deposits[msg.sender]);

        uint256 daysInSale = (now - startTimestamp) / (1 days);
        uint256 thisBonus = 0;
        if(daysInSale < 29 ){
            thisBonus = bonus[daysInSale];
        }

        // Calculate token amount to be created
        uint256 tokens = weiAmount.mul(rate);
        uint256 extraBonus = tokens.mul(thisBonus);
        extraBonus = extraBonus.div(100);
        tokens = tokens.add(extraBonus);

        // Update the global token sale variables
        uint256 finalTokenCount;
        finalTokenCount = tokens.add(tokensSold);
        weiRaised = weiRaisedSoFar;
        tokensSold = finalTokenCount;

        token.mint(beneficiary, tokens);
        hardwareWallet.transfer(msg.value);
        emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
    }

    function grantTokensAdvisors(address beneficiary,uint256 dstTokenCount) public onlyOwner{
        dstTokenCount = dstTokenCount * etherToWei;
        advisorTokensGranted = advisorTokensGranted.add(dstTokenCount);
        token.mint(beneficiary,dstTokenCount);
    }

    function grantTokensCommonPool(address beneficiary,uint256 dstTokenCount) public onlyOwner{
        dstTokenCount = dstTokenCount * etherToWei;
        commonPoolTokensGranted = commonPoolTokensGranted.add(dstTokenCount);
        token.mint(beneficiary,dstTokenCount);
    }

    // finish mining coins and transfer ownership of Change coin to owner
    function finishMinting() public onlyOwner returns(bool){
        require(hasEnded());

        uint issuedTokenSupply = token.totalSupply();
        uint publicTokens = issuedTokenSupply-advisorTokensGranted;
        if(publicTokens>60*advisorTokensGranted/40 ){
          uint restrictedTokens=(publicTokens)*40/60-advisorTokensGranted;
          token.mint(restrictedWallet, restrictedTokens);
          advisorTokensGranted=advisorTokensGranted+restrictedTokens;
        }
        else if(publicTokens<60*advisorTokensGranted/40){
          uint256 deltaCommonPool=advisorTokensGranted*60/40-publicTokens;
          token.mint(additionalTokensFromCommonPoolWallet,deltaCommonPool);
        }

        token.finishMinting();
        token.transferOwnership(owner);
        emit MainSaleClosed();
        return true;
    }

    // fallback function can be used to buy tokens
    function () payable public {
        buyTokens(msg.sender);
    }
    function setRate(uint256 amount) onlyOwner public {
        require(amount>=0);
        rate = amount;
    }
    function setBonus(uint256 [] amounts) onlyOwner public {
      require( amounts.length > 30 );
        bonus = amounts;
    }
    function setWeiRaisedInPresale(uint256 amount) onlyOwner public {
        require(amount>=0);
        weiRaisedInPresale = amount;
    }
    function setEndTimeStamp(uint256 end) onlyOwner public {
        require(end>now);
        endTimestamp = end;
    }
    function setStartTimeStamp(uint256 start) onlyOwner public {
        startTimestamp = start;
    }
    function pauseTrading() onlyOwner public{
        token.pause();
    }
    function startTrading() onlyOwner public{
        token.unpause();
    }
    function smartBlockAddress(address toBeBlocked) onlyOwner public{
        token.blockAddress(toBeBlocked);
    }
    function smartUnBlockAddress(address toBeUnblocked) onlyOwner public{
        token.unBlockAddress(toBeUnblocked);
    }
    function changeTokenOwner(address newOwner) public onlyOwner {
        require(hasEnded());
        token.changeOwner(newOwner);
    }
    function bulkGrantTokenAdvisors(address [] beneficiaries,uint256 [] granttokencounts) public onlyOwner{
      require( beneficiaries.length == granttokencounts.length);
      for (uint256 i=0; i<beneficiaries.length; i++) {
        grantTokensAdvisors(beneficiaries[i],granttokencounts[i]);
      }
    }
    function bulkGrantTokenCommonPool(address [] beneficiaries,uint256 [] granttokencounts) public onlyOwner{
      require( beneficiaries.length == granttokencounts.length);
      for (uint256 i=0; i<beneficiaries.length; i++) {
        grantTokensCommonPool(beneficiaries[i],granttokencounts[i]);
      }
    }

}
function finishMinting() public onlyOwner returns(bool){
        require(hasEnded());

        uint issuedTokenSupply = token.totalSupply();
        uint publicTokens = issuedTokenSupply-advisorTokensGranted;
        if(publicTokens>60*advisorTokensGranted/40 ){
          uint restrictedTokens=(publicTokens)*40/60-advisorTokensGranted;
          token.mint(restrictedWallet, restrictedTokens);
          advisorTokensGranted=advisorTokensGranted+restrictedTokens;
        }
        else if(publicTokens<60*advisorTokensGranted/40){
          uint256 deltaCommonPool=advisorTokensGranted*60/40-publicTokens;
          token.mint(additionalTokensFromCommonPoolWallet,deltaCommonPool);
        }

        token.finishMinting();
        token.transferOwnership(owner);
        emit MainSaleClosed();
        return true;
    }
// finish mining coins and transfer ownership of Change coin to owner