import {useEffect, useState} from 'react';
import styled from 'styled-components';

export function CookiePopup() {
    const [visible, setVisible] = useState(false);
    const [hiding, setHiding] = useState(false);

    useEffect(() => {
        if (!localStorage.getItem('cookie_accepted')) {
            const timerId = window.setTimeout(() => {
                setVisible(true);
            }, 500);

            return () => window.clearTimeout(timerId);
        }
    }, []);

    if (!visible) return null;

    function accept() {
        localStorage.setItem('cookie_accepted', 'true');
        setHiding(true);

        window.setTimeout(() => {
            setVisible(false);
        }, 400);
    }

    return (
        <Popup $hiding={hiding}>
            <div>
                <IconWrapper>
                    <img src="/images/cookie.png" alt="Cookie icon"/>
                </IconWrapper>

                <Text>
                    Мы используем куки и рекомендательные технологии
                    в соответствии с <a href="https://hvatilo.ru/land/cookies_and_recommendations.html" target='_blank'>политикой конфиденциальности</a>
                    , это помогает улучшать сервис и запоминать ваши настройки
                </Text>
            </div>

            <AcceptButton type="button" onClick={accept}>
                Хорошо
            </AcceptButton>
        </Popup>
    );
}

const Popup = styled.div<{ $hiding: boolean }>`
    position: fixed;
    bottom: 25px;
    left: 50%;
    transform: translateX(-50%) translateY(${({$hiding}) => ($hiding ? '30px' : '0')});
    background: #ffffff;
    border-radius: 20px;
    border: 2px solid #E7EDE7;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.05), 0 4px 10px rgba(0, 0, 0, 0.02);
    padding: 16px 32px;
    display: flex;
    align-items: center;
    gap: 20px;
    width: 95%;
    max-width: 780px;
    z-index: 9999;
    opacity: ${({$hiding}) => ($hiding ? 0 : 1)};
    visibility: ${({$hiding}) => ($hiding ? 'hidden' : 'visible')};
    transition: all 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);

    & > div {
        flex-direction: row;
        display: flex;
        gap: 24px;
    }
    
    @media (max-width: 600px) {
        flex-wrap: wrap;
        justify-content: center;
        padding: 20px;
        bottom: 15px;
        text-align: center;
        border-radius: 16px;
        gap: 12px;
    }
`;

const IconWrapper = styled.div`
    flex-shrink: 0;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #edf2f7;
    box-shadow: inset 0 4px 10px 0 rgba(12, 59, 46, 0.1);
    overflow: hidden;

    img {
        width: 85%;
        height: 85%;
        object-fit: contain;
    }

    @media (max-width: 600px) {
        width: 50px;
        height: 50px;
    }
`;

const Text = styled.p`
    display: flex;
    margin: 0;
    flex-grow: 1;
    color: ${({theme}) => theme.colors.darkGreen};
    font-size: 14px;
    line-height: 20px;
    letter-spacing: 0.01em;

    a {
        color: #69a342;
        font-weight: 600;
        display: contents;
    }

    @media (max-width: 600px) {
        font-size: 14px;
        width: 100%;
        order: 2;
    }
`;

const AcceptButton = styled.button`
    flex-shrink: 0;
    background-color: #d6e6d4;
    color: #1c3a2e;
    border: none;
    padding: 10px 24px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.1s ease;
    white-space: nowrap;

    &:hover {
        background-color: #c2d6c0;
    }

    &:active {
        transform: scale(0.96);
    }

    @media (max-width: 600px) {
        width: 100%;
        justify-content: center;
        padding: 12px;
        order: 3;
    }
`;