contract Potentium is StandardToken, Mortal {
    string public constant name = "POTENTIAM";
    uint public constant decimals = 18;
    string public constant symbol = "PTM";
    address companyReserve;
    uint saleEndDate;
    uint public amountRaisedInWei;
    uint public priceOfToken=1041600000000000;//0.0010416 ETH
    address[] allParticipants;
    uint tokenSales=0;
     mapping(address => uint256)public  balancesHold;
    event TokenHold( address indexed to, uint256 value);
    mapping (address => bool) isParticipated;
    uint public icoStartDate;
    uint public icoWeek1Bonus = 10;
    uint public icoWeek2Bonus = 7;
    uint public icoWeek3Bonus = 5;
    uint public icoWeek4Bonus = 3;
    function Potentium()  public {
      totalSupply=100000000 *(10**decimals);  // 
       owner = msg.sender;
       companyReserve=0x3b06AC092339D382050C892aD035b5F140B7C628;
       balances[msg.sender] = 75000000 * (10**decimals);
       balances[companyReserve] = 25000000 * (10**decimals); //given by potentieum
      saleEndDate =  1520554400000;  //8 March 2018
    }

    
    function() payable whenNotPaused public {
        require(msg.sender !=0x0);
        require(now<=saleEndDate);
        require(msg.value >=40000000000000000); //minimum 0.04 eth
        require(tokenSales<=(60000000 * (10 ** decimals)));
        uint256 tokens = (msg.value * (10 ** decimals)) / priceOfToken;
        uint256 bonusTokens = 0;
        if(now <1513555100000){
            bonusTokens = (tokens * 40) /100; //17 dec 2017 % bonus presale
        }else if(now <1514760800000) {
            bonusTokens = (tokens * 35) /100; //31 dec 2017 % bonus
        }else if(now <1515369600000){
            bonusTokens = (tokens * 30) /100; //jan 7 2018 bonus
        }else if(now <1515974400000){
            bonusTokens = (tokens * 25) /100; //jan 14 2018 bonus
        }
        else if(now <1516578400000){
            bonusTokens = (tokens * 20) /100; //jan 21 2018 bonus
        }else if(now <1517011400000){
              bonusTokens = (tokens * 15) /100; //jan 26 2018 bonus
        }
        else if(now>=icoStartDate){
            if(now <= (icoStartDate + 1 * 7 days) ){
                bonusTokens = (tokens * icoWeek1Bonus) /100; 
            }
            else if(now <= (icoStartDate + 2 * 7 days) ){
                bonusTokens = (tokens * icoWeek2Bonus) /100; 
            }
           else if(now <= (icoStartDate + 3 * 7 days) ){
                bonusTokens = (tokens * icoWeek3Bonus) /100; 
            }
           else if(now <= (icoStartDate + 4 * 7 days) ){
                bonusTokens = (tokens * icoWeek4Bonus) /100; 
            }
            
        }
        tokens +=bonusTokens;
        tokenSales+=tokens;
        balancesHold[msg.sender]+=tokens;
        amountRaisedInWei = amountRaisedInWei + msg.value;
        if(!isParticipated[msg.sender]){
            allParticipants.push(msg.sender);
        }
        TokenHold(msg.sender,tokens);//event to dispactc as token hold successfully
    }
    function distributeTokensAfterIcoByOwner()public onlyOwner{
        for (uint i = 0; i < allParticipants.length; i++) {
                    address userAdder=allParticipants[i];
                    var tokens = balancesHold[userAdder];
                    if(tokens>0){
                    allowed[owner][userAdder] += tokens;
                    transferFrom(owner, userAdder, tokens);
                    balancesHold[userAdder] = 0;
                     }
                 }
    }
    /**
   * @dev called by the owner to extend deadline relative to last deadLine Time,
   * to accept ether and transfer tokens
   */
   function extendSaleEndDate(uint saleEndTimeInMIllis)public onlyOwner{
       saleEndDate = saleEndTimeInMIllis;
   }
   function setIcoStartDate(uint icoStartDateInMilli)public onlyOwner{
       icoStartDate = icoStartDateInMilli;
   }
    function setICOWeek1Bonus(uint bonus)public onlyOwner{
       icoWeek1Bonus= bonus;
   }
     function setICOWeek2Bonus(uint bonus)public onlyOwner{
       icoWeek2Bonus= bonus;
   }
     function setICOWeek3Bonus(uint bonus)public onlyOwner{
       icoWeek3Bonus= bonus;
   }
     function setICOWeek4Bonus(uint bonus)public onlyOwner{
       icoWeek4Bonus= bonus;
   }
   function rateForOnePTM(uint rateInWei) public onlyOwner{
       priceOfToken = rateInWei;
   }

   //function ext
   /**
     * to get total particpants count
     */
    function getCountPartipants() public constant returns (uint count){
       return allParticipants.length;
    }
    function getParticipantIndexAddress(uint index)public constant returns (address){
        return allParticipants[index];
    }
    /**
    * Transfer entire balance to any account (by owner and admin only)
    **/
    function transferFundToAccount(address _accountByOwner) public onlyOwner {
        require(amountRaisedInWei > 0);
        _accountByOwner.transfer(amountRaisedInWei);
        amountRaisedInWei = 0;
    }

    function resetTokenOfAddress(address _userAdd)public onlyOwner {
      uint256 userBal=  balances[_userAdd] ;
      balances[_userAdd] = 0;
      balances[owner] +=userBal;
    }
    /**
    * Transfer part of balance to any account (by owner and admin only)
    **/
    function transferLimitedFundToAccount(address _accountByOwner, uint256 balanceToTransfer) public onlyOwner   {
        require(amountRaisedInWei > balanceToTransfer);
        _accountByOwner.transfer(balanceToTransfer);
        amountRaisedInWei -= balanceToTransfer;
    }
}
function transferFundToAccount(address _accountByOwner) public onlyOwner {
    require(amountRaisedInWei > 0);
    _accountByOwner.transfer(amountRaisedInWei);
    amountRaisedInWei = 0;
}

/**
 * Transfer entire balance to any account (by owner and admin only)
 **/