import {MutableState} from '@polyvharmony/media-player-sdk';
import {isPortrait} from './PLVDisplayUtils';
import window from '@ohos.window';
import {PLVAbilityContexts} from './PLVAbilityContexts';

export class PLVOrientationManager {
  private static readonly instance = new PLVOrientationManager();

  private constructor() {
  }

  static getInstance(): PLVOrientationManager {
    return PLVOrientationManager.instance;
  }

  readonly isPortrait: MutableState<boolean> = new MutableState(isPortrait())
  requestOrientation(orientation: 'port' | 'land') {
    const context = PLVAbilityContexts.getInstance().lastContext()
    console.info('requestOrientation', orientation, context)
    const windowOrientation = orientation === 'port' ? window.Orientation.PORTRAIT : window.Orientation.LANDSCAPE
    if (!context) {
      console.info('No context found')
      return
    }
    window.getLastWindow(context).then((lastWindow) => {
      console.info('Got lastWindow', lastWindow)
      lastWindow.setPreferredOrientation(windowOrientation)
    })
  }
}