import './main.css'
import React, { useEffect, useState } from "react";
import { AnimationComponent } from "../animationComponent/animationComponent";
import { SliderComponent } from "../sliderComponent/sliderComponent";
import { CalendarDesktop } from "../calendarDesktop/calendarDesktop";
import { Multiplayer } from "../multiplayer/multiplayer";
import { Devices } from "../devices/devices";
import { Player } from "../player/player";
import { Analytics } from "../analytics/analytics";
import { CloudSolution } from "../cloudSolution/cloudSolution";
import { Demos } from "../demos/demos";
import { PhotoComponent } from "../photoComponent/photoComponent";
import { CalendarMobile } from "../calendarMobile/calendarMobile";
import moment, { Moment } from "moment";
import useScreenSize from 'use-screen-size'




export type TObjct = {
  calendar: boolean;
  timePicker: boolean;
  form: boolean;
  confirm: boolean;
};

export type TText = {
  text: object,
}

export const Main: React.FC<any> = ({ text, locale }) => {

  const { width } = useScreenSize()
  const [displayForm, setDisplayForm] = useState(true);
  const [time, setTime] = useState(moment());

  useEffect(() => {
    setTime(moment().locale(locale))
  }, [locale])


  const [next, setNext] = useState({
    calendar: true,
    timePicker: false,
    form: false,
    confirm: false,
  });
  const size = useScreenSize()
  const isMobile = size.width >= 900 ? false : true;


  useEffect(() => {
    if (isMobile) {
      setNext({
        calendar: true,
        timePicker: true,
        form: true,
        confirm: false,
      })
    }
  }, [width])



  function updateTime(value: Moment) {
    setTime(value);
  }
  function navigationHandler(value: TObjct) {
    setNext(value);
  }

  function onExit() {
    setNext({
      calendar: true,
      timePicker: false,
      form: false,
      confirm: false,
    })
    setDisplayForm(true)
  }
  return (
    <main className="main">
      <AnimationComponent text={text.title} />
      <SliderComponent text={text.slider} />
      <div className="calendar__mobile">
        <CalendarMobile
          locale={locale}
          text={text.calendar}
          setDisplayForm={setDisplayForm}
          displayForm={displayForm}
          setOpen={navigationHandler}
          open={next}
          time={time}
          onUpdate={updateTime}
          onExit={onExit}
          isMobile={isMobile}
        ></CalendarMobile>
      </div>
      <div className="calendar__desktop">
        <CalendarDesktop
          locale={locale}
          text={text.calendar}
          setDisplayForm={setDisplayForm}
          displayForm={displayForm}
          setOpen={navigationHandler}
          open={next}
          time={time}
          onUpdate={updateTime}
          onExit={onExit}
          isMobile={isMobile}
        ></CalendarDesktop>
      </div>
      <Multiplayer text={text.multiplayer} />
      <Devices text={text.devices} />
      <PhotoComponent text={text.photoComponent} />
      <Player text={text.player} />
      <Analytics text={text.analytics} />
      <CloudSolution text={text.cloudSolution} />
      <Demos text={text.demos} />
    </main>
  );
};
