import { FC, useEffect, useState } from 'react';
import { GSX_SERVICE_CALLED, GSX_SERVICE_FINISHED } from '../../constants';
import { LoadingState } from '../../types/general';
import { WASMDataPax } from '../../types/WASMData';
import { CoherentCallOptionsSet } from '../../utils/utils';
import ToggleComponentKH from '../toggleComponent/ToggleComponent';

interface OptionsPaxProps {
  WASMData: WASMDataPax;
  loadingState: LoadingState;
}

const OptionsPax: FC<OptionsPaxProps> = ({ WASMData, loadingState }) => {
  const [paxWeight, setPaxWeight] = useState(WASMData.options.paxWeight);
  const [bagWeight, setBagWeight] = useState(WASMData.options.bagWeight);

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

  const updateData = () => {
    CoherentCallOptionsSet(undefined, paxWeight, bagWeight);
  };

  const handleInput = (input: string, maxValue: number, setter: (value: number) => void) => {
    if (!input) {
      setter(0);
      return;
    }

    const converted = parseInt(input);
    if (converted) {
      if (converted < 0) setter(0);
      else if (converted > maxValue) setter(maxValue);
      else setter(converted);
    }
  };

  useEffect(() => updateData(), [paxWeight, bagWeight]);

  return (
    <>
      <div className="block flex w-full flex-col opacity-100 transition-opacity duration-150 ease-linear mb-4">
        <div className="relative flex w-full items-center justify-between rounded-md bg-zinc-600 p-2 px-4">
          <ToggleComponentKH
            optionName="GSX Sync"
            value={WASMData.options.GSXSync}
            leftLabel={{ value: true }}
            rightLabel={{ value: false }}
            backgroundColor="bg-zinc-700"
            setValue={(value) => {
              CoherentCallOptionsSet(value);
            }}
            disabled={loadingState !== 'preview' || GSXActive()}
          />
        </div>
      </div>

      <div className="block flex w-full flex-col opacity-100 transition-opacity duration-150 ease-linear mb-4">
        <div className="relative flex w-full items-center justify-between rounded-t-md bg-zinc-600 p-2 px-4">
          <div className="flex w-full items-center justify-between text-xs">
            <label>Pax Weight ({WASMData.userData.isImperial ? 'lbs' : 'kg'})</label>
            <input
              type="text"
              placeholder=""
              className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
              value={paxWeight}
              onChange={(e) => handleInput(e.target.value, Number.MAX_VALUE, setPaxWeight)}
              disabled={loadingState !== 'preview' || GSXActive()}
            />
          </div>
        </div>
        <div className="relative flex w-full items-center justify-between bg-zinc-700 p-2 px-4">
          <div className="flex w-full items-center justify-between text-xs">
            <label>Bag Weight ({WASMData.userData.isImperial ? 'lbs' : 'kg'})</label>
            <input
              type="text"
              placeholder=""
              className="w-1/2 rounded-lg border border-white bg-zinc-700 px-3 py-2 text-right focus:border-blue-600 focus:ring-blue-600"
              value={bagWeight}
              onChange={(e) => handleInput(e.target.value, Number.MAX_VALUE, setBagWeight)}
              disabled={loadingState !== 'preview' || GSXActive()}
            />
          </div>
        </div>
      </div>
    </>
  );
};

export default OptionsPax;
