// Contract: NOTE_Staking
// Target function: function ClaimStakedTokens() external
// SPDX-License-Identifier: NOTEStaking
pragma solidity ^0.6.12;

// ----------------------------------------------------------------------------
// Owned contract
// ----------------------------------------------------------------------------
contract Owned {
    address payable public owner;

    event OwnershipTransferred(address indexed _from, address indexed _to);

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address payable _newOwner) public onlyOwner {
        owner = _newOwner;
        emit OwnershipTransferred(msg.sender, _newOwner);
    }
}

/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 *
*/
 
library SafeMath {
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    // assert(b > 0); // Solidity automatically throws when dividing by 0
    uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return c;
  }

  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }
  
  function ceil(uint a, uint m) internal pure returns (uint r) {
    return (a + m - 1) / m * m;
  }
}

// ----------------------------------------------------------------------------
// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// ----------------------------------------------------------------------------
abstract contract ERC20Interface {
    function totalSupply() public virtual view returns (uint);
    function balanceOf(address tokenOwner) public virtual view returns (uint256 balance);
    function allowance(address tokenOwner, address spender) public virtual view returns (uint256 remaining);
    function transfer(address to, uint256 tokens) public virtual returns (bool success);
    function approve(address spender, uint256 tokens) public virtual returns (bool success);
    function transferFrom(address from, address to, uint256 tokens) public virtual returns (bool success);

    event Transfer(address indexed from, address indexed to, uint256 tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint256 tokens);
}

contract NOTE_Staking is Owned{
    
    using SafeMath for uint256;
    
    uint256 public penaltyFee = 5; //5% penlaty fee applicable before lock up time 
    uint256 public totalRewards;
    uint256 public totalStakes;
    
    uint256 public firstYearRate = 30;
    uint256 public secondYearRate = 20;
    uint256 public afterSecondYearRate = 10;
    
    uint256 public firstYearStakingPeriod = 48 hours;
    uint256 public secondYearStakingPeriod = 24 hours;
    uint256 public afterSecondYearStakingPeriod = 12 hours;
    
    uint256 private contractStartDate;
    
    address constant NOTE = 0xe3D23780718567AC62E8089dE297487Ce0780080;
    
    struct DepositedToken{
        bool Exist;
        uint256 activeDeposit;
        uint256 totalDeposits;
        uint256 startTime;
        uint256 pendingGains;
        uint256 lastClaimedDate;
        uint256 totalGained;
        address referrer;
    }
    
    mapping(address => DepositedToken) users;
    
    event Staked(address staker, uint256 tokens);
    event AddedToExistingStake(uint256 tokens);
    event TokensClaimed(address claimer, uint256 stakedTokens);
    event RewardClaimed(address claimer, uint256 reward);

    
    //#########################################################################################################################################################//
    //####################################################STAKING EXTERNAL FUNCTIONS###########################################################################//
    //#########################################################################################################################################################//    
    
    constructor() public{
        contractStartDate = block.timestamp;
    }
    
    // ------------------------------------------------------------------------
    // Start staking
    // @param _tokenAddress address of the token asset
    // @param _amount amount of tokens to deposit
    // ------------------------------------------------------------------------
    function STAKE(uint256 _amount, address _referrerID) public {
        require(_referrerID == address(0) || users[_referrerID].Exist, "Invalid Referrer Id");
        require(_amount > 0, "Invalid amount");
        
        // add new stake
        _newDeposit(NOTE, _amount, _referrerID);
        
        // update referral reward
        _updateReferralReward(_amount, _referrerID);
        
        // transfer tokens from user to the contract balance
        require(ERC20Interface(NOTE).transferFrom(msg.sender, address(this), _amount));
        
        emit Staked(msg.sender, _amount);
        
    }