import window from '@ohos.window';
import { AbilityConstant, Want } from '@kit.AbilityKit';
import { SetToGlobalThis } from 'tuanjieLib';
import { TuanjiePlayerAbilityBase } from 'tuanjieLib';
import { LogUtil, WindowVersionCompat } from 'fastsdk';
import { SdkBridge } from 'tuanjieLib/src/main/ets/SdkPlugin';

export default class TuanjiePlayerAbility extends TuanjiePlayerAbilityBase {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.setConfig();
    super.onCreate(want, launchParam);
    SdkBridge.onCreate(want, launchParam, this.context);
  }

  onDestroy(): void {
    super.onDestroy();
    SdkBridge.onDestory();
  }

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

  onWindowStageCreate(windowStage: window.WindowStage): void {
    super.setPageUri('pages/Index');
    super.onWindowStageCreate(windowStage);
    // 调用工具类获取UIContext（屏蔽版本差异）
    WindowVersionCompat.getInstance().getUIContextAfterLoadContent(
      windowStage,
      (uiContext: UIContext) => {
        SdkBridge.loadContentSuccess(windowStage);
        SdkBridge.initSDK();
      },
      (errorMsg: string) => {
        // 获取失败的兜底处理
        LogUtil.error(errorMsg);
      }
    );
  }

  onWindowStageDestroy(): void {
    super.onWindowStageDestroy();
  }

  onForeground(): void {
    super.onForeground();
  }

  onBackground(): void {
    super.onBackground();
  }

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