import { Capacitor, registerPlugin } from '@capacitor/core'

export type AndroidDisplay = {
  id: number
  name: string
  width: number
  height: number
  refreshRate: number
  isCurrent: boolean
  isPresentation: boolean
}

type DualScreenNativePlugin = {
  getDisplays: () => Promise<{
    currentDisplayId: number
    displays: AndroidDisplay[]
  }>
  openTopDisplay: (options?: { displayId?: number }) => Promise<AndroidDisplay & {
    opened: boolean
  }>
  closeTopDisplay: () => Promise<void>
}

const nativePlugin = registerPlugin<DualScreenNativePlugin>('DualScreen')

export function hasNativeDualScreenBridge() {
  return Capacitor.isNativePlatform()
}

export function getNativeDisplays() {
  return nativePlugin.getDisplays()
}

export function openNativeTopDisplay(displayId?: number) {
  return nativePlugin.openTopDisplay(
    displayId === undefined ? undefined : { displayId },
  )
}

export function closeNativeTopDisplay() {
  return nativePlugin.closeTopDisplay()
}
