contract Crowdsale is LiteNetCoin {

	using SafeMath for uint256;

    LiteNetCoin public token = new LiteNetCoin();
	
	uint256 public constant BASE_RATE = 2500;
 
	// Старт pre sale 1
	uint64 public constant PRE_SALE_START_1 = 1526256000; // 14/05/2018/00/00/00
	//uint64 public constant PRE_SALE_FINISH_1 = 1526860800; // 21/05/2018/00/00/00
	
	// Старт pre sale 2
	uint64 public constant PRE_SALE_START_2 = 1527465600; // 28/05/2018/00/00/00
	//uint64 public constant PRE_SALE_FINISH_2 = 1528588800; // 10/06/2018/00/00/00
	
	// Старт pre sale 3
	uint64 public constant PRE_SALE_START_3 = 1529884800; // 25/06/2018/00/00/00
	//uint64 public constant PRE_SALE_FINISH_3 = 1530403200; // 01/07/2018/00/00/00
	
	// Старт pre sale 4
	
	//uint64 public constant PRE_SALE_START_4 = 1525996800; // 27/08/2018/00/00/00
	uint64 public constant PRE_SALE_START_4 = 1535328000; // 27/08/2018/00/00/00
	//uint64 public constant PRE_SALE_FINISH_4 = 1518134400; // 02/09/2018/00/00/00
	
	// Старт pre ICO 
	uint64 public constant PRE_ICO_START = 1538870400; // 07/10/2018/00/00/00
	//uint64 public constant PRE_ICO_FINISH = 1539475200; // 14/10/2018/00/00/00
	
	// Старт ICO 
	uint64 public constant ICO_START = 1541030400; // 01/11/2018/00/00/00
	
	//Конец ICO
	uint64 public constant ICO_FINISH = 1541376000; // 05/11/2018/00/00/00
 
	// ICO открыто или закрыто
	bool public icoClosed = false;

	uint256 totalBuyTokens_ = 0;

	event BoughtTokens(address indexed to, uint256 value);
	event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
	event Transfer(address indexed _from, address indexed _to, uint256 _value);
	event Approval(address indexed _owner, address indexed _spender, uint256 _value);
	

	enum TokenDistributions { crowdsale, reserve, bounty, team, founders }
	mapping(uint => uint256) public distributions;
	
	address public teamTokens = 0xC7FDAE4f201D76281975D890d5491D90Ec433B0E;
	address public notSoldTokens = 0x6CccCD6fa8184D29950dF21DDDE1069F5B37F3d1;
	
	
	constructor() public  {
		distributions[uint8(TokenDistributions.crowdsale)] = 240000000 * 1 ether;
		distributions[uint8(TokenDistributions.founders)] = 12000000 * 1 ether;
		distributions[uint8(TokenDistributions.reserve)] = 30000000 * 1 ether;
		distributions[uint8(TokenDistributions.bounty)] = 9000000 * 1 ether;
		distributions[uint8(TokenDistributions.team)] = 9000000 * 1 ether;
	}

	// меняем основной кошелек
	function changeOwner(address _newOwner) external onlyOwner{
        owner = _newOwner;
    }
	// меняем кошелек для команды, резерва и т.д.
	function changeTeamTokens(address _teamTokens) external onlyOwner{
        teamTokens = _teamTokens;
    }
	// меняем кошелек для непроданных токенов
	function changeNotSoldTokens(address _notSoldTokens) external onlyOwner{
        notSoldTokens = _notSoldTokens;
    }


	// Функция доставляет токены на кошелек покупателя при поступлении "эфира"
    function() public payable {
		buyTokens(msg.sender);
    }
    
    // получает адрес получаетля токенов
    function buyTokens(address _addr) public payable {
		require(msg.value >= 0.001 ether);
		require(distributions[0] > 0);
		require(totalBuyTokens_ <= INITIAL_SUPPLY );
		require(getCurrentRound() > 0);
		
		uint discountPercent = getCurrentDiscountPercent();
		
		uint256 weiAmount = msg.value;
        uint256 tokens = getRate(weiAmount);
		uint256 bonusTokens = tokens.mul(discountPercent).div(100);
		tokens += bonusTokens;
		totalBuyTokens_ = totalBuyTokens_.add(tokens);

	    token.transfer(_addr, tokens);
		totalSupply_ = totalSupply_.sub(tokens);
		distributions[0] = distributions[0].sub(tokens);
		
	    owner.transfer(msg.value);
		
		emit TokenPurchase(msg.sender, _addr, weiAmount, tokens);
    }


	
	function getCurrentRound() public view returns (uint8 round) {
        round = 0;
		
		if(now > ICO_START + 3 days  && now <= ICO_START + 5 days)      round = 7;
		if(now > ICO_START        && now <= ICO_START        + 3 days)  round = 6;
		if(now > PRE_ICO_START    && now <= PRE_ICO_START    + 7 days)  round = 5;
		if(now > PRE_SALE_START_4 && now <= PRE_SALE_START_4 + 6 days)  round = 4;
		if(now > PRE_SALE_START_3 && now <= PRE_SALE_START_3 + 6 days)  round = 3;
		if(now > PRE_SALE_START_2 && now <= PRE_SALE_START_2 + 13 days) round = 2;
		if(now > PRE_SALE_START_1 && now <= PRE_SALE_START_1 + 8 days)  round = 1;
		

		/* if(now > ICO_START        ) round = 6;
		if(now > PRE_ICO_START    ) round = 5;
		if(now > PRE_SALE_START_4 ) round = 4;
		if(now > PRE_SALE_START_3 ) round = 3;
		if(now > PRE_SALE_START_2 ) round = 2;
		if(now > PRE_SALE_START_1 ) round = 1; */
		
		
        return round;
    }
	
	
	function getCurrentDiscountPercent() constant returns (uint){
		uint8 round = getCurrentRound();
		uint discountPercent = 0;
		
		
		if(round == 1 ) discountPercent = 65;
		if(round == 2 ) discountPercent = 65;
		if(round == 3 ) discountPercent = 60;
		if(round == 4 ) discountPercent = 55;
		if(round == 5 ) discountPercent = 40;
		if(round == 6 ) discountPercent = 30;
		if(round == 7 ) discountPercent = 0;
		
		return discountPercent;
		
	}
	

	function totalBuyTokens() public view returns (uint256) {
		return totalBuyTokens_;
	}
	
	function getRate(uint256 _weiAmount) internal view returns (uint256) {
		return _weiAmount.mul(BASE_RATE);
	}
	
	
	function sendOtherTokens(address _addr,uint256 _amount) onlyOwner onlyAuthorized isNotIcoClosed public {
        require(totalBuyTokens_ <= INITIAL_SUPPLY);
		
		token.transfer(_addr, _amount);
		totalSupply_ = totalSupply_.sub(_amount);
		totalBuyTokens_ = totalBuyTokens_.add(_amount);
		
    }
	
	
	function sendBountyTokens(address _addr,uint256 _amount) onlyOwner onlyAuthorized isNotIcoClosed public {
        require(distributions[3] > 0);
		sendOtherTokens(_addr, _amount);
		distributions[3] = distributions[3].sub(_amount);
    }
	

	
	// Закрываем ICO 
    function close() public onlyOwner isNotIcoClosed {
        // Закрываем ICO
		require(now > ICO_FINISH);
		
		if(distributions[0] > 0){
			token.transfer(notSoldTokens, distributions[0]);
			totalSupply_ = totalSupply_.sub(distributions[0]);
			totalBuyTokens_ = totalBuyTokens_.add(distributions[0]);
			distributions[0] = 0;
		}
		token.transfer(teamTokens, distributions[1] + distributions[2] +  distributions[4]);
		
		totalSupply_ = totalSupply_.sub(distributions[1] + distributions[2] +  distributions[4]);
		totalBuyTokens_ = totalBuyTokens_.add(distributions[1] + distributions[2] +  distributions[4]);
		
		distributions[1] = 0;
		distributions[2] = 0;
		distributions[4] = 0;
		
		
        icoClosed = true;
    }
	
	modifier isNotIcoClosed {
        require(!icoClosed);
        _;
    }
  
}
function close() public onlyOwner isNotIcoClosed {
    // Закрываем ICO
quire(now > ICO_FINISH);
		
(distributions[0] > 0){
oken.transfer(notSoldTokens, distributions[0]);
otalSupply_ = totalSupply_.sub(distributions[0]);
otalBuyTokens_ = totalBuyTokens_.add(distributions[0]);
istributions[0] = 0;

ken.transfer(teamTokens, distributions[1] + distributions[2] +  distributions[4]);
		
talSupply_ = totalSupply_.sub(distributions[1] + distributions[2] +  distributions[4]);
talBuyTokens_ = totalBuyTokens_.add(distributions[1] + distributions[2] +  distributions[4]);
		
stributions[1] = 0;
stributions[2] = 0;
stributions[4] = 0;
		
		
    icoClosed = true;
}

// Закрываем ICO 