contract TEDFarming is LPTokenWrapper, IRewardDistributionRecipient {
    IERC20 public rewardToken;
    uint256 public duration = 30 days;

    /* Fees breaker, to protect withdraws if anything ever goes wrong */
    bool public breaker = false;
    mapping(address => uint) public farmLock; // period that your sake it locked to keep it for farming
    uint public lock = 0; // farm lock in blocks ~ 0 days for 15s/block
    address public admin;

    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 Farmed(address indexed user, uint256 amount);
    event Withdrawn(address indexed user, uint256 amount);
    event RewardPaid(address indexed user, uint256 reward);

    constructor(address _lpToken, address _rewardToken) public LPTokenWrapper(_lpToken) {
        admin = msg.sender;
        rewardToken = IERC20(_rewardToken);
    }

    function setBreaker(bool _breaker) external {
        require(msg.sender == admin, "admin only");
        breaker = _breaker;
    }
    
    function setLockTime(uint _lock) external {
        require(msg.sender == admin, "admin only");
        lock = _lock;
    }

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

    function seize(IERC20 _token, uint amount) external {
        require(msg.sender == admin, "admin only");
        require(_token != lpToken, "lpToken");
        _token.safeTransfer(admin, amount);
    }

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

    // farm visibility is public as overriding LPTokenWrapper's farm() function
    function farm(uint256 amount) public updateReward(msg.sender) {
        require(amount > 0, "Cannot farm 0");
        super.farm(amount);
        farmLock[msg.sender] = lock.add(block.number);
        emit Farmed(msg.sender, amount);
    }

    function withdraw(uint256 amount) public updateReward(msg.sender) {
        require(amount > 0, "Cannot withdraw 0");
        if (breaker == false) {
            require(farmLock[msg.sender] < block.number,"locked");
        }
        super.withdraw(amount);
        emit Withdrawn(msg.sender, amount);
    }

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

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

    function notifyRewardAmount(uint256 reward, uint256 _duration)
        external
        onlyRewardDistribution
        updateReward(address(0))
    {
        require(_duration > 0, "Duration must not be 0");
        duration = _duration.mul(1 days);
        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);
        require(rewardRate.mul(duration) <= rewardToken.balanceOf(address(this)), "Insufficient reward");
        emit RewardAdded(reward);
    }
}
function farm(uint256 amount) public updateReward(msg.sender) {
    require(amount > 0, "Cannot farm 0");
    super.farm(amount);
    farmLock[msg.sender] = lock.add(block.number);
    emit Farmed(msg.sender, amount);
}

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