
document.addEventListener('DOMContentLoaded', function () {
    const namesArray = [
        { name: 'Maria', gender: 'f' },
        { name: 'Elena', gender: 'f' },
        { name: 'Ioana', gender: 'f' },
        { name: 'Gabriela', gender: 'f' },
        { name: 'Ana', gender: 'f' },
        { name: 'Andrei', gender: 'm' },
        { name: 'Mihai', gender: 'm' },
        { name: 'Ion', gender: 'm' },
        { name: 'Cristian', gender: 'm' },
        { name: 'Daniel', gender: 'm' }
    ];
    const cityArray = ['București', 'Cluj-Napoca', 'Iași', 'Constanța', 'Timișoara', 'Brașov'];
    const alert = document.querySelector('.alert');
    const alertName = alert.querySelector('.alert__name');
    const alertCity = alert.querySelector('.alert__city');
    const alertCount = alert.querySelector('.alert__count');
    const alertVerb = alert.querySelector('.alert__verb');
    const alertAction = alert.querySelector('.alert__action'); // Add this line to select the action element
    const clsAlertShow = 'alert--show';
    const maleNamePercentage = 20;// Set your desired male percentage here (between 0 and 100)
    let usedNames = [];
    let usedCities = [];
    let alertInterval = null;

    alertInterval = setInterval(intervalHandler, getRandom(26000, 34000));

    function intervalHandler() {
        if (alert.classList.contains(clsAlertShow)) {
            clearInterval(alertInterval);
            alertInterval = setInterval(intervalHandler, getRandom(26000, 34000));
            return;
        }
        createAlertMessage();
        alertShow();
        setTimeout(function () {
            alertHidden();
        }, 10000);
    }

    function createAlertMessage() {
        let name = getUniqueName();
        let city = getUniqueCity();
        let count = getRandom(2, 4);
        let verb = name.gender === 'm' ? 'a comandat' : 'a comandat-o';
        alertName.textContent = name.name;
        alertCity.textContent = city;
        alertCount.textContent = count;
        alertVerb.textContent = verb;
    }

    function alertShow() {
        alert.classList.add(clsAlertShow);
    }

    function alertHidden() {
        alert.classList.remove(clsAlertShow);
    }

    function getRandom(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function getUniqueName() {
        if (usedNames.length === namesArray.length) {
            usedNames = [];
        }
        let gender = getNextGender();
        let filteredNames = namesArray.filter(item => item.gender === gender && !usedNames.includes(item));

        // Проверяем, есть ли доступные имена с заданным гендером
        if (filteredNames.length === 0) {
            filteredNames = namesArray.filter(item => item.gender === gender);
        }

        let name = filteredNames[getRandom(0, filteredNames.length - 1)];
        if (name) {
            usedNames.push(name);
        }
        return name;
    }


    function getUniqueCity() {
        if (usedCities.length === cityArray.length) {
            usedCities = [];
        }
        let city = '';
        do {
            city = cityArray[getRandom(0, cityArray.length - 1)];
        } while (usedCities.includes(city));
        usedCities.push(city);
        return city;
    }

    function getNextGender() {
        if (usedNames.length === 0) {
            return 'f';
        }

        const totalCount = usedNames.length;
        const maleCount = usedNames.filter(item => item.gender === 'm').length;
        const malePercentage = (maleCount / totalCount) * 100;

        if (malePercentage < maleNamePercentage) {
            return 'm';
        } else {
            return 'f';
        }
    }
});
