contract ZEHCoin is Context, ERC1363, AccessControlEnumerable, ERC20Burnable, ERC20Pausable, ERC20Capped{
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    uint256 public sellPrice = 0; //in Wei.
    uint256 public buyPrice = 0;  //in Wei.
    address payable public _owner;
    bool public salePaused = true;
    event UpdateOwnership(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
     * account that deploys the contract.
     *
     * See {ERC20-constructor}.
     */
 
    constructor(string memory name, string memory symbol, uint256 _sellPrice,uint256 _buyPrice) ERC20(name, symbol) ERC20Capped(SafeMath.mul(3000000000 , (10 ** uint256(decimals())))) {
        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
        _setupRole(MINTER_ROLE, _msgSender());
        _setupRole(PAUSER_ROLE, _msgSender());
        _owner = payable(_msgSender());
        sellPrice = _sellPrice; // in Wei.
        buyPrice = _buyPrice;   // in Wei.
    } 
    // to recieve ether in contract.
    fallback() external payable {} 
    receive() external payable {}

   // @dev if owner wants to transfer contract ether balance to own account.
    function transferBalanceTo(address _to,uint256 _value) public {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "ERC20: must have admin role to tranfer balance");
        require(_value <= address(this).balance);
        payable(_to).transfer(_value);
    }
    // @dev only admin can set prices.
    function setPrice(uint256 _sellPrice,uint256 _buyPrice) public {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "ERC20: must have admin role to set price");
        sellPrice = _sellPrice; // in Wei.
        buyPrice = _buyPrice;   // in Wei.
    }
       // @notice Buy tokens from contract by  sending ether
    function buy() payable public {
        require(!salePaused, "Buying and Selling is Paused");
        uint256 amount = SafeMath.mul(msg.value , (10 ** uint256(decimals()))); 
        amount =  SafeMath.div(amount,buyPrice); // calculates the amount  
        _transfer(_owner, msg.sender, amount);   // makes the transfers
       
    }

    // @notice Sell `amount` tokens to contract
    function sell(uint256 amount) public {
        require(!salePaused,"Buying and Selling is Paused");
        require(amount > 0);
        require(balanceOf(msg.sender) >= amount);
        uint256 requiredBalance = SafeMath.mul(amount ,sellPrice); // calculates the amount.
        requiredBalance=SafeMath.div(requiredBalance, (10 ** uint256(decimals())));
        require(address(this).balance >= requiredBalance);  // checks if the contract has enough ether. 
        _transfer(msg.sender, _owner, amount); 

        if(!payable(msg.sender).send(requiredBalance)){
            return payable(msg.sender).transfer(requiredBalance); // sends ether to the seller.
        }
       
    }


    // /**
    // * @dev Allows the current owner to transfer control of the contract to a newOwner.
    // * @param newOwner The address to transfer ownership to.
    // */
    modifier onlyOwner {
        require(msg.sender == _owner);
        _;
    }
    function updateOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0));
        emit UpdateOwnership(_owner, newOwner);
        _owner = payable(newOwner);
    }

  
      /**
     * @dev See {ERC165-_supportsInterface,ERC1363-_supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(AccessControlEnumerable, ERC1363) returns (bool) {
        return super.supportsInterface(interfaceId);
    }
      /**
     * @dev See {ERC20-_mint,ERC20Capped-_mint}.
     */
    function _mint(address account, uint256 amount) internal virtual override(ERC20, ERC20Capped)  {
      super._mint(account, amount);
    }
    /**
     * @dev Creates `amount` new tokens for `to`.
     *
     * See {ERC20-_mint}.
     *
     * Requirements:
     *
     * - the caller must have the `MINTER_ROLE`.
     */
    function mint(address to, uint256 amount) public virtual {
        require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
        _mint(to, amount);
    }

    /**
     * @dev Pauses all token transfers.
     *
     * See {ERC20Pausable} and {Pausable-_pause}.
     *
     * Requirements:
     *
     * - the caller must have the `PAUSER_ROLE`.
     */
    function pause() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
        _pause();
    }
 
    /**
     * @dev Unpauses all token transfers.
     *
     * See {ERC20Pausable} and {Pausable-_unpause}.
     *
     * Requirements:
     *
     * - the caller must have the `PAUSER_ROLE`.
     */
    function unpause() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
        _unpause();
    }
    // buying and selling pause setting.
     function pauseSale() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
        salePaused = true;
    }
    function unPauseSale() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
        salePaused = false;
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual override(ERC20, ERC20Pausable) {
        super._beforeTokenTransfer(from, to, amount);
    }
}
/**
 * @dev {ERC20} token, including:
 *
 *  - ability for holders to burn (destroy) their tokens
 *  - a minter role that allows for token minting (creation)
 *  - a pauser role that allows to stop all token transfers
 *
 * This contract uses {AccessControl} to lock permissioned functions using the
 * different roles - head to its documentation for details.
 *
 * The account that deploys the contract will be granted the minter and pauser
 * roles, as well as the default admin role, which will let it grant both minter
 * and pauser roles to other accounts.
 */
function supportsInterface(bytes4 interfaceId) public view virtual override(AccessControlEnumerable, ERC1363) returns (bool) {
    return super.supportsInterface(interfaceId);
}

/**
 * @dev See {ERC165-_supportsInterface,ERC1363-_supportsInterface}.
 */