/**
 * Copyright (c) 2025 TDCAT.CN
 */

import { router } from '@kit.ArkUI';
import logManager, { LogCategory, LogEventType } from '../common/logtext';

@Entry
@Component
struct WelcomePage {
  @State message: string = 'Hello World';
  @State time: number = 3 //定时器执行总时间
  timer: number = -1 //定时器句柄
  canGo: boolean = true;

  goHome(): void {
    if (this.canGo) {
      router.pushUrl({ url: 'pages/login' })
      this.canGo = false
    }
  }
  aboutToAppear(): void { //页面显示之前显示
    this.Log_WelcomeEvent('aboutToAppear');
    this.timer = setInterval(() => {

      if (this.time <= 1) {
        clearInterval(this.timer)
        //路由
        this.goHome()
      }
      this.time--
    }, 1000)
  }

  aboutToDisappear(): void {
    this.Log_WelcomeEvent('aboutToDisappear');
    //清除定时器
    clearInterval(this.timer)
  }

  build() {
    Stack({ alignContent: Alignment.TopEnd }) {

      Image($r('app.media.welcome'))

      Button("跳过" + this.time).onClick(() => {
        this.goHome()
      })
        .backgroundColor('#ccc')
        .fontColor('#fff')

    }
    .height('100%')
    .width('100%')

  }

  // 页面生命周期方法
  onPageShow(): void { // 页面显示时触发的生命周期方法
    this.Log_WelcomeEvent('onPageShow');
  }

  onPageHide(): void { // 页面隐藏时触发的生命周期方法
    this.Log_WelcomeEvent('onPageHide');
  }

  onBackPress(): void { // 页面返回按键按下时触发的生命周期方法
    this.Log_WelcomeEvent('onBackPress');
  }

  // 通用日志记录方法
  private Log_WelcomeEvent(eventName: string): void {
    // 根据事件名称选择合适的日志事件类型
    switch (eventName) {
      case 'aboutToAppear':
        logManager.info(LogCategory.SYSTEM, LogEventType.PAGE_APPEAR, 'WelcomePage');
        break;
      case 'aboutToDisappear':
        logManager.info(LogCategory.SYSTEM, LogEventType.PAGE_DISAPPEAR, 'WelcomePage');
        break;
      case 'onPageShow':
        logManager.info(LogCategory.SYSTEM, LogEventType.PAGE_SHOW, 'WelcomePage');
        break;
      case 'onPageHide':
        logManager.info(LogCategory.SYSTEM, LogEventType.PAGE_HIDE, 'WelcomePage');
        break;
      case 'onBackPress':
        logManager.info(LogCategory.SYSTEM, LogEventType.PAGE_BACK, 'WelcomePage');
        break;
      default:
        logManager.info(LogCategory.SYSTEM, LogEventType.SYSTEM_INFO, `WelcomePage: ${eventName}`);
    }
  }
}