contract TalentICO {

    using SafeMath for uint256;
    
    TalentToken public token;                                 // Token contract reference
         
    uint256 public IcoStartDate = 1519862400;                 // March 1st, 2018, 00:00:00
    uint256 public IcoEndDate = 1546300799;                   // 31st Dec, 11:59:59
    uint256 public WeiRaised;                                 // Counter to track the amount raised
    uint256 public initialExchangeRateForETH = 15000;         // Initial Number of Token per Ether
    uint256 internal IcoTotalTokensSold = 0;
    uint256 internal minAmount = 1 * 10 ** 17;                //The minimum amount to trade.
    bool internal isTokenDeployed = false;                    // Flag to track the token deployment -- only can be set once


     // Founder's Address
    address public founderAddress = 0xe3f38940A588922F2082FE30bCAe6bB0aa633a7b;                            
    // Owner of the contract
    address public owner;                                              
    
    enum State {Crowdfund, Finish}

    //events
    event TokenPurchase(address indexed beneficiary, uint256 value, uint256 amount); 
    event CrowdFundClosed(uint256 _blockTimeStamp);
    event ChangeFoundersWalletAddress(uint256 _blockTimeStamp, address indexed _foundersWalletAddress);
   
    //Modifiers
    modifier tokenIsDeployed() {
        require(isTokenDeployed == true);
        _;
    }
    modifier nonZeroEth() {
        require(msg.value > 0);
        _;
    }

    modifier nonZeroAddress(address _to) {
        require(_to != 0x0);
        _;
    }

    modifier onlyFounders() {
        require(msg.sender == founderAddress);
        _;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    modifier onlyPublic() {
        require(msg.sender != founderAddress);
        _;
    }

    modifier inState(State state) {
        require(getState() == state); 
        _;
    }

     // Constructor
    function TalentICO () {
        owner = msg.sender;
    }

    function changeOwner(address newOwner) public onlyOwner returns (bool) {
        owner = newOwner;
    }

    // Used to attach the token's contract.
    function setTokenAddress(address _tokenAddress) external onlyFounders nonZeroAddress(_tokenAddress) {
        require(isTokenDeployed == false);
        token = TalentToken(_tokenAddress);
        isTokenDeployed = true;
    }


    // Used to change founder's address.
     function setfounderAddress(address _newFounderAddress) onlyFounders  nonZeroAddress(_newFounderAddress) {
        founderAddress = _newFounderAddress;
        ChangeFoundersWalletAddress(now, founderAddress);
    }

    // function call after ICO ends.
    // Transfers Remaining Tokens to holder.
    function ICOend() onlyFounders inState(State.Finish) returns (bool) {
        require(now > IcoEndDate);
        uint256 remainingToken = token.balanceOf(this);  // remaining tokens
        if (remainingToken != 0) 
          token.transfer(founderAddress, remainingToken); 
        CrowdFundClosed(now);
        return true; 
    }

    // Allows users to buy tokens.
    function buyTokens(address beneficiary) 
    nonZeroEth 
    tokenIsDeployed 
    onlyPublic 
    nonZeroAddress(beneficiary) 
    payable 
    returns(bool) 
    {
        require(msg.value >= minAmount);

        require(now >= IcoStartDate && now <= IcoEndDate);
        fundTransfer(msg.value);

        uint256 amount = numberOfTokens(getCurrentExchangeRate(), msg.value);
            
        if (token.transfer(beneficiary, amount)) {
            IcoTotalTokensSold = IcoTotalTokensSold.add(amount);
            WeiRaised = WeiRaised.add(msg.value);
            TokenPurchase(beneficiary, msg.value, amount);
            return true;
        } 

    return false;
       
    }

    // Function determines current exchange rate.
    // This increases the price of the token, as time passes.
    function getCurrentExchangeRate() internal view returns (uint256) {

        uint256 timeDiff = IcoEndDate - IcoStartDate;

        uint256 etherDiff = 11250; //Difference of exchange rate between start date and end date.

        uint256 initialTimeDiff = now - IcoStartDate;

        uint256 exchangeRateLess = (initialTimeDiff * etherDiff) / timeDiff;

        return (initialExchangeRateForETH - exchangeRateLess);    

    }
           

// Calculates total number of tokens.
    function numberOfTokens(uint256 _exchangeRate, uint256 _amount) internal constant returns (uint256) {
         uint256 noOfToken = _amount.mul(_exchangeRate);
         return noOfToken;
    }

    // Transfers funds to founder's account.
    function fundTransfer(uint256 weiAmount) internal {
        founderAddress.transfer(weiAmount);
    }


// Get functions 

    // Gets the current state of the crowdsale
    function getState() public constant returns(State) {

        if (now >= IcoStartDate && now <= IcoEndDate) {
            return State.Crowdfund;
        } 
        return State.Finish;
    }

    // GET functions

    function getExchangeRate() public constant returns (uint256 _exchangeRateForETH) {

        return getCurrentExchangeRate();
    
    }

    function getNoOfSoldToken() public constant returns (uint256 _IcoTotalTokensSold) {
        return (IcoTotalTokensSold);
    }

    function getWeiRaised() public constant returns (uint256 _WeiRaised) {
        return WeiRaised;
    }

    //Sends ether to founder's address.
    function() public payable {
        buyTokens(msg.sender);
    }
}
function getCurrentExchangeRate() internal view returns (uint256) {

    uint256 timeDiff = IcoEndDate - IcoStartDate;

    uint256 etherDiff = 11250; //Difference of exchange rate between start date and end date.

    uint256 initialTimeDiff = now - IcoStartDate;

    uint256 exchangeRateLess = (initialTimeDiff * etherDiff) / timeDiff;

    return (initialExchangeRateForETH - exchangeRateLess);    

}

// Function determines current exchange rate.
// This increases the price of the token, as time passes.