// Wails runtime bridge
// When running in Wails, window.go is injected.
// For dev (browser), we stub the calls.

const isWails = typeof window !== 'undefined' && window.go !== undefined

function wailsCall(path, ...args) {
  if (!isWails) {
    console.warn('[Wails stub]', path, args)
    return Promise.resolve(null)
  }
  // path like "main.App.GetSatelliteList"
  const parts = path.split('.')
  let fn = window.go
  for (const p of parts) {
    fn = fn[p]
    if (!fn) {
      console.error('[Wails] method not found:', path)
      return Promise.resolve(null)
    }
  }
  return fn(...args)
}

export const Go = {
  GetSatelliteList: () => wailsCall('main.App.GetSatelliteList'),
  GetPasses: (name, hours) => wailsCall('main.App.GetPasses', name, hours),
  GetCurrentPosition: (name) => wailsCall('main.App.GetCurrentPosition', name),
  SetObserverLocation: (lat, lon, alt) => wailsCall('main.App.SetObserverLocation', lat, lon, alt),
  SetSatelliteFrequencies: (down, up) => wailsCall('main.App.SetSatelliteFrequencies', down, up),
  StartTracking: (name) => wailsCall('main.App.StartTracking', name),
  StopTracking: () => wailsCall('main.App.StopTracking'),
  ConnectFlexRadio: (host, port) => wailsCall('main.App.ConnectFlexRadio', host, port),
  DisconnectFlexRadio: () => wailsCall('main.App.DisconnectFlexRadio'),
  ConnectRotor: (host, port) => wailsCall('main.App.ConnectRotor', host, port),
  DisconnectRotor: () => wailsCall('main.App.DisconnectRotor'),
  RefreshTLE: () => wailsCall('main.App.RefreshTLE'),
  GetFlexRadioStatus: () => wailsCall('main.App.GetFlexRadioStatus'),
  GetRotorStatus: () => wailsCall('main.App.GetRotorStatus'),
  GetTLEAge: () => wailsCall('main.App.GetTLEAge'),
  GetWatchlist: () => wailsCall('main.App.GetWatchlist'),
  SetWatchlist: (names) => wailsCall('main.App.SetWatchlist', names),
  GetGroundtrack: (name, minutes) => wailsCall('main.App.GetGroundtrack', name, minutes),
  GetSliceConfig: () => wailsCall('main.App.GetSliceConfig'),
  SetDopplerEnabled: (v) => wailsCall('main.App.SetDopplerEnabled', v),
  SetRotorEnabled: (v) => wailsCall('main.App.SetRotorEnabled', v),
  SetRotorAzOnly: (v) => wailsCall('main.App.SetRotorAzOnly', v),
  GetRotorEnabled: () => wailsCall('main.App.GetRotorEnabled'),
  GetDopplerEnabled: () => wailsCall('main.App.GetDopplerEnabled'),
  SetSliceConfig: (rx, tx) => wailsCall('main.App.SetSliceConfig', rx, tx),
  SetSatelliteMode: (mode) => wailsCall('main.App.SetSatelliteMode', mode),
  SetTrackFreqMode: (v) => wailsCall('main.App.SetTrackFreqMode', v),
  SetTrackAzimuth: (v) => wailsCall('main.App.SetTrackAzimuth', v),
  // SkyCAT / rigctld
  ConnectSkyCAT: (host, port) => wailsCall('main.App.ConnectSkyCAT', host, port),
  DisconnectSkyCAT: () => wailsCall('main.App.DisconnectSkyCAT'),
  GetSkyCATStatus: () => wailsCall('main.App.GetSkyCATStatus'),
  SetRadioType: (type) => wailsCall('main.App.SetRadioType', type),
  SetSkyCATMode: (mode) => wailsCall('main.App.SetSkyCATMode', mode),
}

// Wails event listener wrapper
export function onWailsEvent(event, callback) {
  if (isWails && window.runtime) {
    window.runtime.EventsOn(event, callback)
    return () => window.runtime.EventsOff(event)
  }
  return () => {}
}