// File: test.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;

import "@openzeppelin/contracts/security/Pausable.sol";


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 Ownable is Context {
    address private _owner;

    event OwnershipTransferred(
        address indexed previousOwner,
        address indexed newOwner
    );
    constructor() {
        _transferOwnership(_msgSender());
    }
    function owner() public view virtual returns (address) {
        return _owner;
    }
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(
            newOwner != address(0),
            "Ownable: new owner is the zero address"
        );
        _transferOwnership(newOwner);
    }
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}
pragma solidity ^0.8.18;

library Address {
    function isContract(address account) internal view returns (bool) {
        uint256 size;
        assembly {
            size := extcodesize(account)
        }
        return size > 0;
    }
    function functionCall(
        address target,
        bytes memory data
    ) internal returns (bytes memory) {
        return functionCall(target, data, "Address: low-level call failed");
    }
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return
            functionCallWithValue(
                target,
                data,
                value,
                "Address: low-level call with value failed"
            );
    }
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(
            address(this).balance >= value,
            "Address: insufficient balance for call"
        );
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(
            data
        );
        return verifyCallResult(success, returndata, errorMessage);
    }
    function functionStaticCall(
        address target,
        bytes memory data
    ) internal view returns (bytes memory) {
        return
            functionStaticCall(
                target,
                data,
                "Address: low-level static call failed"
            );
    }
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }
    function functionDelegateCall(
        address target,
        bytes memory data
    ) internal returns (bytes memory) {
        return
            functionDelegateCall(
                target,
                data,
                "Address: low-level delegate call failed"
            );
    }
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            if (returndata.length > 0) {
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}
pragma solidity ^0.8.18;

library SafeERC20 {
    using Address for address;

    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(token.transfer.selector, to, value)
        );
    }

    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(token.transferFrom.selector, from, to, value)
        );
    }
    function safeApprove(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(token.approve.selector, spender, value)
        );
    }

    function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender) + value;
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(
                token.approve.selector,
                spender,
                newAllowance
            )
        );
    }

    function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(
                oldAllowance >= value,
                "SafeERC20: decreased allowance below zero"
            );
            uint256 newAllowance = oldAllowance - value;
            _callOptionalReturn(
                token,
                abi.encodeWithSelector(
                    token.approve.selector,
                    spender,
                    newAllowance
                )
            );
        }
    }
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        bytes memory returndata = address(token).functionCall(
            data,
            "SafeERC20: low-level call failed"
        );
        if (returndata.length > 0) {
            // Return data is optional
            require(
                abi.decode(returndata, (bool)),
                "SafeERC20: ERC20 operation did not succeed"
            );
        }
    }
}
pragma solidity ^0.8.18;

interface IERC20Metadata is IERC20 {
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
}
pragma solidity ^0.8.18;


