import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { initApp } from '../common/App';

const DOMAIN = 0x0000;
let copyrightDisplayed = false;

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
    
    this.displayCopyright();
    
    // 初始化应用，注册路由
    initApp();
    
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
  }

  private displayCopyright(): void {
    if (copyrightDisplayed) return;
    
    const asciiArt = `
    ███████╗███╗   ███╗ █████╗ ██████╗ ███████╗██████╗     ██████╗ ██╗  ██╗
    ██╔════╝████╗ ████║██╔══██╗██╔══██╗██╔════╝██╔══██╗    ██╔══██╗██║ ██╔╝
    ███████╗██╔████╔██║███████║██████╔╝█████╗  ██████╔╝    ██████╔╝█████╔╝ 
    ╚════██║██║╚██╔╝██║██╔══██║██╔══██╗██╔══╝  ██╔══██╗    ██╔══██╗██╔═██╗ 
    ███████║██║ ╚═╝ ██║██║  ██║██║  ██║███████╗██║  ██║    ██║  ██║██║  ██╗
    ╚══════╝╚═╝     ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝    ╚═╝  ╚═╝╚═╝  ╚═╝
    `;
    
    console.info('==========================================');
    console.info(asciiArt);
    console.info('Copyright (c) 2025 TDCAT.CN');
    console.info('==========================================');
    
    hilog.info(DOMAIN, 'COPYRIGHT', '==========================================');
    hilog.info(DOMAIN, 'COPYRIGHT', 'Copyright (c) 2025 TDCAT.CN');
    hilog.info(DOMAIN, 'COPYRIGHT', '==========================================');
    
    hilog.info(DOMAIN, 'COPYRIGHT', '%{public}s', '==========================================');
    hilog.info(DOMAIN, 'COPYRIGHT', '%{public}s', 'Copyright (c) 2025 TDCAT.CN');
    hilog.info(DOMAIN, 'COPYRIGHT', '%{public}s', '==========================================');
    
    copyrightDisplayed = true;
  }

  onDestroy(): void {
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/WelcomePage', (err) => {
      if (err.code) {
        hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
        return;
      }
      hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
    });
  }

  onWindowStageDestroy(): void {
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
  }
}