contract S4ERC20 is Context, IERC20, Ownable {
    event RenouncedWhitelist(bool);
    
    using SafeMath for uint256;
    using Address for address;
    uint256 public immutable oneDay;
    uint256 public immutable turning;
            
     
    bool public UniSwapSaleEnds;
    uint256 public UniSwapSaleEndsTime;
    // Transaction Fees:
    uint256 public txFee = 10; // 1% fees
    address public feeDistributor; 
    
    mapping(address => bool) public feelessSender;
    mapping(address => bool) public feelessReciever;
    mapping(address => bool) public UniSwapReciever;
    // 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 '''SetUniSwapSaleEnds''' 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;
        
        oneDay  = 60 * 60 * 24; //for 1 day
        turning = 6;
        
        UniSwapSaleEnds = false;
    }
    

    /**
     * @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 SetOneDayBehind(uint256 _days) public onlyOwner { //enter input in days to set fee started from that day
        UniSwapSaleEndsTime = block.timestamp.sub(_days.mul(oneDay));
    }


    function setUniSwapReciever(address _recipient, bool _feeless) public onlyOwner {
        require(_recipient != address(0), "ERC20: transfer from the zero address");
        UniSwapReciever[_recipient] = _feeless;
    }
    
    function setFeeStartStopCycle() public onlyOwner {
        UniSwapSaleEnds = !UniSwapSaleEnds;
        UniSwapSaleEndsTime = block.timestamp;
    }
    
   function seTxFee(uint256 _ff) public onlyOwner {
        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) {
       
        if(UniSwapReciever[recipient] && UniSwapSaleEnds){
            uint256 timeNow = block.timestamp;
            uint256 sfee = 5; //5% fee default if time not met in loop. 
            
            uint256 datediff = timeNow - UniSwapSaleEndsTime;
            
            datediff = datediff.div(oneDay);
            datediff = datediff.mod(turning);
            
            if(datediff == 0) { sfee = 30; }
            else if(datediff == 1) { sfee = 25; }
            else if(datediff == 2) { sfee = 20; }
            else if(datediff == 3) { sfee = 15; }
            else if(datediff == 4) { sfee = 10; }
            else if(datediff == 5) { sfee = 5; }
            
            sfee = amount.mul(sfee).div(100);
            return (amount.sub(sfee), sfee);
        }

        if (feelessSender[sender] || feelessReciever[recipient]) {
            return (amount, 0);
        }

        // calculate fees and amounts
        uint256 fee = amount.mul(txFee).div(1000);
        return (amount.sub(fee), fee);
    }

    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 _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 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.
 */