contract NFIUSDTPool is NFITokenWrapper, IRewardDistributionRecipient {
    // !! Mainnet NFI token address
    IERC20 public nfi = IERC20(0x2CE41ACEcBb817D7307D6D6b38C643b269F5580E);
    uint256 public constant DURATION = 7 days;

    // !! Mainnet pool start time: utc+8 2020-11-11 20:00:00
    uint256 public constant startTime = 1605096000; //utc+8 2020-11-11 20:00:00
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored = 0;
    bool private open = true;
    uint256 private constant _gunit = 1e18;
    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public rewards; // Unclaimed rewards

    event RewardAdded(uint256 reward);
    event Staked(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward);
    event SetOpen(bool _open);

    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }

    function lastTimeRewardApplicable() public view returns (uint256) {
        return Math.min(block.timestamp, periodFinish);
    }

    /**
     * Calculate the rewards for each token
     */
    function rewardPerToken() public view returns (uint256) {
        if (totalSupply() == 0) {
            return rewardPerTokenStored;
        }
        return
        rewardPerTokenStored.add(
            lastTimeRewardApplicable()
            .sub(lastUpdateTime)
            .mul(rewardRate)
            .mul(_gunit)
            .div(totalSupply())
        );
    }

    function earned(address account) public view returns (uint256) {
        return
        balanceOf(account)
        .mul(rewardPerToken().sub(userRewardPerTokenPaid[account]))
        .div(_gunit)
        .add(rewards[account]);
    }

    function stake(uint256 amount) public checkOpen checkStart updateReward(msg.sender){
        require(amount > 0, "NFI-USDT-POOL: Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) public checkStart updateReward(msg.sender){
        require(amount > 0, "NFI-USDT-POOL: Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

    function exit() external {
        withdraw(balanceOf(msg.sender));
        getReward();
    }

    function getReward() public checkStart updateReward(msg.sender){
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            nfi.safeTransfer(msg.sender, reward);
            emit RewardPaid(msg.sender, reward);
        }
    }

    modifier checkStart(){
        require(block.timestamp > startTime,"NFI-USDT-POOL: Not start");
        _;
    }

    modifier checkOpen() {
        require(open, "NFI-USDT-POOL: Pool is closed");
        _;
    }

    function getPeriodFinish() external view returns (uint256) {
        return periodFinish;
    }

    function isOpen() external view returns (bool) {
        return open;
    }

    function setOpen(bool _open) external onlyOwner {
        open = _open;
        emit SetOpen(_open);
    }

    function notifyRewardAmount(uint256 reward)
    external
    onlyRewardDistribution
    checkOpen
    updateReward(address(0)) {
        if (block.timestamp > startTime){
            if (block.timestamp >= periodFinish) {
                uint256 period = block.timestamp.sub(startTime).div(DURATION).add(1);
                periodFinish = startTime.add(period.mul(DURATION));
                rewardRate = reward.div(periodFinish.sub(block.timestamp));
            } else {
                uint256 remaining = periodFinish.sub(block.timestamp);
                uint256 leftover = remaining.mul(rewardRate);
                rewardRate = reward.add(leftover).div(remaining);
            }
            lastUpdateTime = block.timestamp;
        }else {
            rewardRate = reward.div(DURATION);
            periodFinish = startTime.add(DURATION);
            lastUpdateTime = startTime;
        }

        nfi.mint(address(this),reward);
        emit RewardAdded(reward);

        // avoid overflow to lock assets
        _checkRewardRate();
    }

    function _checkRewardRate() internal view returns (uint256) {
        return DURATION.mul(rewardRate).mul(_gunit);
    }
}
function rewardPerToken() public view returns (uint256) {
    if (totalSupply() == 0) {
        return rewardPerTokenStored;
    }
    return
    rewardPerTokenStored.add(
        lastTimeRewardApplicable()
        .sub(lastUpdateTime)
        .mul(rewardRate)
        .mul(_gunit)
        .div(totalSupply())
    );
}

/**
 * Calculate the rewards for each token
 */