'use client'
import React, {useEffect, useState} from 'react';
import styled from "styled-components";
import {blackColor, darkGreenColor, lightGreenColor} from "@/styles/colors";
import {AnimatePresence, motion} from "framer-motion";

type Room = {
    id: number;
    name: string;
    description: string;
    cpu: string;
    gpu: string;
    ram: string;
    monitor: string;
    mouse: string;
    keyboard: string;
    headphones: string;
    chair: string;
    bracket: string;
};

const RoomButton = styled.button`
    width: 472px;
    min-height: 100px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 24px 40px 16px;
    background-color: ${blackColor};
    border-radius: 20px;
    color: white;
    position: relative;
    text-align: left;
    font-size: 24px;
    line-height: 1;
    transition: all 0.3s ease;
    .room-text{
        width: 100%;
    }
    
    p {
        margin-bottom: 10px;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        text-transform: uppercase;
    }
    
    &.active {
        background: linear-gradient(to right, ${lightGreenColor}, ${darkGreenColor});
        color: white;
    }
    
    @media (max-width: 1400px) {
        width: 100%;
        padding: 20px 30px 14px;
        p {
            font-size: 20px;
        }
    }
    
    @media (max-width: 800px) {
        padding: 20px 20px 18px;
        display: flex;
        justify-content: center;
        min-height: 70px;
        p {
            text-align: center;
            font-size: 18px;
            margin-bottom: 0;
        }
    }
    
    @media (max-width: 480px) {
        padding: 14px 20px 10px;
        min-height: 70px;
        p {
            font-size: 16px;
        }
    }
`;

const RoomTitle = styled.div`
  font-size: 32px;
  font-weight: 800;
  text-transform: uppercase;
  line-height: 1;

  @media (max-width: 1400px) {
    font-size: 28px;
  }

  @media (max-width: 800px) {
    font-size: 24px;
  }

  @media (max-width: 480px) {
    font-size: 20px;
  }
`;


const Wrapper = styled.div`
    display: flex;
    justify-content: space-between;
    align-items: start;
    width: 100%;
    max-width: 1400px;
    margin: 74px auto 0;
    padding: 0 20px;
    gap: 32px;
    
    .roomsList {
        display: flex;
        justify-content: start;
        align-items: start;
        flex-direction: column;
        gap: 12px;
        width: 50%;
        .gradient-border {
            width: 100%;
            border: 2px solid transparent;
            border-radius: 20px;
            background: linear-gradient(to right, ${lightGreenColor}, ${darkGreenColor});
            background-origin: border-box;
        }
    }
    
    @media (max-width: 1200px) {
        width: 90%;
        gap: 32px;
        .roomsList {
            width: 60%;
        }
    }
    
    @media (max-width: 800px) {
        flex-direction: column;
        gap: 40px;
        margin-top: 30px;
        .roomsList {
            width: 100%;
        }
    }
    
    @media (max-width: 480px) {
        padding: 0 12px;
        margin-top: 30px;
        gap: 30px;
    }
`;

const RoomInfo = styled.div`
    background: ${blackColor};
    padding: 32px 52px;
    height: 100%;
    border-radius: 20px;
    h4 {
        background: linear-gradient(to right, ${lightGreenColor}, ${darkGreenColor});
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        text-transform: uppercase;
        font-size: 36px;
        text-align: center;
        margin-bottom: 24px;
    }
    
    @media (max-width: 1200px) {
        padding: 28px 20px;
        h4 {
            font-size: 32px;
            margin-bottom: 20px;
        }
    }
    @media (max-width: 1050px) {
        padding:20px 36px;
    }
    
    @media (max-width: 800px) {
        padding: 26px 32px 42px;
        
        h4 {
            line-height: 1;
            font-size: 40px;
            margin-bottom: 16px;
        }
    }
    
    @media (max-width: 480px) {
        padding: 20px 12px 32px;
        h4 {
            font-size: 32px;
            margin-bottom: 0px;
        }
    }
`;

