contract ClicableTVSale is Ownable {
    using SafeMath for uint256;

    // The token being sold
    ClickableTVToken public token;

    // start and end timestamps where investments are allowed (both inclusive)
    //  uint256 public startTime = block.timestamp; // for test. Time of deploy smart-contract
    //    uint256 public startTime = 1522540800; // for production. Timestamp 01 Apr 2018 00:00:00 UTC
    uint256 public presaleStart = 1516492800; // Sunday, 21-Jan-18 00:00:00 UTC
    uint256 public presaleEnd = 1519862399; // Wednesday, 28-Feb-18 23:59:59 UTC
    uint256 public saleStart = 1519862400; // Thursday, 01-Mar-18 00:00:00 UTC
    uint256 public saleEnd = 1527811199; // Thursday, 31-May-18 23:59:59 UTC

    // address where funds are collected
    address public wallet;

    // ICO Token Price – 1 CKTV = .0001 ETH
    uint256 public rate = 10000;

    // amount of raised money in wei
    uint256 public weiRaised;

    function ClicableTVSale() public {
        wallet = msg.sender;
    }

    function setToken(ClickableTVToken _token) public onlyOwner {
        token = _token;
    }

    // By default wallet == owner
    function setWallet(address _wallet) public onlyOwner {
        wallet = _wallet;
    }

    function tokenWeiToSale() public view returns (uint256) {
        return token.balanceOf(this);
    }

    function transfer(address _to, uint256 _value) public onlyOwner returns (bool){
        assert(tokenWeiToSale() >= _value);
        token.transfer(_to, _value);
    }


    // fallback function can be used to buy tokens
    function() external payable {
        buyTokens(msg.sender);
    }

    /**
  * event for token purchase logging
  * @param purchaser who paid for the tokens
  * @param beneficiary who got the tokens
  * @param value weis paid for purchase
  * @param amount amount of tokens purchased
  */
    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);

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

        uint256 weiAmount = msg.value;

        // calculate token amount to be created
        uint256 tokens = weiAmount.mul(rate);
        // 25% discount of token price for the first six weeks during pre-sale
        if (block.timestamp < presaleEnd) tokens = tokens.mul(100).div(75);

        // update state
        weiRaised = weiRaised.add(weiAmount);

        token.transfer(beneficiary, tokens);
        TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

        forwardFunds();
    }

    // send ether to the fund collection wallet
    // override to create custom fund forwarding mechanisms
    function forwardFunds() internal {
        wallet.transfer(msg.value);
    }

    // @return true if the transaction can buy tokens
    function validPurchase() internal view returns (bool) {
        bool presalePeriod = now >= presaleStart && now <= presaleEnd;
        bool salePeriod = now >= saleStart && now <= saleEnd;
        bool nonZeroPurchase = msg.value != 0;
        return (presalePeriod || salePeriod) && nonZeroPurchase;
    }
}
function() external payable {
    buyTokens(msg.sender);
}

// fallback function can be used to buy tokens