import React, { useEffect, useState } from 'react';
import './App.css';
import { ContextWindowHeightOrientation } from './components/contextWindowHeight';
import { MainScreen } from './components/mainScreen/mainScreen';

function App() {
  // const [windowHeight, setWindowHeight] = useState<number>(window.screen.availHeight); - не робит на iphone
  const [windowHeight, setWindowHeight] = useState<number>(window.visualViewport.height);
  const [isLandscape, setIsLandscape] = useState<boolean>(window.orientation !== 0);
  const [currentOrientaton, setCurrentOrientaton] = useState<number>(window.orientation);
  

  window.visualViewport.onresize = (e) => {
    //@ts-ignore
    setWindowHeight(e.currentTarget.height);
  }
 
  // !!! работает на свойстве помеченном как deprecated - на айфоне иначе хз как
  window.onorientationchange = e => {
    //@ts-ignore
    let angle = e.target.orientation;
    if(currentOrientaton !== angle) {
      setCurrentOrientaton(angle);
    }
    if(angle !== 0) {
      setIsLandscape(true);
    } else {
      setIsLandscape(false);
    }
  }

  useEffect(() => {
    if(!navigator.userAgent.includes('iPhone')) {
      return;
    }
    if(currentOrientaton === 90) {
      document.querySelector('body').style['paddingLeft'] = 'env(safe-area-inset-left)'
    } else if(currentOrientaton === -90) {
      document.querySelector('body').style['paddingLeft'] = '0'
    }
  }, [currentOrientaton]);

  return (
    <div className="App">
      <ContextWindowHeightOrientation.Provider value={{windowHeight: windowHeight, isLandscape: isLandscape}}>
        <MainScreen />
      </ContextWindowHeightOrientation.Provider>
    </div>
  );
}

export default App;