const RoomInfo_block = styled.div`
    width: 100%;
    max-width: 600px;
    margin: 24px auto 0;
    h5 {
        background: linear-gradient(to right, ${lightGreenColor}, ${darkGreenColor});
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        font-size: 24px;
        text-transform: uppercase;
        margin-bottom: 16px;
    }
    
    @media (max-width: 1200px) {
        margin: 20px auto 0;
        h5 {
            font-size: 22px;
            margin-bottom: 14px;
        }
    }
    
    @media (max-width: 800px) {
        margin: 16px auto 0;
        h5 {
            font-size: 22px;
            margin-bottom: 12px;
            text-align: center;
        }
    }
    
    @media (max-width: 480px) {
        margin: 32px auto 0;
        h5 {
            font-size: 24px;
            margin-bottom: 10px;
        }
    }
`;

const RoomBorder = styled.div`
    border: 2px solid transparent;
    border-radius: 20px;
    background: linear-gradient(to right, ${lightGreenColor}, ${darkGreenColor});
    background-origin: border-box;
    width: 100%;
    height: 100%;
    overflow: hidden;
    
    @media (max-width: 1200px) {
        width: 550px;
    }
    @media (max-width: 1050px) {
        width: 450px;
    }
    @media (max-width: 900px) {
        width: 380px;
    }
    @media (max-width: 800px) {
        width: 100%;
        margin: 0 auto;
    }
`;

const RoomDetail = styled.div`
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 24px;
    text-transform: uppercase;
    &:last-child {
        margin-bottom: 0;
    }
    
    .label {
        color: rgba(255, 255, 255, 0.8);
    }
    
    .value {
        text-align: right;
        max-width: 50%;
        word-wrap: break-word;
    }
    
    @media (max-width: 1200px) {
        font-size: 20px;
    }
    
    @media (max-width: 800px) {
        font-size: 22px;
        .value {
            max-width: 45%;
        }
    }
    
    @media (max-width: 480px) {
        flex-direction: column;
        align-items: center;
        text-align: center;
        gap: 2px;
        margin-bottom: 20px;
        
        .label {
            font-size: 20px;
            line-height: 1;
        }
        
        .value {
            max-width: 100%;
            text-align: center;
            font-size: 20px;
            line-height: 1;
        }
    }
`;
const RoomDescription = styled.div`
  font-size: 20px;
  font-weight: 600;
  margin-top: 10px;
  line-height: 1.3;
  text-transform: none;

  @media (max-width: 1400px) {
    font-size: 18px;
  }

  @media (max-width: 800px) {
    font-size: 16px;
  }

  @media (max-width: 480px) {
    font-size: 15px;
  }
`;


