contract lockContract is owned{
    using SafeMath for uint256;
    
    /*
     * deposit vars
    */
    struct Items {
        address tokenAddress;
        uint256 tokenAmount;
        uint256 unlockTime;
        bool withdrawn;
    }
    
    uint256 public depositId;
    uint256[] public allDepositIds;
    mapping (uint256 => Items) public lockedToken;
    
    event LogWithdrawal(address SentToAddress, uint256 AmountTransferred);
    
    /**
     * Constrctor function
    */
    function lockContract() public {

    }
    
    /**
     *lock tokens
    */
    function lockTokens(address _tokenAddress, uint256 _amount, uint256 _unlockTime) onlyOwner public returns (uint256 _id) {
        require(_amount > 0);
        require(_unlockTime < 10000000000);
        require(Token(_tokenAddress).transferFrom(msg.sender, this, _amount));
        
        _id = ++depositId;
        lockedToken[_id].tokenAddress = _tokenAddress;
        lockedToken[_id].tokenAmount = _amount;
        lockedToken[_id].unlockTime = _unlockTime;
        lockedToken[_id].withdrawn = false;
        
        allDepositIds.push(_id);
    }
    
    /**
     *withdraw tokens
    */
    function withdrawTokens(uint256 _id) public onlyOwner {
        require(block.timestamp >= lockedToken[_id].unlockTime);
        require(!lockedToken[_id].withdrawn);
        require(Token(lockedToken[_id].tokenAddress).transfer(msg.sender, lockedToken[_id].tokenAmount));
        
        lockedToken[_id].withdrawn = true;
        
        LogWithdrawal(msg.sender, lockedToken[_id].tokenAmount);
    }

     /*get total token balance in contract*/
    function getTotalTokenBalance(address _tokenAddress) view public returns (uint256)
    {
       return Token(_tokenAddress).balanceOf(this);
    }
    
    /*get allDepositIds*/
    function getAllDepositIds() view public returns (uint256[])
    {
        return allDepositIds;
    }
    
    /*get getDepositDetails*/
    function getDepositDetails(uint256 _id) view public returns (address tokenAddress, uint256 tokenAmount, uint256 unlockTime, bool withdrawn)
    {
        return(lockedToken[_id].tokenAddress,lockedToken[_id].tokenAmount,
        lockedToken[_id].unlockTime,lockedToken[_id].withdrawn);
    }
    
    /*get number of active deposits*/
    function numOfActiveDeposits() public view returns (uint256) {
        uint256 staked = 0;
        for (uint i = 0; i < allDepositIds.length; i++) {
            if (!lockedToken[allDepositIds[i]].withdrawn) {
                staked++;
            }
        }
        return staked;
    }
    
    /*get getWithdrawableDeposits*/
    function getWithdrawableDeposits() view public returns (uint256[])
    {
        uint256[] memory deposits = new uint256[](numOfActiveDeposits());
        uint256 tempIdx = 0;
        for(uint256 i = 0; i < allDepositIds.length; i++) {
            if(!lockedToken[allDepositIds[i]].withdrawn) {
                deposits[tempIdx] =  allDepositIds[i];
                tempIdx ++;
            }
        }
        return deposits;
    }
    
}
function getTotalTokenBalance(address _tokenAddress) view public returns (uint256)
{
   return Token(_tokenAddress).balanceOf(this);
}

/*get total token balance in contract*/