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

@Entry
@Component
struct ClassPage {
  @State currentClassInfo: ClassInfo = new ClassInfo('数学', '张老师', 'A101');
  @State classData: ClassDataType = {
    name: '',
    time: '',
    content: '',
    status: false
  }; // Initialize with empty values
  
  // 从设置服务获取设置
  @State settings: SettingsModel = settingsService.getSettings();
  // 获取文本资源
  @State texts: TextResources = settingsService.getTextResources();

  aboutToAppear() {
    // 注册语言变化的回调
    settingsService.registerLanguageChangeCallback(() => {
      this.texts = settingsService.getTextResources();
    });
    
    // 注册主题颜色变化的回调
    settingsService.registerColorChangeCallback(() => {
      this.settings = settingsService.getSettings();
    });
    
    this.Log_Event('aboutToAppear');
  }

  build() {
    Column() {
      // 顶部导航栏
      Row() {
        Text(this.texts.classPageTitle).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() {
            Text(this.currentClassInfo.name).fontSize(24).fontWeight(FontWeight.Bold).margin({ bottom: 10 })
            Text(`${this.texts.teacher}: ${this.currentClassInfo.teacher}`).fontSize(16).fontColor('#666').margin({ bottom: 8 })
            Text(`${this.texts.classroom}: ${this.currentClassInfo.room}`).fontSize(16).fontColor('#666')
          }
          .width('100%')
          .backgroundColor(Color.White)
          .borderRadius(12)
          .padding(20)
          .shadow({ radius: 6, color: '#eeeeee' })
          .margin({ top: 15, bottom: 15 })

          // 上课数据卡片
          Card({ 
            title: this.texts.classData, 
            data: this.classData,
            themeColor: this.settings.themeColor
          })
        }
        .width('100%')
        .padding({ left: 16, right: 16 })
      }
      .layoutWeight(1)
      .scrollBar(BarState.Off)

      // 底部导航栏
      BottomNavigation({
        activePage: 'class',
        themeColor: this.settings.themeColor,
        texts: this.texts
      })
    }
    .width('100%')
    .height('100%')
  }

  // 页面生命周期方法
  onPageShow(): void {
    this.Log_Event('onPageShow');
  }

  onPageHide(): void {
    this.Log_Event('onPageHide');
  }

  onBackPress(): void {
    this.Log_Event('onBackPress');
  }

  aboutToDisappear(): void {
    this.Log_Event('aboutToDisappear');
  }

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

// Define a class for currentClassInfo
class ClassInfo {
  name: string;
  teacher: string;
  room: string;

  constructor(name: string, teacher: string, room: string) {
    this.name = name;
    this.teacher = teacher;
    this.room = room;
  }
}

// Define proper interfaces instead of using any
interface ClassDataType {
  name: string;
  time: string;
  content: string;
  status: boolean;
}

// 卡片组件
@Component
struct Card {
  title: string = '';
  data: ClassDataType | MonitorDataType = {} as ClassDataType;
  themeColor: string = '#409eff';

  build() {
    Column() {
      // 卡片标题
      Row() {
        Text(this.title)
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
          .fontColor('#333')
      }
      .width('100%')
      .padding({ top: 8, bottom: 16 })

      // 图表内容
      Column() {
        Text('图表内容')
          .fontSize(16)
          .fontColor('#999')
          .height(150)
          .width('100%')
          .textAlign(TextAlign.Center)
      }
      .backgroundColor('#f5f5f5')
      .borderRadius(8)
      .width('100%')
    }
    .width('100%')
    .backgroundColor(Color.White)
    .borderRadius(12)
    .padding(16)
    .shadow({ radius: 6, color: '#eeeeee' })
  }
}

// 底部导航栏组件
@Component
struct BottomNavigation {
  @Prop activePage: string = 'class';
  @Prop themeColor: string;
  @Prop texts: TextResources = {} as TextResources;
  
  build() {
    Row() {
      // 首页按钮
      Column() {
        Image($r('app.media.home'))
          .width(24)
          .height(24)
          .fillColor(this.activePage === 'home' ? this.themeColor : '#999')
        Text(this.texts.home)
          .fontSize(12)
          .fontColor(this.activePage === 'home' ? this.themeColor : '#999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      .onClick(() => {
        if (this.activePage !== 'home') {
          router.replaceUrl({
            url: 'pages/HomePage',
            params: {
              direction: 'left' // 从上课页向左切换到首页
            }
          });
        }
      })
      
      // 上课按钮
      Column() {
        Image($r('app.media.class'))
          .width(24)
          .height(24)
          .fillColor(this.activePage === 'class' ? this.themeColor : '#999')
        Text(this.texts.class)
          .fontSize(12)
          .fontColor(this.activePage === 'class' ? this.themeColor : '#999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      .onClick(() => {
        if (this.activePage !== 'class') {
          router.replaceUrl({
            url: 'pages/ClassPage'
          });
        }
      })
      
      // 设置按钮
      Column() {
        Image($r('app.media.settings'))
          .width(24)
          .height(24)
          .fillColor(this.activePage === 'settings' ? this.themeColor : '#999')
        Text(this.texts.settings)
          .fontSize(12)
          .fontColor(this.activePage === 'settings' ? this.themeColor : '#999')
          .margin({ top: 4 })
      }
      .layoutWeight(1)
      .onClick(() => {
        if (this.activePage !== 'settings') {
          router.replaceUrl({
            url: 'pages/SettingsPage',
            params: {
              direction: 'right' // 从上课页向右切换到设置页
            }
          });
        }
      })
    }
    .width('100%')
    .height(60)
    .backgroundColor(Color.White)
    .border({ color: '#eeeeee', width: 1, style: BorderStyle.Solid })
  }
}


