import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { AppUtil } from '@pura/harmony-utils';
import { WXApi, WXEventHandler } from '../utils/wechat/WXApiEventHandlerImpl';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
    AppUtil.init(this.context);
    this.handleWeChatCallIfNeed(want);
  }

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

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    AppStorage.setOrCreate('windowStage', windowStage);
    windowStage.loadContent('pages/splash/SplashPage', (err) => {
      // this.registerFont();
      if (err.code) {
        return;
      }
    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
  }

  onDestroy(): void {
  }

  onForeground(): void {
    // Ability has brought to foreground
  }

  onBackground(): void {
    // Ability has back to background
  }

  private handleWeChatCallIfNeed(want: Want) {  //放在与onCreate同级
    WXApi.handleWant(want, WXEventHandler)
  }
}