import React, {useEffect, useState} from 'react';
import styled from "styled-components";
import Image from "next/image";
import {blackColor, gradient, lightGreenColor} from "@/styles/colors";

const features = [
    {
        icon: 'stairs.svg',
        text: '2 игровых этажа',
        image: '1.png'
    },
    {
        icon: 'vip.svg',
        text: 'Все VIP кабины',
        image: '2.png'
    },
    {
        icon: 'energy.svg',
        text: 'Мощное железо',
        image: '3.png'
    },
    {
        icon: 'wc.svg',
        text: 'WC для девочек',
        image: '4.png'
    },
    {
        icon: 'smoke.png',
        text: 'Дымный Lounge',
        image: '5.png'
    },
    {
        icon: 'price.svg',
        text: 'Выгодные пакеты',
        image: '6.png'
    },
    {
        icon: 'group.svg',
        text: '3 VIP Bootcamp',
        image: '7.png'
    },
    {
        icon: 'vent.svg',
        text: 'Хорошая вентиляция',
        image: '8.png'
    },
    {
        icon: 'room.svg',
        text: 'Звукоизоляция',
        image: '9.png'
    },
    {
        icon: 'guard.svg',
        text: 'Безопасность',
        image: '10.png'
    },
    {
        icon: 'food.svg',
        text: 'Ассортимент еды',
        image: '11.png'
    },
    {
        icon: 'game.svg',
        text: '5 Зон с PS 5 Pro',
        image: '12.png'
    },

]

const FeaturesContent = styled.div`
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    position: relative;
    &::after{
        position: absolute;
        content: "";
        width: 400px;
        height: 600px;
        top: 80px;
        left: -500px;
        transform: rotate(40deg);
        border-radius: 50%;
        filter: blur(100px);
        opacity: .5;
        background: ${lightGreenColor};
    }
    @media (max-width: 1260px) {
        justify-content: center;
        gap: 40px;
    }
    @media (max-width: 1100px) {
        gap: 30px;
    }
    @media (max-width: 900px) {
        flex-direction: column;
        margin-top: 30px;
        gap: 30px;
    }
`
const TitleBlock = styled.h2`
    line-height: 1;
    width: 100%;
    @media (max-width: 1400px) {
        width: 100% !important;
        margin: 0 auto;
        font-size: 40px !important;
    }
    @media (max-width: 1260px) {
        width: 90% !important;
        margin: 0 auto;
    }
    @media (max-width: 1100px) {
        font-size: 40px !important;
        text-align: center !important;
        margin-bottom: 32px !important;
    }
    @media (max-width: 470px) {
        font-size: 32px !important;
    }
`
const FeaturesGrid = styled.div`
    display: grid;
    grid-template-columns: repeat(4, 140px);
    gap: 12px 16px;
    @media (max-width: 1100px) {
        grid-template-columns: repeat(4, 120px);
        gap: 10px 12px;
    }
    @media (max-width: 900px) {
        grid-template-columns: repeat(3, 130px);
        gap: 12px;
    }
    @media (max-width: 480px) {
        grid-template-columns: repeat(3, 100px);
        gap: 10px;
    }
    @media (max-width: 350px) {
        grid-template-columns: repeat(3, 90px);
        gap: 8px;
    }
`

const FeaturesImage = styled.div`
    display: flex;
    justify-content: space-between;
    align-items: center;
`
const ImageWrapper = styled.div`
    position: relative;
    width: 439px;
    height: 618px;
    @media (max-width: 1100px) {
        width: 380px;
        height: 534px;
    }

    @media (max-width: 900px) {
        width: 380px;
        height: 534px;
    }
    @media (max-width: 480px) {
        width: 320px;
        height: 350px;
    }
    @media (max-width: 350px) {
        width: 280px;
        height: 304px;
    }

    img {
        position: absolute;
        top: 0;
        left: 0;
        transition: opacity 0.5s ease;
        opacity: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
        &.visible {
            opacity: 1;
        }
    }
`;

const FeaturesBlock = styled.div<{ $active?: boolean }>`
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 4px;
    text-align: center;
    background: ${blackColor};
    border-radius: 20px;
    padding: 18px 0;
    position: relative;
    .highlight {
        position: absolute;
        width: 104%;
        height: 104%;
        top: -2%;
        left: -2%;
        background: ${gradient};
        border-radius: 20px;
        z-index: -1;
        transition: transform .3s ease;
        transform: ${({ $active }) => ($active ? 'scale(1)' : 'scale(.9)')};
    }
    img{
        height: 70px;
    }
    p{
        cursor: default;
        width: 120px;
        font-size: 19px;
        line-height: 1.2;
    }
    @media (max-width: 1100px) {
        padding: 12px 0;
        img {
            height: 45px;
        }
        p {
            font-size: 16px;
            width: 100px;
        }
    }
    @media (max-width: 900px) {
        padding: 14px 0;
        img {
            height: 55px;
        }
        p {
            font-size: 16px;
            width: 100px;
            margin-top: 6px;
        }
    }
    @media (max-width: 480px) {
        padding: 12px 0;
        img {
            height: 45px;
        }
        p {
            font-size: 14px;
            width: 90px;
            margin-top: 4px;
        }
    }
    @media (max-width: 350px) {
        padding: 10px 0;
        img {
            height: 40px;
        }
        p {
            font-size: 13px;
            width: 80px;
        }
    }
`


const FeaturesList = () => {
    const [activeIndex, setActiveIndex] = useState<number>(0)

    const useIsMobile = () => {
        const [isMobile, setIsMobile] = useState(false)

        useEffect(() => {
            const check = () => setIsMobile(window.innerWidth <= 1024)
            check()
            window.addEventListener('resize', check)
            return () => window.removeEventListener('resize', check)
        }, [])

        return isMobile
    }
    const isMobile = useIsMobile()

    return (
        <div>
            <TitleBlock style={{ textAlign: 'start' }}>
                <span>Твое топовое</span><br />
                игровое пространство
            </TitleBlock>
            <FeaturesContent>
                <FeaturesGrid>
                    {features.map((feature, index) => (
                        <FeaturesBlock
                            key={index}
                            $active={activeIndex === index}
                            onMouseEnter={!isMobile ? () => setActiveIndex(index) : undefined}
                            onClick={isMobile ? () => setActiveIndex(index) : undefined}
                        >
                            <div className="highlight" />
                            <Image src={'/icons/' + feature.icon} alt={'startLeft'} width={40} height={40} loading={'lazy'} />
                            <p>{feature.text}</p>
                        </FeaturesBlock>
                    ))}
                </FeaturesGrid>
                <FeaturesImage>
                    <ImageWrapper>
                        {features.map((f, i) => (
                            <Image
                                key={i}
                                src={`/images/features/${f.image}`}
                                alt={'feature'}
                                width={439}
                                height={618}
                                className={activeIndex === i ? 'visible' : ''}
                                style={{ zIndex: activeIndex === i ? 1 : 0 }}
                            />
                        ))}
                    </ImageWrapper>
                </FeaturesImage>
            </FeaturesContent>
        </div>
    );
};

export default FeaturesList;