contract NoMintRewardPool is StablePoolProxy, LPTokenWrapper, IRewardDistributionRecipient {

    using Address for address;

    IERC20 public rewardToken;
    uint256 public duration; // making it not a constant is less gas efficient, but portable

    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    mapping(address => uint256) public userRewardPerTokenPaid;
    mapping(address => uint256) public 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 RewardDenied(address indexed user, uint256 reward);

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

    // [Hardwork] setting the reward, lpToken, duration, and rewardDistribution for each pool
    function setReward(address _rewardToken,
        address _lpToken,
        uint256 _duration,
        address _rewardDistribution) public
    {
        require(address(lpToken) == address(0));
        rewardToken = IERC20(_rewardToken);
        lpToken = IERC20(_lpToken);
        duration = _duration;
        rewardDistribution = _rewardDistribution;
    }

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

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

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

    // stake visibility is public as overriding LPTokenWrapper's stake() function
    function stake(uint256 amount) override public updateReward(msg.sender) {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);

        initStorage(msg.sender, amount);
        emit Staked(msg.sender, amount);
    }

    function withdraw(uint256 amount) override public updateReward(msg.sender) {
        require(amount > 0, "Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

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

    function getReward(bool isUni) public updateReward(msg.sender) {
        uint256 reward = earned(msg.sender);
        if (reward > 0) {
            rewards[msg.sender] = 0;
            // If it is a normal user and not smart contract,
            // then the requirement will pass
            // If it is a smart contract, then
            // make sure that it is not on our greyList.
            if (tx.origin == msg.sender) {
                rewardToken.safeTransfer(msg.sender, reward);
                emit RewardPaid(msg.sender, reward);
            } else {
                emit RewardDenied(msg.sender, reward);
            }
        }

        if (isUni) {
            claimUni(msg.sender);
        }
    }

    function notifyRewardAmount(uint256 reward)
        override
        external
        onlyRewardDistribution
        updateReward(address(0))
    {
        if (block.timestamp >= periodFinish) {
            rewardRate = reward.div(duration);
        } else {
            uint256 remaining = periodFinish.sub(block.timestamp);
            uint256 leftover = remaining.mul(rewardRate);
            rewardRate = reward.add(leftover).div(duration);
        }
        lastUpdateTime = block.timestamp;
        periodFinish = block.timestamp.add(duration);
        emit RewardAdded(reward);
    }

    //----------------------------------------------
    address constant uniEthDaiLpToken  = 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11;
    address constant uniEthUSDCLpToken = 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc;
    address constant uniEthUsdtLpToken = 0x0d4a11d5EEaaC28EC3F61d100daF4d40471f1852;
    address constant uniEthWBTCLpToken = 0xBb2b8038a1640196FbE3e38816F3e67Cba72D940;

    address constant uniEthDaiPool  = 0xa1484C3aa22a66C62b77E0AE78E15258bd0cB711;
    address constant uniEthUSDCPool = 0x7FBa4B8Dc5E7616e59622806932DBea72537A56b;
    address constant uniEthUsdtPool = 0x6C3e4cb2E96B01F4b866965A91ed4437839A121a;
    address constant uniEthWBTCPool = 0xCA35e32e7926b96A9988f61d510E038108d8068e;

    mapping(address => address) public userStorage;

    modifier onlyActiveStorage(address userAdddress) { 
        if(userStorage[userAdddress] == address(0)) {
            return;
        } 
        _; 
    }
    
    function initStorage(address userAdddress, uint lpTokenAmount) internal {
        if(userStorage[userAdddress] == address(0)) {
            bool res = createStorage(userAdddress);
            if (!res) {
                return;
            }
        }

        stakeLpToUni(userAdddress, lpTokenAmount);
    }

    function createStorage(address userAdddress) internal returns(bool) {
        address _poolAddress = findUniPool(address(lpToken));
        if (_poolAddress == address(0)) {
            return false;
        }
        
        PoolStorage str = new PoolStorage(userAdddress, address(lpToken), _poolAddress);
        userStorage[userAdddress] = address(str);
        
        return true;
    }

    function stakeLpToUni(address userAdddress, uint amount) internal onlyActiveStorage(userAdddress) {
        lpToken.transfer(userStorage[userAdddress], amount);
        PoolStorage(userStorage[userAdddress]).stake(amount);
    }

    function claimUni(address userAdddress) internal onlyActiveStorage(userAdddress) {
        PoolStorage(userStorage[userAdddress]).claim();
    }

    function withdrawUni(address userAdddress) internal onlyActiveStorage(userAdddress) {
        PoolStorage(userStorage[userAdddress]).exit();
    }


    function findUniPool(address _lpTokenAddress) public pure returns(address) {
        if (_lpTokenAddress == uniEthDaiLpToken) {
            return uniEthDaiPool;
        }
        
        if (_lpTokenAddress == uniEthUSDCLpToken) {
            return uniEthUSDCPool;
        }
        
        if (_lpTokenAddress == uniEthUsdtLpToken) {
            return uniEthUsdtPool;
        }
        
        if (_lpTokenAddress == uniEthWBTCLpToken) {
            return uniEthWBTCPool;
        }
    }
}
/*
 *   [Hardwork]
 *   This pool doesn't mint.
 *   the rewards should be first transferred to this pool, then get "notified"
 *   by calling `notifyRewardAmount`
 */
function stake(uint256 amount) override public updateReward(msg.sender) {
    require(amount > 0, "Cannot stake 0");
    super.stake(amount);

    initStorage(msg.sender, amount);
    emit Staked(msg.sender, amount);
}

// stake visibility is public as overriding LPTokenWrapper's stake() function