import window from '@ohos.window';
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { SetToGlobalThis, TuanjiePlayer, TuanjieLog, SdkBridge } from 'tuanjieLib';
export default class TuanjiePlayerAbility extends UIAbility {
  private defaultPageUri: string = "pages/Index"
  private tuanjiePlayer: TuanjiePlayer = TuanjiePlayer.getInstance()

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.setConfig();
    this.tuanjiePlayer.onCreate(this.context, want, launchParam)
    SdkBridge.onCreate(want, launchParam, this.context);
  }

  onDestroy(): void {
    this.tuanjiePlayer.onDestroy();
    SdkBridge.onDestory();
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    windowStage.loadContent(this.defaultPageUri, (err: ESObject, data: ESObject) => {
      if (err.code) {
        TuanjieLog.error('Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      TuanjieLog.info('Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
      SdkBridge.loadContentSuccess(windowStage);
      SdkBridge.initSDK();
    });
    this.tuanjiePlayer.onWindowStageCreate(windowStage)
  }

  onWindowStageDestroy(): void {
    this.tuanjiePlayer.onWindowStageDestroy()
  }

  onForeground(): void {
    this.tuanjiePlayer.onForeground()
  }

  onBackground(): void {
    this.tuanjiePlayer.onBackground()
  }

  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.tuanjiePlayer.onNewWant(want, launchParam)
    SdkBridge.onNewWant(want, launchParam);
  }

  setConfig(): void {
    SetToGlobalThis("staticSplashScreenFit", $r('app.integer.StaticSplashScreenFit'));
    SetToGlobalThis("appSplash", $r('app.media.app_splash'));
    SetToGlobalThis("showStaticSplash", $r('app.integer.ShowStaticSplashScreen'));
  }
}
