contract Bone is ERC20 {
    string public constant name = "Bone";
    string public constant symbol = "Bone";
    uint8 public constant decimals = 18;
    uint256 public constant initialSupply = 1000000000 * (10 ** uint256(decimals));
    bool public _lockStatus = false;
    bool private isValue;
    constructor() public {
        super._mint(msg.sender, initialSupply);
        owner = msg.sender;
    }
    mapping (address => uint256) private time;
    mapping (address => uint256) private _lockedAmount;

    //ownership
    address public owner;

    event OwnershipRenounced(address indexed previousOwner);
    event OwnershipTransferred(
     address indexed previousOwner,
     address indexed newOwner
    );

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }

  /**
   * @dev Allows the current owner to relinquish control of the contract.
   * @notice Renouncing to ownership will leave the contract without an owner.
   * It will not be possible to call the functions with the `onlyOwner`
   * modifier anymore.
   */
    function renounceOwnership() public onlyOwner {
        emit OwnershipRenounced(owner);
        owner = address(0);
    }

  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param _newOwner The address to transfer ownership to.
   */
    function transferOwnership(address _newOwner) public onlyOwner {
        _transferOwnership(_newOwner);
    }

  /**
   * @dev Transfers control of the contract to a newOwner.
   * @param _newOwner The address to transfer ownership to.
   */
    function _transferOwnership(address _newOwner) internal {
        require(_newOwner != address(0), "Already owner");
        emit OwnershipTransferred(owner, _newOwner);
        owner = _newOwner;
    }

    //pausable
    event Pause();
    event Unpause();

    bool public paused = false;

    /**
    * @dev Modifier to make a function callable only when the contract is not paused.
    */
    modifier whenNotPaused() {
        require(!paused, "Paused by owner");
        _;
    }

    /**
    * @dev Modifier to make a function callable only when the contract is paused.
    */
    modifier whenPaused() {
        require(paused, "Not paused now");
        _;
    }

    /**
    * @dev called by the owner to pause, triggers stopped state
    */
    function pause() public onlyOwner whenNotPaused {
        paused = true;
        emit Pause();
    }

    /**
    * @dev called by the owner to unpause, returns to normal state
    */
    function unpause() public onlyOwner whenPaused {
        paused = false;
        emit Unpause();
    }

    //freezable
    event Frozen(address target);
    event Unfrozen(address target);

    mapping(address => bool) internal freezes;

    modifier whenNotFrozen() {
        require(!freezes[msg.sender], "Sender account is locked.");
        _;
    }

    function freeze(address _target) public onlyOwner {
        freezes[_target] = true;
        emit Frozen(_target);
    }

    function unfreeze(address _target) public onlyOwner {
        freezes[_target] = false;
        emit Unfrozen(_target);
    }

    function isFrozen(address _target) public view returns (bool) {
        return freezes[_target];
    }


    /* ----------------------------------------------------------------------------
     * Locking functions
     * ----------------------------------------------------------------------------
     */

    /**
     * @dev Lock all transfer functions of the contract
     * @return request status
     */
    function setAllTransfersLockStatus(bool RunningStatusLock) external onlyOwner returns (bool)
    {
        _lockStatus = RunningStatusLock;
        return true;
    }

    /**
     * @dev check lock status of all transfers
     * @return lock status
     */
    function getAllTransfersLockStatus() public view returns (bool)
    {
        return _lockStatus;
    }

    /**
     * @dev time calculator for locked tokens
     */
     function addLockingTime(address lockingAddress,uint8 lockingTime, uint256 amount) internal returns (bool){
        time[lockingAddress] = now + (lockingTime * 1 days);
        _lockedAmount[lockingAddress] = amount;
        return true;
     }

       function transferByOwner(address to, uint256 value, uint8 lockingTime) public AllTransfersLockStatus onlyOwner returns (bool) {
        addLockingTime(to,lockingTime,value);
        _transfer(msg.sender, to, value);
        return true;
    }

     /**
      * @dev check for time based lock
      * @param _address address to check for locking time
      * @return time in block format
      */
      function checkLockingTimeByAddress(address _address) public view returns(uint256){
         return time[_address];
      }
      /**
       * @dev return locking status
       * @param userAddress address of to check
       * @return locking status in true or false
       */
       function getLockingStatus(address userAddress) public view returns(bool){
           if (now < time[userAddress]){
               return true;
           }
           else{
               return false;
           }
       }

    /**
     * @dev  Decreaese locking time
     * @param _affectiveAddress Address of the locked address
     * @param _decreasedTime Time in days to be affected
     */
    function decreaseLockingTimeByAddress(address _affectiveAddress, uint _decreasedTime) external onlyOwner returns(bool){
          require(_decreasedTime > 0 && time[_affectiveAddress] > now, "Please check address status or Incorrect input");
          time[_affectiveAddress] = time[_affectiveAddress] - (_decreasedTime * 1 days);
          return true;
      }

      /**
     * @dev Increase locking time
     * @param _affectiveAddress Address of the locked address
     * @param _increasedTime Time in days to be affected
     */
    function increaseLockingTimeByAddress(address _affectiveAddress, uint _increasedTime) external onlyOwner returns(bool){
          require(_increasedTime > 0 && time[_affectiveAddress] > now, "Please check address status or Incorrect input");
          time[_affectiveAddress] = time[_affectiveAddress] + (_increasedTime * 1 days);
          return true;
      }

    /**
     * @dev modifier to check validation of lock status of smart contract
     */
    modifier AllTransfersLockStatus()
    {
        require(_lockStatus == false,"All transactions are locked for this contract");
        _;
    }

    /**
     * @dev modifier to check locking amount
     * @param _address address to check
     * @param requestedAmount Amount to check
     * @return status
     */
     modifier checkLocking(address _address,uint256 requestedAmount){
         if(now < time[_address]){
         require(!( _balances[_address] - _lockedAmount[_address] < requestedAmount), "Insufficient unlocked balance");
         }
        else{
            require(1 == 1,"Transfer can not be processed");
        }
        _;
     }

    function transfer(
        address _to,
        uint256 _value
    )
      public
      AllTransfersLockStatus
      checkLocking(msg.sender,_value)
      whenNotFrozen
      whenNotPaused
      returns (bool)
    {
        releaseLock(msg.sender);
        return super.transfer(_to, _value);
    }

    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    )
      public
      AllTransfersLockStatus
      checkLocking(_from,_value)
      whenNotPaused
      returns (bool)
     {
        require(!freezes[_from], "From account is locked.");
        releaseLock(_from);
        return super.transferFrom(_from, _to, _value);
     }

    //mintable
    event Mint(address indexed to, uint256 amount);

    function mint(
        address _to,
        uint256 _amount
    )
      public
      onlyOwner
      returns (bool)
    {
        super._mint(_to, _amount);
        emit Mint(_to, _amount);
        return true;
    }

    //burnable
    event Burn(address indexed burner, uint256 value);

    function burn(address _who, uint256 _value) public
    onlyOwner
    checkLocking(msg.sender,_value)
    {
        require(_value <= super.balanceOf(_who), "Balance is too small.");

        _burn(_who, _value);
        emit Burn(_who, _value);
    }

    //lockable
    struct LockInfo {
        uint256 releaseTime;
        uint256 balance;
    }
    mapping(address => LockInfo[]) internal lockInfo;

    event Lock(address indexed holder, uint256 value, uint256 releaseTime);
    event Unlock(address indexed holder, uint256 value);

    function balanceOf(address _holder) public view returns (uint256 balance) {
        uint256 lockedBalance = 0;
        for(uint256 i = 0; i < lockInfo[_holder].length ; i++ ) {
            lockedBalance = lockedBalance.add(lockInfo[_holder][i].balance);
        }
        return super.balanceOf(_holder).add(lockedBalance);
    }

    function releaseLock(address _holder) internal {

        for(uint256 i = 0; i < lockInfo[_holder].length ; i++ ) {
            if (lockInfo[_holder][i].releaseTime <= now) {
                _balances[_holder] = _balances[_holder].add(lockInfo[_holder][i].balance);
                emit Unlock(_holder, lockInfo[_holder][i].balance);
                lockInfo[_holder][i].balance = 0;

                if (i != lockInfo[_holder].length - 1) {
                    lockInfo[_holder][i] = lockInfo[_holder][lockInfo[_holder].length - 1];
                    i--;
                }
                lockInfo[_holder].length--;

            }
        }
    }
    function lockCount(address _holder) public view returns (uint256) {
        return lockInfo[_holder].length;
    }
    function lockState(address _holder, uint256 _idx) public view returns (uint256, uint256) {
        return (lockInfo[_holder][_idx].releaseTime, lockInfo[_holder][_idx].balance);
    }

    function lock(address _holder, uint256 _amount, uint256 _releaseTime) public onlyOwner {
        require(super.balanceOf(_holder) >= _amount, "Balance is too small.");
        _balances[_holder] = _balances[_holder].sub(_amount);
        lockInfo[_holder].push(
            LockInfo(_releaseTime, _amount)
        );
        emit Lock(_holder, _amount, _releaseTime);
    }


    function lockAfter(address _holder, uint256 _amount, uint256 _afterTime) public onlyOwner {
        require(super.balanceOf(_holder) >= _amount, "Balance is too small.");
        _balances[_holder] = _balances[_holder].sub(_amount);
        lockInfo[_holder].push(
            LockInfo(now + _afterTime, _amount)
        );
        emit Lock(_holder, _amount, now + _afterTime);
    }

    function unlock(address _holder, uint256 i) public onlyOwner {
        require(i < lockInfo[_holder].length, "No lock information.");

        _balances[_holder] = _balances[_holder].add(lockInfo[_holder][i].balance);
        emit Unlock(_holder, lockInfo[_holder][i].balance);
        lockInfo[_holder][i].balance = 0;

        if (i != lockInfo[_holder].length - 1) {
            lockInfo[_holder][i] = lockInfo[_holder][lockInfo[_holder].length - 1];
        }
        lockInfo[_holder].length--;
    }

    function transferLockedTokens(address from, address to, uint256 value) external onlyOwner returns (bool){
        require((_lockedAmount[from] >= value) && (now < time[from]), "Insufficient unlocked balance");
        require(from != address(0) && to != address(0), "Invalid address");
        _lockedAmount[from] = _lockedAmount[from] - value;
        _transfer(from,to,value);
     }

    function transferWithLock(address _to, uint256 _value, uint256 _releaseTime) public onlyOwner returns (bool) {
        require(_to != address(0), "wrong address");
        require(_value <= super.balanceOf(owner), "Not enough balance");

        _balances[owner] = _balances[owner].sub(_value);
        lockInfo[_to].push(
            LockInfo(_releaseTime, _value)
        );
        emit Transfer(owner, _to, _value);
        emit Lock(_to, _value, _releaseTime);

        return true;
    }

    function transferWithLockAfter(address _to, uint256 _value, uint256 _afterTime) public onlyOwner returns (bool) {
        require(_to != address(0), "wrong address");
        require(_value <= super.balanceOf(owner), "Not enough balance");

        _balances[owner] = _balances[owner].sub(_value);
        lockInfo[_to].push(
            LockInfo(now + _afterTime, _value)
        );
        emit Transfer(owner, _to, _value);
        emit Lock(_to, _value, now + _afterTime);

        return true;
    }

    function currentTime() public view returns (uint256) {
        return now;
    }

    function afterTime(uint256 _value) public view returns (uint256) {
        return now + _value;
    }

    //airdrop
    mapping (address => uint256) public airDropHistory;
    event AirDrop(address _receiver, uint256 _amount);

    function airdropByOwner(address[] memory receivers, uint256[] memory values) public AllTransfersLockStatus onlyOwner {
    require(receivers.length != 0);
    require(receivers.length == values.length);

    for (uint256 i = 0; i < receivers.length; i++) {
      address receiver = receivers[i];
      uint256 amount = values[i];

      transfer(receiver, amount);
      airDropHistory[receiver] += amount;

      emit AirDrop(receiver, amount);
    }
  }
}
function getAllTransfersLockStatus() public view returns (bool)
{
    return _lockStatus;
}

/**
 * @dev check lock status of all transfers
 * @return lock status
 */