import { FC, useState } from 'react';
import { GSX_SERVICE_CALLED, GSX_SERVICE_FINISHED } from '../../constants';
import { LoadingState } from '../../types/general';
import { WASMDataF } from '../../types/WASMData';
import Profile from '../profile/Profile';
import SBEntryF from '../SBEntry/SBEntryF';
import StationEntryF from '../stationEntry/StationEntryF';
import Tabbar from '../tabbar/Tabbar';
import ZFWEntryF from '../zfwEntry/ZFWEntryF';

interface FreighterProps {
  WASMData: WASMDataF;
  username?: string;
}

const Freighter: FC<FreighterProps> = ({ WASMData, username }) => {
  const [selectedTab, setSelectedTab] = useState(0);
  const [loadingState, setLoadingState] = useState<LoadingState>('preview');

  const upper1 = (overrideState: LoadingState = loadingState) => {
    if (overrideState !== 'loaded') return Math.round(WASMData.targetPayload.upper1);

    return Math.round(WASMData.livePayload.upper1);
  };
  const upper2 = (overrideState: LoadingState = loadingState) => {
    if (overrideState !== 'loaded') return Math.round(WASMData.targetPayload.upper2);

    return Math.round(WASMData.livePayload.upper2);
  };
  const upper3 = (overrideState: LoadingState = loadingState) => {
    if (overrideState !== 'loaded') return Math.round(WASMData.targetPayload.upper3);

    return Math.round(WASMData.livePayload.upper3);
  };
  const upper4 = (overrideState: LoadingState = loadingState) => {
    if (overrideState !== 'loaded') return Math.round(WASMData.targetPayload.upper4);

    return Math.round(WASMData.livePayload.upper4);
  };
  const lower1 = () => {
    if (loadingState !== 'loaded' && !GSXActive()) return Math.round(WASMData.targetPayload.lowerForward);

    return Math.round(WASMData.livePayload.lowerForward);
  };
  const lower2 = () => {
    if (loadingState !== 'loaded' && !GSXActive()) return Math.round(WASMData.targetPayload.lowerRear);

    return Math.round(WASMData.livePayload.lowerRear);
  };
  const OEW = () => {
    if (loadingState !== 'loaded' && !GSXActive()) return Math.round(WASMData.targetPayload.empty);

    return Math.round(WASMData.livePayload.empty);
  };
  const crew = () => {
    if (loadingState !== 'loaded' && !GSXActive()) return Math.round(WASMData.targetPayload.crew);

    return Math.round(WASMData.livePayload.crew);
  };

  const GSXActive = () => {
    return (
      (WASMData.GSX.boardingState >= GSX_SERVICE_CALLED || WASMData.GSX.deboardingState >= GSX_SERVICE_CALLED) &&
      WASMData.GSX.deboardingState !== GSX_SERVICE_FINISHED
    );
  };

  const CGs = (): [string, boolean, string, boolean] => {
    if (loadingState !== 'loaded' && !GSXActive()) {
      return [
        WASMData.targetPayload.ZFWCG.toFixed(1),
        WASMData.targetPayload.ZFWCG < WASMData.limits.minCG || WASMData.targetPayload.ZFWCG > WASMData.limits.maxCG,
        WASMData.targetPayload.TOCG.toFixed(1),
        WASMData.targetPayload.TOCG < WASMData.limits.minCG || WASMData.targetPayload.TOCG > WASMData.limits.maxCG,
      ];
    }

    return [
      WASMData.livePayload.ZFWCG.toFixed(1),
      WASMData.livePayload.ZFWCG < WASMData.limits.minCG || WASMData.livePayload.ZFWCG > WASMData.limits.maxCG,
      WASMData.livePayload.TOCG.toFixed(1),
      WASMData.livePayload.TOCG < WASMData.limits.minCG || WASMData.livePayload.TOCG > WASMData.limits.maxCG,
    ];
  };

  return (
    <>
      <Profile
        type="PAX"
        isER={WASMData.userData.isER}
        upper1={`${upper1(GSXActive() ? 'loaded' : loadingState)}`}
        upper2={`${upper2(GSXActive() ? 'loaded' : loadingState)}`}
        upper3={`${upper3(GSXActive() ? 'loaded' : loadingState)}`}
        upper4={`${upper4(GSXActive() ? 'loaded' : loadingState)}`}
        lower1={`${lower1()}`}
        lower2={`${lower2()}`}
        OEW={`${OEW()}`}
        crew={`${crew()}`}
        unit={WASMData.userData.isImperial ? 'LBS' : 'KG'}
        inPreview={loadingState !== 'loaded' && !GSXActive()}
        CGs={CGs()}
      />
      <Tabbar
        tabs={
          username ? ['Simbrief', 'ZFW', 'Passengers & Cargo', 'Options'] : ['ZFW', 'Passengers & Cargo', 'Options']
        }
        selectedTab={selectedTab}
        setSelectedTab={setSelectedTab}
      />
      {username && selectedTab === 0 && (
        <SBEntryF
          WASMData={WASMData}
          loadingState={loadingState}
          username={username}
          setLoadingState={setLoadingState}
        />
      )}
      {((username && selectedTab === 1) || (!username && selectedTab === 0)) && (
        <ZFWEntryF WASMData={WASMData} loadingState={loadingState} setLoadingState={setLoadingState} />
      )}
      {((username && selectedTab === 2) || (!username && selectedTab === 1)) && (
        <StationEntryF WASMData={WASMData} loadingState={loadingState} setLoadingState={setLoadingState} />
      )}
    </>
  );
};

export default Freighter;
