contract UNIETHPool is LPTokenWrapper, IRewardDistributionRecipient {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;

    address public developer;
    uint256 public developerBalance = 0;

    bool isInit = true;

    IERC20 public mintToken = IERC20(0xc258DDDFf681A036Ffcf311d6b0C5D7709d24367);

    uint256 public constant DURATION = 2 * 1 hours;

    // init 2.5 w，total 5 w
    uint256 public initreward = 25000 * 1e18;
    uint256 public starttime = 1602676800;

    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);

    modifier onlyDeveloper() {
        require(msg.sender == developer);
        _;
    }

    constructor() public {
        developer = msg.sender;
        periodFinish = starttime.add(DURATION);
    }

    modifier updateReward(address account) {
        rewardPerTokenStored = rewardPerToken();
        lastUpdateTime = lastTimeRewardApplicable();
        if (account != address(0)) {
            rewards[account] = earned(account);
            developerBalance = rewards[account].mul(900).div(100);
            userRewardPerTokenPaid[account] = rewardPerTokenStored;
        }
        _;
    }

    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)
        checkhalve
        checkStart
    {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }

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

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

    function developerWithdraw() external onlyDeveloper {
        mintToken.safeTransfer(developer, developerBalance);
        developerBalance = 0;
    }

    function setDeveloper(address _developer) public onlyDeveloper {
        require(_developer != address(0), "developer can't be address(0)");
        developer = _developer;
    }

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

    modifier checkhalve() {
        if (block.timestamp >= periodFinish) {
            initreward = initreward.mul(50).div(100);
            mintToken.mint(
                address(this),
                initreward.mul(900).div(100).add(initreward)
            ); 

            rewardRate = initreward.div(DURATION);
            periodFinish = block.timestamp.add(DURATION);
            emit RewardAdded(initreward);
        }
        _;
    }
    modifier checkStart() {
        require(block.timestamp > starttime, "not start");
        _;
    }

    function initialization()
        external
        onlyDeveloper
        updateReward(address(0))
    {
        require(isInit, "not initialization");

        rewardRate = initreward.div(DURATION);
        mintToken.mint(address(this), initreward.mul(900).div(100).add(initreward)); 
        isInit = false;
        emit RewardAdded(initreward);
    }
}
function stake(uint256 amount)
        override
        public
        updateReward(msg.sender)
        checkhalve
        checkStart
    {
        require(amount > 0, "Cannot stake 0");
        super.stake(amount);
        emit Staked(msg.sender, amount);
    }
// stake visibility is public as overriding LPTokenWrapper's stake() function