contract FrogInitPool is FrogTokenWrapper, IRewardDistributionRecipient {
    IERC20 public frog = IERC20(0x488E0369f9BC5C40C002eA7c1fe4fd01A198801c);
    address public devPool = address(0x0);
    uint256 public/* constant*/ DURATION = 7 days;

    uint256 public/* constant*/ startTime = 1999652800; //utc+8 2020-09-09 20:00:00
    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored = 0;
    uint256 public maxReward = 0;
    uint256 public claimedReward = 0;
    bool private open = true;
    bool private emergency = false;
    uint256 private constant _gunit = 1e18;
    mapping(address => uint256) public userRewardPerTokenPaid; 
    mapping(address => uint256) public rewards; // Unclaimed rewards
    mapping(address => uint256) public claimedRewards; // claimed 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);
    event SetEmergency(bool _emergency);

    constructor(address _frogToken, address _devPool) public {
        frog = IERC20(_frogToken);
        devPool = _devPool;
        rewardDistribution = msg.sender;
    }

    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 checkNotEnd checkStakeToken updateReward(msg.sender){ 
        require(amount > 0, "FROG-POOL: Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }

    function exit() external checkOpen checkStart checkEnd checkStakeToken updateReward(msg.sender){
        withdrawStakeToken(balanceOf(msg.sender));
        getReward();
    }

    function withdrawStakeToken(uint256 amount) internal{
        require(amount > 0, "FROG-POOL: Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

    function getReward() internal{
        uint256 reward = earned(msg.sender);
        uint256 userClaimedReward = claimedRewards[msg.sender];
        if (reward > 0 && claimedReward.add(reward) <= maxReward) {
            rewards[msg.sender] = 0;
            claimedReward = claimedReward.add(reward);
            frog.mint(msg.sender, reward);
            if(reward >= 10) frog.mint(devPool, reward.div(10));
            claimedRewards[msg.sender] = userClaimedReward.add(reward);
            emit RewardPaid(msg.sender, reward);
        }
    }

    function withdraw(uint256 amount) public checkOpen checkStart checkEnd checkStakeToken checkEmergency{
        require(amount > 0, "FROG-POOL: Cannot withdraw 0");
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

    function notifyRewardAmount(address _stakeToken, uint256 _startTime, uint256 _duration, uint256 reward)
        external
        onlyRewardDistribution
        checkOpen
        updateReward(address(0)) 
    {

        stakeToken = IERC20(_stakeToken);
        startTime = _startTime;
        DURATION = _duration;
        rewardRate = reward.div(DURATION);
        periodFinish = startTime.add(DURATION);
        lastUpdateTime = startTime;
        claimedReward = 0;
        maxReward = reward;

        // frog.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 setOpen(bool _open) external onlyOwner {
        open = _open;
        emit SetOpen(_open);
    }

    function setEmergency(bool _emergency) external onlyOwner {
        emergency = _emergency;
        emit SetEmergency(_emergency);
    }

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

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

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

    modifier checkEnd(){
        require(block.timestamp > periodFinish,"FROG-POOL: Not end");
        _;
    }

    modifier checkNotEnd(){
        require(block.timestamp <= periodFinish,"FROG-POOL: ended");
        _;
    }

    modifier checkOpen() {
        require(open, "FROG: Pool is closed");
        _;
    }

    modifier checkEmergency() {
        require(emergency, "FROG: Emergency is closed");
        _;
    }

    modifier checkStakeToken() {
        require(stakeToken != IERC20(0x0), "FROG: Pool is closed");
        _;
    }

    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }
}
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
 */