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

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

@Entry
@Component
struct AccountPage {
  @State settings: SettingsModel = settingsService.getSettings();
  @State texts: TextResources = settingsService.getTextResources();
  @State userName: string = settingsService.getCurrentAccount() || "用户";
  @State userEmail: string = "未设置邮箱";

  aboutToAppear() {
    // 注册语言变化的回调
    settingsService.registerLanguageChangeCallback(() => {
      this.texts = settingsService.getTextResources();
    });
    
    // 获取用户名
    this.userName = settingsService.getCurrentAccount() || "用户";
    
    this.Log_Event('aboutToAppear');
  }

  build() {
    Column() {
      // 顶部导航栏
      Row() {
        Text("我的账户").fontSize(22).fontColor(Color.White).fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .backgroundColor(this.settings.themeColor)
      .height(60)
      .justifyContent(FlexAlign.Center)
      .padding({ left: 20 })

      // 页面内容区
      Scroll() {
        Column() {
          // 用户基本信息
          Column() {
            Image($r('app.media.tdcat'))
              .width(80)
              .height(80)
              .borderRadius(40)
              .margin({ top: 20, bottom: 10 })
            
            Text(this.userName)
              .fontSize(20)
              .fontWeight(FontWeight.Bold)
              .margin({ bottom: 8 })
            
            Text(this.userEmail)
              .fontSize(14)
              .fontColor('#666666')
              .margin({ bottom: 20 })
          }
          .width('100%')
          .alignItems(HorizontalAlign.Center)
          
          // 账户选项列表
          Column() {
            // 账户信息
            this.AccountListItem("账户信息", "查看和编辑您的个人资料")
            
            // 安全设置
            this.AccountListItem("安全设置", "修改密码和安全选项")
            
            // 通知管理
            this.AccountListItem("通知管理", "管理您收到的通知")
            
            // 隐私设置
            this.AccountListItem("隐私设置", "管理您的隐私选项")
            
            // 退出登录
            Button("退出登录")
              .width('90%')
              .height(50)
              .fontSize(16)
              .backgroundColor('#FF5A5A')
              .fontColor(Color.White)
              .margin({ top: 40, bottom: 20 })
              .onClick(() => {
                this.logout();
              })
          }
          .width('100%')
          .padding({ top: 20 })
          .backgroundColor('#FFFFFF')
          .borderRadius(16)
          .margin({ top: 20 })
          .alignItems(HorizontalAlign.Center)
        }
        .width('100%')
        .padding({ left: 16, right: 16 })
      }
      .layoutWeight(1)
      .scrollBar(BarState.Off)

      // 底部导航栏
      this.BottomNavigationBar('account')
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#F5F7FA')
  }
  
  @Builder
  AccountListItem(title: string, description: string) {
    Row() {
      Column() {
        Text(title)
          .fontSize(16)
          .fontWeight(FontWeight.Medium)
        
        Text(description)
          .fontSize(12)
          .fontColor('#999999')
          .margin({ top: 4 })
      }
      .alignItems(HorizontalAlign.Start)
      .layoutWeight(1)
      
      Image($r('app.media.arrow_right'))
        .width(20)
        .height(20)
        .margin({ right: 10 })
    }
    .width('90%')
    .padding({ top: 16, bottom: 16 })
    .borderRadius(8)
    .backgroundColor('#FFFFFF')
    .margin({ bottom: 12 })
  }
  
  @Builder
  BottomNavigationBar(activePage: string) {
    Row() {
      // 首页
      Column() {
        Image($r('app.media.home'))
          .width(24)
          .height(24)
          .fillColor(activePage === 'home' ? this.settings.themeColor : '#999999')
        Text("首页")
          .fontSize(12)
          .fontColor(activePage === 'home' ? this.settings.themeColor : '#999999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      .onClick(() => {
        router.pushUrl({ url: 'pages/HomePage' });
      })
      
      // 课程
      Column() {
        Image($r('app.media.class'))
          .width(24)
          .height(24)
          .fillColor(activePage === 'class' ? this.settings.themeColor : '#999999')
        Text("课程")
          .fontSize(12)
          .fontColor(activePage === 'class' ? this.settings.themeColor : '#999999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      .onClick(() => {
        router.pushUrl({ url: 'pages/ClassPage' });
      })
      
      // 账户
      Column() {
        Image($r('app.media.info'))
          .width(24)
          .height(24)
          .fillColor(activePage === 'account' ? this.settings.themeColor : '#999999')
        Text("账户")
          .fontSize(12)
          .fontColor(activePage === 'account' ? this.settings.themeColor : '#999999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      
      // 设置
      Column() {
        Image($r('app.media.settings'))
          .width(24)
          .height(24)
          .fillColor(activePage === 'settings' ? this.settings.themeColor : '#999999')
        Text("设置")
          .fontSize(12)
          .fontColor(activePage === 'settings' ? this.settings.themeColor : '#999999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      .onClick(() => {
        router.pushUrl({ url: 'pages/SettingsPage' });
      })
    }
    .width('100%')
    .height(60)
    .backgroundColor(Color.White)
    .padding({ top: 8, bottom: 8 })
    .justifyContent(FlexAlign.SpaceAround)
    .alignItems(VerticalAlign.Center)
  }
  
  // 退出登录方法
  private logout() {
    settingsService.setCurrentAccount(""); // Clear the account by setting it empty
    logManager.info(LogCategory.USER, LogEventType.SYSTEM_INFO, "User logged out");
    
    // 跳转到登录页
    router.replaceUrl({
      url: 'pages/login'
    });
  }
  
  // 日志记录方法
  private Log_Event(eventName: string): void {
    logManager.info(LogCategory.HOME, LogEventType.PAGE_APPEAR, `AccountPage: ${eventName}`);
  }
}

export { AccountPage } 