contract LPLockV1 is Ownable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;

    IERC20 public lp = IERC20(0xf52cC785D5F3E7D6FF0AD4e665a929b84F69e7c5);

    // timestamp that indicates when the owner can unlock
    uint256 public deadlock = 0;
    
    // timestamp that indicates when the owner can withdraw
    uint256 public timelock = 0;
    
    uint256 public constant SECONDS_PER_DAY = 1 days;
    
    // the withdrawal must wait 3 days after unlock
    uint256 public constant TIMELOCK_DURATION = 3 * SECONDS_PER_DAY;

    event Locked(uint256 deadlock);
    event Unlocked(uint256 timelock);
    event Deposited(uint256 amount);
    event Withdrawn(uint256 amount);

    // deadlock = now + lockDays
    // 1. in deadlock period, the owner can not unlock or withdraw.
    // 2. lock() can be invoked more than once to extend the deadlock.
    function lock(uint256 lockDays) external onlyOwner {
      uint256 lockSeconds = lockDays.mul(SECONDS_PER_DAY);
      require(
        currentTime().add(lockSeconds) > deadlock,
        "deadlock can not be decreased"
      );
      
      // reset timelock when lock()
      timelock = 0;
      
      deadlock = currentTime().add(lockSeconds);
      emit Locked(deadlock);
    }    

    // can invoke unlock() afeter deadlock only.
    // unlock() will setup a timelock. 
    // timelock = now + 3 days.
    function unlock() external onlyOwner {
      require(
        currentTime() > deadlock,
        "can not unlock before deadlock"
      );
      timelock = currentTime().add(TIMELOCK_DURATION);
      emit Unlocked(timelock);
    }

    // deposit all the lp to this contract.
    function depositAll() external onlyOwner {
      uint256 balance = lp.balanceOf(msg.sender);
      lp.safeTransferFrom(msg.sender, address(this), balance);
      emit Deposited(balance);
    }

    // withdrawAll() can be invoked after timelock.
    function withdrawAll() external onlyOwner {
      require(
        currentTime() > deadlock,
        "can not withdraw before deadlock"
      );
      require(
        timelock != 0 && currentTime() > timelock,
        "can not withdraw before timelock"
      );
      uint256 balance = lp.balanceOf(address(this));
      lp.safeTransfer(msg.sender, balance);
      emit Withdrawn(balance);
    }

    function currentTime() internal view returns (uint256) {
        return block.timestamp;
    }
}
function lock(uint256 lockDays) external onlyOwner {
  uint256 lockSeconds = lockDays.mul(SECONDS_PER_DAY);
  require(
    currentTime().add(lockSeconds) > deadlock,
    "deadlock can not be decreased"
  );
      
  // reset timelock when lock()
  timelock = 0;
      
  deadlock = currentTime().add(lockSeconds);
  emit Locked(deadlock);
}    

// deadlock = now + lockDays
// 1. in deadlock period, the owner can not unlock or withdraw.
// 2. lock() can be invoked more than once to extend the deadlock.