const RoomList = () => {
    const [rooms, setRooms] = useState<Room[]>([]);
    const [selectedRoom, setSelectedRoom] = useState<Room | null>(null);



    useEffect(() => {
        fetch('/api/rooms')
            .then((res) => res.json())
            .then((data) => {
                setRooms(data)
                setSelectedRoom(data[0])
            })
    }, []);
    return (
        <Wrapper>
            <div className={'roomsList'}>
                {rooms.map((room)=>(
                    <div key={room.id} className={'gradient-border'}>
                        <RoomButton
                            className={room.id === selectedRoom?.id ? 'active' : ''}
                            onClick={() => setSelectedRoom(room)}
                        >
                            <div className="room-text">
                                <RoomTitle>{room.name}</RoomTitle>
                                <RoomDescription>{room.description}</RoomDescription>
                            </div>
                        </RoomButton>
                    </div>
                ))}
            </div>
            <AnimatePresence mode="wait">
                <div style={{width: '100%'}}>
                    {selectedRoom && (
                        selectedRoom.name === 'ps' ? (
                            <motion.div
                                key={selectedRoom.id}
                                initial={{opacity: 0, y: 50}}
                                animate={{opacity: 1, y: 0}}
                                exit={{opacity: 0, y: -50}}
                                transition={{duration: 0.4, ease: "easeOut"}}
                            >
                                <RoomBorder>
                                    <RoomInfo>
                                        <motion.h4
                                            initial={{opacity: 0, y: -50}}
                                            animate={{opacity: 1, y: 0}}
                                            transition={{duration: 0.4, ease: "easeOut", delay: 0.1}}
                                        >
                                            {selectedRoom.name}
                                        </motion.h4>
                                        <motion.div
                                            initial={{opacity: 0, y: 50}}
                                            animate={{opacity: 1, y: 0}}
                                            transition={{duration: 0.4, ease: "easeOut", delay: 0.2}}
                                        >
                                            <RoomInfo_block>
                                                <h5>Cистемный блок</h5>
                                                <RoomDetail>
                                                    <span className="label">Приставки</span>
                                                    <span className="value">{selectedRoom.cpu}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Геймпады</span>
                                                    <span className="value">{selectedRoom.gpu}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Телевизор</span>
                                                    <span className="value">{selectedRoom.ram}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Удобынй большой диван и стол</span>
                                                </RoomDetail>
                                            </RoomInfo_block>
                                        </motion.div>
                                    </RoomInfo>
                                </RoomBorder>
                            </motion.div>
                        ) : (
                            <motion.div
                                key={selectedRoom.id}
                                initial={{opacity: 0, y: 50}}
                                animate={{opacity: 1, y: 0}}
                                exit={{opacity: 0, y: -50}}
                                transition={{duration: 0.4, ease: "easeOut"}}
                            >
                                <RoomBorder>
                                    <RoomInfo>
                                        <motion.h4
                                            initial={{opacity: 0, y: -50}}
                                            animate={{opacity: 1, y: 0}}
                                            transition={{duration: 0.4, ease: "easeOut", delay: 0.1}}
                                        >
                                            {selectedRoom.name}
                                        </motion.h4>
                                        <motion.div
                                            initial={{opacity: 0, y: 50}}
                                            animate={{opacity: 1, y: 0}}
                                            transition={{duration: 0.4, ease: "easeOut", delay: 0.2}}
                                        >
                                            <RoomInfo_block>
                                                <h5>Cистемный блок</h5>
                                                <RoomDetail>
                                                    <span className="label">Процессор</span>
                                                    <span className="value">{selectedRoom.cpu}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Озу</span>
                                                    <span className="value">{selectedRoom.ram}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Видеокарта</span>
                                                    <span className="value">{selectedRoom.gpu}</span>
                                                </RoomDetail>
                                            </RoomInfo_block>
                                            <RoomInfo_block>
                                                <h5>Периферия</h5>
                                                <RoomDetail>
                                                    <span className="label">Мышка</span>
                                                    <span className="value">{selectedRoom.mouse}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Клавиатура</span>
                                                    <span className="value">{selectedRoom.keyboard}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Наушники</span>
                                                    <span className="value">{selectedRoom.headphones}</span>
                                                </RoomDetail>
                                            </RoomInfo_block>
                                            <RoomInfo_block>
                                                <h5>Монитор+Кронштейн</h5>
                                                <RoomDetail>
                                                    <span className="label">Монитор</span>
                                                    <span className="value">{selectedRoom.monitor}</span>
                                                </RoomDetail>
                                                <RoomDetail>
                                                    <span className="label">Кронштейн</span>
                                                    <span className="value">{selectedRoom.bracket}</span>
                                                </RoomDetail>
                                            </RoomInfo_block>
                                            <RoomInfo_block>
                                                <RoomDetail>
                                                    <h5>Кресло</h5>
                                                    <span className="value">{selectedRoom.chair}</span>
                                                </RoomDetail>
                                            </RoomInfo_block>
                                        </motion.div>
                                    </RoomInfo>
                                </RoomBorder>
                            </motion.div>
                        )
                    )}
                </div>
            </AnimatePresence>
        </Wrapper>
);
};

export default RoomList;

