import { useEffect, useMemo, useReducer, useState } from 'react';
import { MiniTitle } from '../../ui/MiniTitle';
import { useWindowWidth } from '../../hooks/useWindowWidth';

export function Projects() {
  return (
    <div className="desktop:py-[70px] desktop:px-10 mobile:py-14 tablet:px-6 mobile:px-4 overflow-hidden select-none">
      <h1 className="text-[#ffffff] font-medium desktop:text-[64px] desktop:leading-[58px] desktop:mb-14 mobile:mb-6 tablet:text-[40px] tablet:leading-10 mobile:text-[28px] mobile:leading-7">
        <span
          className="bg-text-gradient bg-gradient-to-r from-[#798FFF] to-[#D375FF]"
          style={{
            backgroundClip: 'text',
            WebkitBackgroundClip: 'text',
            WebkitTextFillColor: 'transparent',
          }}
        >
          Большой опыт в работе
        </span>{' '}
        с промышленными предприятиями и учебными заведениями
      </h1>
      <MiniTitle text="реализованные проекты" />
      <Slider
        projects={[
          {
            src: 'src/assets/tank.png',
            tags: ['Симулятор', 'VR-приложение'],
            title: 'Ремонт и обслуживание двигателей спецтехники',
          },
          {
            src: 'src/assets/helicopter.jpg',
            tags: ['Симулятор'],
            title: 'Сборка-разборка вертолётного двигателя',
          },
          {
            src: 'src/assets/train.png',
            tags: ['Симулятор'],
            title: 'Симулятор машиниста',
          },
        ]}
      />
    </div>
  );
}

function Project({
  src,
  title,
  tags,
}: {
  src: string;
  title: string;
  tags: string[];
}) {
  return (
    <div className="bg-[#3D425C] bg-opacity-50 rounded-2xl box-border desktop:min-w-[624px] tablet:min-w-[520px] mobile:min-w-[328px] duration-1000 select-none">
      <div
        className="bg-cover h-[340px] rounded-2xl"
        style={{ backgroundImage: `url(${src})` }}
      />
      <div className="p-5">
        <h1 className="text-[#ffffff] font-medium desktop:text-lg tablet:text-base mobile:text-sm">
          {title}
        </h1>
        <div className="flex gap-2 mt-4">
          {tags.map(tag => (
            <p
              key={tag}
              className="text-[#ffffff] opacity-80 font-medium desktop:text-sm mobile:text-xs rounded-3xl py-3 px-4 border border-[#798FFF]"
            >
              {tag}
            </p>
          ))}
        </div>
      </div>
    </div>
  );
}

function Slider({
  projects,
}: {
  projects: { src: string; title: string; tags: string[] }[];
}) {
  const width = useWindowWidth();
  const baseOffset = useMemo(
    () => (width >= 1024 ? 640 : width >= 640 ? 536 : 336),
    [width],
  );
  const [sliderOffset, setSliderOffset] = useState(-baseOffset);
  const [slide, setSlide] = useState(0);

  const [order, dispatch] = useReducer(
    (state: typeof projects, action: string) => {
      if (action === 'next') {
        setSliderOffset(prev => prev + baseOffset);
        return [...state.slice(1), state[2]];
      }
      if (action === 'prev') {
        setSliderOffset(-baseOffset * 2);
        return [state[state.length - 3], ...state.slice(0, -1)];
      }
      return state;
    },
    [projects[projects.length - 1], ...projects, projects[0]],
  );

  const [touchStart, setTouchStart] = useState(0);

  useEffect(() => {
    setSliderOffset(-baseOffset);
  }, [order, baseOffset]);

  return (
    <div className="flex flex-col desktop:mt-4 tablet:mt-3 mobile:mt-2">
      <div
        className="flex gap-2 overflow-visible relative mb-[18px] -mr-10 duration-1000"
        style={{
          transition: `${+!(sliderOffset === 0 || sliderOffset === -baseOffset * 2)}s`,
          transform: `translateX(${sliderOffset}px)`,
        }}
        onTouchStart={e => {
          setTouchStart(e.targetTouches[0].clientX);
        }}
        onTouchEnd={e => {
          if (e.nativeEvent.changedTouches[0].clientX - touchStart > 100) {
            dispatch('prev');
            setSlide(prev => (prev === 0 ? order.length - 3 : prev - 1));
            return;
          } else if (
            e.nativeEvent.changedTouches[0].clientX - touchStart <
            -100
          ) {
            dispatch('next');
            setSlide(prev => (prev === order.length - 3 ? 0 : prev + 1));
            return;
          }
        }}
      >
        {order.map((project, index) => (
          <Project key={index} {...project} />
        ))}
      </div>
      <div className="flex items-center gap-4 desktop-figma:min-w-[1135px] mobile:max-desktop-figma:w-full self-center">
        <button
          onClick={() => {
            dispatch('prev');
            setSlide(prev => (prev === 0 ? order.length - 3 : prev - 1));
          }}
          className="mobile:max-tablet:hidden"
        >
          <img src="src/assets/left_slide.svg" alt="" />
        </button>
        <div className="h-1 bg-[#3D425C] w-full">
          <div
            className="bg-[#ffffff] h-1  duration-1000"
            style={{ width: `${((slide + 1) / 3) * 100}%` }}
          />
        </div>
        <button
          onClick={() => {
            dispatch('next');
            setSlide(prev => (prev === order.length - 3 ? 0 : prev + 1));
          }}
          className="mobile:max-tablet:hidden"
        >
          <img src="src/assets/right_slide.svg" alt="" />
        </button>
      </div>
    </div>
  );
}
