// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);

    event Approval(address indexed owner, address indexed spender, uint256 value);
}

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }
}

abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor () {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}

contract CATZUROVendor is Ownable {

    uint256 public price = 1 * 10**10;
    uint256 public totalsold; 
    uint256 public totalAvailable = 50000000000 * 10**18;
    bool isActive;
    uint256 public startTime;
    uint256 public endTime;
    uint256 day = 86400;
    IERC20 public token;

    uint256 minBuy = 1 * 10**16;
    uint256 maxBuy = 15 * 10**18;

    event Buy(address sender,uint256 totalvalue); 
   
    // constructor 
    constructor(){
    }

    function togglePresale() public onlyOwner {
        isActive = !isActive;
        startTime = block.timestamp;
        endTime = startTime + day * 7;
    }

    function buy() public payable {
        require(isActive, "Not allowed yet!");
        require(endTime > block.timestamp, "Presale ended!");
        require(msg.value >= minBuy, "Minimum buy is 0.1 ETH");
        require(msg.value <= maxBuy, "Maximum buy is 1 ETH");
       uint256 amountToBuy = (msg.value / price) * 10**18;
       require(totalsold + amountToBuy <= totalAvailable, "All the tokens have been sold!");
       token.transfer(_msgSender(), amountToBuy);
       totalsold += amountToBuy;

       emit Buy(_msgSender(), amountToBuy);
        
    }

    // end sale
    function endsale() public onlyOwner {
        // transfer all the remaining tokens to admin
        token.transfer(_msgSender(), token.balanceOf(address(this)));
        // ADD ETH TRANSFER
        uint256 balance = address(this).balance;
        ( bool transfer, ) = payable(_msgSender()).call{value: balance}("");
        
        require(transfer, "Transfer failed.");
    }

    function setToken(address _token) public onlyOwner {
        token = IERC20(_token);
    }
}