import { COHERENT_COMM_BUS_WASM_CALL } from '../constants';
import { CallMode, CommBusEvent, SimBrief } from '../types/general';

export const loadAircraft = () => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.LOAD_SET,
    })
  );
};

export const unloadAircraft = () => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.UNLOAD_SET,
    })
  );
};

export const inRangeOf = (value: number, target: number, tolerance: number = 10) => {
  return Math.abs(value - target) < tolerance;
};

export const CoherentCallZFWEntry = (ZFWTarget: number, CGTarget: number) => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.ZFW_SET,
      ZFWTarget: ZFWTarget,
      CGTarget: CGTarget,
    })
  );
};

export const CoherentCallStationEntryPax = (
  business1: number,
  business2: number,
  economy1: number,
  economy2: number,
  forwardCargo: number,
  rearCargo: number
) => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.STATION_SET,
      business1,
      business2,
      economy1,
      economy2,
      forwardCargo,
      rearCargo,
    })
  );
};

export const CoherentCallStationEntryF = (
  upper1: number,
  upper2: number,
  upper3: number,
  upper4: number,
  lowerForward: number,
  lowerRear: number
) => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.STATION_SET,
      upper1,
      upper2,
      upper3,
      upper4,
      lowerForward,
      lowerRear,
    })
  );
};

export const CoherentCallSBEntryPax = (CGTarget: number, SBPlan?: SimBrief) => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.SB_SET,
      cargo: SBPlan?.cargo,
      numPax: SBPlan ? parseInt(SBPlan.pax as unknown as string) : undefined,
      CGTarget: CGTarget,
      plannedZFW: SBPlan?.plannedZFW,
      plannedGW: SBPlan?.plannedGW,
    })
  );
};

export const CoherentCallSBEntryF = (CGTarget: number, SBPlan?: SimBrief) => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.SB_SET,
      cargo: SBPlan?.cargo,
      CGTarget: CGTarget,
      plannedZFW: SBPlan?.plannedZFW,
      plannedGW: SBPlan?.plannedGW,
    })
  );
};

export const CoherentCallOptionsSet = (GSXSync?: boolean, paxWeight?: number, bagWeight?: number) => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.OPTIONS_SET,
      GSXSync,
      paxWeight,
      bagWeight,
    })
  );
};

export const CoherentCallGSXReset = () => {
  Coherent.call(
    COHERENT_COMM_BUS_WASM_CALL,
    CommBusEvent.UPDATE_TARGET,
    JSON.stringify({
      mode: CallMode.GSX_RESET,
    })
  );
};