contract Presale is Ownable , Pausable{
    using SafeERC20 for IERC20;
    using SafeERC20 for IERC20Metadata;
    uint256[4] public rate;
    address public saleToken;
    uint public saleTokenDec;
    uint256 public totalTokensforSale;
    uint256 public maxBuyLimit;
    uint256 public minBuyLimit;
    mapping(address => bool) public tokenWL;
    mapping(address => uint256[4]) public tokenPrices;
    uint256 public presaleStartTime;
    uint256 public presaleEndTime;
    bool public isUnlockingStarted;
    mapping(address => BuyerTokenDetails) public buyersAmount;
    uint256 public totalTokensSold;

    struct BuyerTokenDetails {
        uint amount;
        bool isClaimed;
    }

    constructor() {}
    modifier isPresaleHasNotStarted() {
        if (presaleStartTime != 0) {
            require(
                block.timestamp < presaleStartTime,
                "Presale: Presale has already started"
            );
        }
        _;
    }
    modifier isPresaleStarted() {
        require(
            block.timestamp >= presaleStartTime,
            "Presale: Presale has not started yet"
        );
        _;
    }
    modifier isPresaleNotEnded() {
        require(block.timestamp < presaleEndTime, "Presale: Presale has ended");
        _;
    }
    modifier isPresaleEnded() {
        require(
            block.timestamp >= presaleEndTime,
            "Presale: Presale has not ended yet"
        );
        _;
    }
    event TokenAdded(address token, uint256[4] price);
    event TokenUpdated(address token, uint256[4] price);
    event TokensBought(
        address indexed buyer,
        address indexed token,
        uint256 amount,
        uint256 tokensBought
    );
    event TokensUnlocked(address indexed buyer, uint256 amount);
    event SaleTokenAdded(address token, uint256 amount);

    function setSaleTokenParams(
        address _saleToken,
        uint256 _totalTokensforSale
    ) external onlyOwner{
        require(
            _saleToken != address(0),
            "Presale: Sale token cannot be zero address"
        );
        require(
            _totalTokensforSale > 0,
            "Presale: Total tokens for sale cannot be zero"
        );
        saleToken = _saleToken;
        saleTokenDec = IERC20Metadata(saleToken).decimals();

        IERC20(saleToken).safeTransferFrom(
            msg.sender,
            address(this),
            _totalTokensforSale
        );
        totalTokensforSale = IERC20(saleToken).balanceOf(address(this));
        emit SaleTokenAdded(_saleToken, _totalTokensforSale);
    }

    function startAndSetPresaleEndTime(
        uint256 _presaleEndTime
    ) external onlyOwner {
        presaleStartTime = block.timestamp;
        presaleEndTime = _presaleEndTime;
    }

    function updateTime(
        uint256 _presaleEndTime
    ) external onlyOwner {
        presaleEndTime =  _presaleEndTime;
    }

    function addWhiteListedToken(
        address _token,
        uint256[4] memory _price
    ) external onlyOwner {
        tokenWL[_token] = true;
        tokenPrices[_token] = _price;
        emit TokenAdded(_token, _price);
    }
    function updateEthRate(uint256[4] memory _rate) external onlyOwner {
        rate = _rate;
    }
    function updateTokenRate(
        address _token,
        uint256[4] memory _price
    ) external onlyOwner {
        require(tokenWL[_token], "Presale: Token not whitelisted");
        tokenPrices[_token] = _price;
        emit TokenUpdated(_token, _price);
    }

    function startUnlocking() external onlyOwner isPresaleEnded {
        require(!isUnlockingStarted, "Presale: Unlocking has already started");
        isUnlockingStarted = true;
    }
    function stopUnlocking() external onlyOwner isPresaleEnded {
        require(isUnlockingStarted, "Presale: Unlocking hasn't started yet!");
        isUnlockingStarted = false;
    }


 
    function getCurrentTier() public pure returns (uint) {
        return 0;
    }
    
    function getTokenAmount(
        address token,
        uint256 amount
    ) public view returns (uint256) {
        uint256 amtOut;
        uint tier = getCurrentTier();
        if (token != address(0)) {
            require(tokenWL[token], "Presale: Token not whitelisted");

            amtOut = tokenPrices[token][tier] != 0
                ? (amount * (10 ** saleTokenDec)) / (tokenPrices[token][tier])
                : 0;
        } else {
            amtOut = rate[tier] != 0
                ? (amount * (10 ** saleTokenDec)) / (rate[tier])
                : 0;
        }
        return amtOut;
    }


    function buyToken(
        address _token,
        uint256 _amount
    ) external payable isPresaleStarted isPresaleNotEnded whenNotPaused{
        uint256 saleTokenAmt = _token != address(0)
            ? getTokenAmount(_token, _amount)
            : getTokenAmount(address(0), msg.value);
        require(
            saleTokenAmt >= minBuyLimit,
            "Presale: Min buy limit not reached"
        );
        require(
            buyersAmount[msg.sender].amount + saleTokenAmt <= maxBuyLimit,
            "Presale: Max buy limit reached for this phase"
        );
        require(
            (totalTokensSold + saleTokenAmt) <= totalTokensforSale,
            "Presale: Total Token Sale Reached!"
        );

        if (_token != address(0)) {
            require(_amount > 0, "Presale: Cannot buy with zero amount");
            require(tokenWL[_token], "Presale: Token not whitelisted");

            IERC20(_token).safeTransferFrom(msg.sender, address(this), _amount);
        }

        saleTokenAmt = saleTokenAmt ;

        totalTokensSold += saleTokenAmt;
        buyersAmount[msg.sender].amount += saleTokenAmt;

        emit TokensBought(msg.sender, _token, _amount, saleTokenAmt);
    }


    function withdrawToken() external whenNotPaused {
        require(isUnlockingStarted, "Presale: Locking period not over yet");

        require(
            !buyersAmount[msg.sender].isClaimed,
            "Presale: Already claimed"
        );

        uint256 tokensforWithdraw = buyersAmount[msg.sender].amount;
        buyersAmount[msg.sender].isClaimed = true;
        IERC20(saleToken).safeTransfer(msg.sender, tokensforWithdraw);

        emit TokensUnlocked(msg.sender, tokensforWithdraw);
    }

    function setMinBuyLimit(uint _minBuyLimit) external onlyOwner {
        minBuyLimit = _minBuyLimit;
    }

    function setMaxBuyLimit(uint _maxBuyLimit) external onlyOwner {
        maxBuyLimit = _maxBuyLimit;
    }

    function withdrawSaleToken(
        uint256 _amount
    ) external onlyOwner isPresaleEnded {
        IERC20(saleToken).safeTransfer(msg.sender, _amount);
    }

    function withdrawAllSaleToken() external onlyOwner isPresaleEnded {
        uint256 amt = IERC20(saleToken).balanceOf(address(this));
        IERC20(saleToken).safeTransfer(msg.sender, amt);
    }

    function withdraw(address token, uint256 amt) public onlyOwner {
        require(
            token != saleToken,
            "Presale: Cannot withdraw sale token with this method, use withdrawSaleToken() instead"
        );
        IERC20(token).safeTransfer(msg.sender, amt);
    }

    function withdrawAll(address token) public onlyOwner {
        require(
            token != saleToken,
            "Presale: Cannot withdraw sale token with this method, use withdrawAllSaleToken() instead"
        );
        uint256 amt = IERC20(token).balanceOf(address(this));
        withdraw(token, amt);
    }

    function withdrawCurrency(uint256 amt) public onlyOwner {
        payable(msg.sender).transfer(amt);
    }

    // Function to pause the contract (only callable by the owner)
    function pauseContract() external onlyOwner {
        _pause();
    }

    // Function to unpause the contract (only callable by the owner)
    function unpauseContract() external onlyOwner {
       _unpause();
    }

    
}
// File: @openzeppelin/contracts/security/Pausable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which allows children to implement an emergency stop
 * mechanism that can be triggered by an authorized account.
 *
 * This module is used through inheritance. It will make available the
 * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
 * the functions of your contract. Note that they will not be pausable by
 * simply including this module, only once the modifiers are put in place.
 */
abstract contract Pausable is Context {
    /**
     * @dev Emitted when the pause is triggered by `account`.
     */
    event Paused(address account);

    /**
     * @dev Emitted when the pause is lifted by `account`.
     */
    event Unpaused(address account);

    bool private _paused;

    /**
     * @dev Initializes the contract in unpaused state.
     */
    constructor() {
        _paused = false;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is not paused.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    modifier whenNotPaused() {
        _requireNotPaused();
        _;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is paused.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    modifier whenPaused() {
        _requirePaused();
        _;
    }

    /**
     * @dev Returns true if the contract is paused, and false otherwise.
     */
    function paused() public view virtual returns (bool) {
        return _paused;
    }

    /**
     * @dev Throws if the contract is paused.
     */
    function _requireNotPaused() internal view virtual {
        require(!paused(), "Pausable: paused");
    }

    /**
     * @dev Throws if the contract is not paused.
     */
    function _requirePaused() internal view virtual {
        require(paused(), "Pausable: not paused");
    }

    /**
     * @dev Triggers stopped state.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    function _pause() internal virtual whenNotPaused {
        _paused = true;
        emit Paused(_msgSender());
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual whenPaused {
        _paused = false;
        emit Unpaused(_msgSender());
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}