// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


contract Context {

    function _msgSender() internal view virtual returns (address payable) {
        return payable(msg.sender);
    }
    function _msgData() internal view virtual returns (bytes memory) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data; 
    }
}


interface IBEP20 {
    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);
}


library SafeMath {
    function add(uint256 a, uint256 b) internal pure returns (uint256) 
    
    {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;

        return c;
    }
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0, errorMessage);
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return mod(a, b, "SafeMath: modulo by zero");
    }
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }
}

contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    constructor ()  {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    function owner() public view returns (address) {
        return _owner;
    }
    modifier onlyOwner() {
        require(_owner == _msgSender(), "Ownable: caller is not the owner");
        _;
    }
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }
    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 WindowFlat_Presale is Ownable {
    using SafeMath for uint256;

    IBEP20 public token;
    IBEP20 public USDT;
    uint256 public TokenPriceETH = 120000000000000000000000; // 120000 tokens per eth
    uint256 public TokenPriceUSDT = 100000000000000000000;  //100  tokens per USDT
    event Deposited(address indexed user, uint256 amount);
    event Recovered(address token, uint256 amount);
    bool public presaleStatus;
    mapping(address => uint256) public deposits;

    constructor(IBEP20 _token,IBEP20 _USDT)  {
        token = _token;
        USDT = _USDT;
        presaleStatus = true;
    }

    receive() payable external {
        BuyWithETH();
    }
    
    function balanceOf(address account) public view returns (uint256) {
        return token.balanceOf(account);
    }


    function getToken(uint256 value) public view returns(uint256)
    {
        return (TokenPriceETH*value)/1e18;
    }

    function getTokenUSDT(uint256 value) public view returns(uint256)
    {
        return (TokenPriceUSDT*value)/1e6;
    }

    function BuyWithETH() public payable 
    {
        require(presaleStatus == true, "Presale : Presale is finished");
        require(msg.value > 0, "Presale : Unsuitable Amount");
        uint256 tokenAmount = getToken(msg.value);  
        token.transfer(msg.sender, tokenAmount);
        deposits[msg.sender] = deposits[msg.sender].add(msg.value);
        emit Deposited(msg.sender, msg.value);
    }

      function BuyWithUSDT(uint256 _amount ) public  
    {
        require(presaleStatus == true, "Presale : Presale is finished");
        require(_amount > 0, "Presale : Unsuitable Amount");
        USDT.transferFrom(msg.sender, address(this), _amount);
        uint256 tokenAmount = getTokenUSDT(_amount);
        token.transfer(msg.sender, tokenAmount);
        deposits[msg.sender] = deposits[msg.sender].add(_amount);
        emit Deposited(msg.sender, _amount);
    }

    function contractbalance() public view returns(uint256)
    {
      return address(this).balance;
    }

    function recoverBEP20(address tokenAddress, uint256 tokenAmount) external onlyOwner {
        IBEP20(tokenAddress).transfer(this.owner(), tokenAmount);
        emit Recovered(tokenAddress, tokenAmount);
    }
       function releaseFunds() external onlyOwner 
    {
        payable(msg.sender).transfer(address(this).balance);
    }
    
    function setRewardTokenPriceforETH(uint256 _count) external onlyOwner {
        TokenPriceETH = _count;
    }

     function setRewardTokenPriceforUSDT(uint256 _count) external onlyOwner {
        TokenPriceUSDT = _count;
    }

    function changeToken(IBEP20 _token) external onlyOwner{
        token=_token;
    }

    function stopPresale() external onlyOwner {
        presaleStatus = false;
    }

    function resumePresale() external onlyOwner {
        presaleStatus = true;
    }
    
  
}