contract IndividualLockableToken is ERC20Pausable, Ownable{
  using SafeMath for uint256;

  event LockTimeSetted(address indexed holder, uint256 old_release_time, uint256 new_release_time);
  event Locked(address indexed holder, uint256 locked_balance_change, uint256 total_locked_balance, uint256 release_time);

  struct lockState {
    uint256 locked_balance;
    uint256 release_time;
  }

  // default lock period
  uint256 public lock_period = 24 weeks;

  mapping(address => lockState) internal userLock;

  // Specify the time that a particular person's lock will be released
  function setReleaseTime(address _holder, uint256 _release_time)
    public
    onlyOwner
    returns (bool)
  {
    require(_holder != address(0));
	require(_release_time >= block.timestamp);

	uint256 old_release_time = userLock[_holder].release_time;

	userLock[_holder].release_time = _release_time;
	emit LockTimeSetted(_holder, old_release_time, userLock[_holder].release_time);
	return true;
  }
  
  // Returns the point at which token holder's lock is released
  function getReleaseTime(address _holder)
    public
    view
    returns (uint256)
  {
    require(_holder != address(0));

	return userLock[_holder].release_time;
  }

  // Unlock a specific person. Free trading even with a lock balance
  function clearReleaseTime(address _holder)
    public
    onlyOwner
    returns (bool)
  {
    require(_holder != address(0));
    require(userLock[_holder].release_time > 0);

	uint256 old_release_time = userLock[_holder].release_time;

	userLock[_holder].release_time = 0;
	emit LockTimeSetted(_holder, old_release_time, userLock[_holder].release_time);
	return true;
  }

  // Increase the lock balance of a specific person.
  // If you only want to increase the balance, the release_time must be specified in advance.
  function increaseLockBalance(address _holder, uint256 _value)
    public
    onlyOwner
    returns (bool)
  {
	require(_holder != address(0));
	require(_value > 0);
	require(balanceOf(_holder) >= _value);
	
	if (userLock[_holder].release_time == 0) {
		userLock[_holder].release_time = block.timestamp + lock_period;
	}
	
	userLock[_holder].locked_balance = (userLock[_holder].locked_balance).add(_value);
	emit Locked(_holder, _value, userLock[_holder].locked_balance, userLock[_holder].release_time);
	return true;
  }

  // Decrease the lock balance of a specific person.
  function decreaseLockBalance(address _holder, uint256 _value)
    public
    onlyOwner
    returns (bool)
  {
	require(_holder != address(0));
	require(_value > 0);
	require(userLock[_holder].locked_balance >= _value);

	userLock[_holder].locked_balance = (userLock[_holder].locked_balance).sub(_value);
	emit Locked(_holder, _value, userLock[_holder].locked_balance, userLock[_holder].release_time);
	return true;
  }

  // Clear the lock.
  function clearLock(address _holder)
    public
    onlyOwner
    returns (bool)
  {
	require(_holder != address(0));
	require(userLock[_holder].release_time > 0);

	userLock[_holder].locked_balance = 0;
	userLock[_holder].release_time = 0;
	emit Locked(_holder, 0, userLock[_holder].locked_balance, userLock[_holder].release_time);
	return true;
  }

  // Check the amount of the lock
  function getLockedBalance(address _holder)
    public
    view
    returns (uint256)
  {
    if(block.timestamp >= userLock[_holder].release_time) return uint256(0);
    return userLock[_holder].locked_balance;
  }

  // Check your remaining balance
  function getFreeBalance(address _holder)
    public
    view
    returns (uint256)
  {
    if(block.timestamp >= userLock[_holder].release_time) return balanceOf(_holder);
    return balanceOf(_holder).sub(userLock[_holder].locked_balance);
  }

  // transfer overrride
  function transfer(
    address _to,
    uint256 _value
  )
    public
    returns (bool)
  {
    require(getFreeBalance(msg.sender) >= _value);
    return super.transfer(_to, _value);
  }

  // transferFrom overrride
  function transferFrom(
    address _from,
    address _to,
    uint256 _value
  )
    public
    returns (bool)
  {
    require(getFreeBalance(_from) >= _value);
    return super.transferFrom(_from, _to, _value);
  }

  // approve overrride
  function approve(
    address _spender,
    uint256 _value
  )
    public
    returns (bool)
  {
    require(getFreeBalance(msg.sender) >= _value);
    return super.approve(_spender, _value);
  }

  // increaseAllowance overrride
  function increaseAllowance(
    address _spender,
    uint _addedValue
  )
    public
    returns (bool success)
  {
    require(getFreeBalance(msg.sender) >= allowance(msg.sender, _spender).add(_addedValue));
    return super.increaseAllowance(_spender, _addedValue);
  }
  
  // decreaseAllowance overrride
  function decreaseAllowance(
    address _spender,
    uint _subtractedValue
  )
    public
    returns (bool success)
  {
	uint256 oldValue = allowance(msg.sender, _spender);
	
    if (_subtractedValue < oldValue) {
      require(getFreeBalance(msg.sender) >= oldValue.sub(_subtractedValue));	  
    }    
    return super.decreaseAllowance(_spender, _subtractedValue);
  }
}
function getFreeBalance(address _holder)
    public
    view
    returns (uint256)
  {
    if(block.timestamp >= userLock[_holder].release_time) return balanceOf(_holder);
    return balanceOf(_holder).sub(userLock[_holder].locked_balance);
  }
// Check your remaining balance