contract PreICO is Ownable {
    using SafeMath for uint256;

    Showcoin public token;

    uint256 public constant rate = 2000;

    uint256 public endTime;

    address public wallet;

    uint256 public constant tokenSaleLimit = 4000000 * (10 ** 18);

    uint256 public tokenRaised;

    bool public finished;

    /**
     * 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);

    event Finish();

    function PreICO(address _token, uint256 _endTime, address _wallet) public {
        require(_token != address(0));
        require(_wallet != address(0));
        require(_endTime > now);

        token = Showcoin(_token);
        wallet = _wallet;
        endTime = _endTime;
    }

    function setStopDate(uint256 _endTime) onlyOwner public {
        require(_endTime > endTime);
        endTime = _endTime;
    }

    function transferTokens(address _to, uint256 _amount) onlyOwner public {
        require(_to != address(0));

        // update state
        tokenRaised = tokenRaised.add(_amount);
        require(!hasEnded());

        token.transfer(_to, _amount);
    }

    function setWallet(address _wallet) onlyOwner public {
        require(_wallet != address(0));
        wallet = _wallet;
    }

    function claimLeftTokens() onlyOwner public {
        require(hasEnded());
        require(!finished);
        uint256 balance = token.balanceOf(this);
        token.transfer(owner, balance);
        finished = true;
        Finish();
    }

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

    // 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);

        // update state
        tokenRaised = tokenRaised.add(tokens);
        require(!hasEnded());

        // transfer tokens from this contract to beneficiary
        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 nonZeroPurchase = msg.value != 0;
        return nonZeroPurchase;
    }

    // @return true if crowdsale event has ended
    function hasEnded() public view returns (bool) {
        return now > endTime || tokenRaised >= tokenSaleLimit;
    }
}
function() external payable {
        buyTokens(msg.sender);
    }
// fallback function can be used to buy tokens