// 导入日志管理器 - 仅用于获取版本号
import logManager, { LogCategory, LogEventType } from './logtext';

// 可选的主题颜色
export enum ThemeColor {
  BLUE = '#409eff',
  GREEN = '#67C23A',
  RED = '#F56C6C',
  ORANGE = '#E6A23C',
  PURPLE = '#909399'
}

// 可选的语言
export enum Language {
  CHINESE = '中文',
  ENGLISH = 'English'
}

// 用户信息模型
@Observed
export class UserInfoModel {
  avatar: string;
  name: string;
  email: string;

  constructor(avatar: string = 'avatar-placeholder.png', 
              name: string = '张三', 
              email: string = 'zhangsan@example.com') {
    this.avatar = avatar;
    this.name = name;
    this.email = email;
  }
}

// 版本日志项模型
export interface VersionLogItem {
  version: string;      // 版本号，例如 "1.0.0"
  date: string;         // 发布日期，例如 "2023-10-01"
  changes: string[];    // 更新内容列表
}

// 多语言文本集合
export class TextResources {
  // 通用
  title: string = '';

  // 首页
  homeTitle: string = '';
  classroomMonitor: string = '';
  comprehensiveData: string = '';

  // 上课页面
  classPageTitle: string = '';
  classData: string = '';
  teacher: string = '';
  classroom: string = '';

  // 设置页面
  settingsTitle: string = '';
  systemSettings: string = '';
  themeColor: string = '';
  languageText: string = '';
  notificationSettings: string = '';
  about: string = '';
  versionInfo: string = '';
  
  // 版本日志相关
  versionLog: string = '';
  versionHistory: string = '';
  latestVersion: string = '';
  noVersionInfo: string = '';

  // 颜色名称
  blue: string = '';
  green: string = '';
  red: string = '';
  orange: string = '';
  purple: string = '';

  // 底部导航
  home: string = '';
  class: string = '';
  settings: string = '';

  // 通知状态
  enabled: string = '';
  disabled: string = '';
}

// 设置项模型
export class SettingsModel {
  themeColor: ThemeColor = ThemeColor.BLUE;
  language: Language = Language.CHINESE;
  notificationEnabled: boolean = true;

  constructor(themeColor: ThemeColor = ThemeColor.BLUE,
    language: Language = Language.CHINESE,
    notificationEnabled: boolean = true) {
    this.themeColor = themeColor;
    this.language = language;
    this.notificationEnabled = notificationEnabled;
  }
}

// 中文文本资源
const chineseTexts: TextResources = new TextResources();
// 通用
chineseTexts.title = '智慧教室管理系统';
// 首页
chineseTexts.homeTitle = '首页';
chineseTexts.classroomMonitor = '教室监控';
chineseTexts.comprehensiveData = '综合上课数据';
// 上课页面
chineseTexts.classPageTitle = '上课';
chineseTexts.classData = '上课数据';
chineseTexts.teacher = '授课教师';
chineseTexts.classroom = '教室';
// 设置页面
chineseTexts.settingsTitle = '设置';
chineseTexts.systemSettings = '系统设置';
chineseTexts.themeColor = '主题颜色';
chineseTexts.languageText = '语言';
chineseTexts.notificationSettings = '通知设置';
chineseTexts.about = '关于';
chineseTexts.versionInfo = '版本信息';
// 版本日志相关
chineseTexts.versionLog = '版本日志';
chineseTexts.versionHistory = '版本历史';
chineseTexts.latestVersion = '最新版本';
chineseTexts.noVersionInfo = '没有版本信息';
// 颜色名称
chineseTexts.blue = '蓝色';
chineseTexts.green = '绿色';
chineseTexts.red = '红色';
chineseTexts.orange = '橙色';
chineseTexts.purple = '紫色';
// 底部导航
chineseTexts.home = '首页';
chineseTexts.class = '上课';
chineseTexts.settings = '设置';
// 通知状态
chineseTexts.enabled = '开启';
chineseTexts.disabled = '关闭';

// 英文文本资源
const englishTexts: TextResources = new TextResources();
// 通用
englishTexts.title = 'Smart Classroom Management System';
// 首页
englishTexts.homeTitle = 'Home';
englishTexts.classroomMonitor = 'Classroom Monitor';
englishTexts.comprehensiveData = 'Comprehensive Class Data';
// 上课页面
englishTexts.classPageTitle = 'Class';
englishTexts.classData = 'Class Data';
englishTexts.teacher = 'Teacher';
englishTexts.classroom = 'Classroom';
// 设置页面
englishTexts.settingsTitle = 'Settings';
englishTexts.systemSettings = 'System Settings';
englishTexts.themeColor = 'Theme Color';
englishTexts.languageText = 'Language';
englishTexts.notificationSettings = 'Notification Settings';
englishTexts.about = 'About';
englishTexts.versionInfo = 'Version Information';
// 版本日志相关
englishTexts.versionLog = 'Version Log';
englishTexts.versionHistory = 'Version History';
englishTexts.latestVersion = 'Latest Version';
englishTexts.noVersionInfo = 'No Version Information';
// 颜色名称
englishTexts.blue = 'Blue';
englishTexts.green = 'Green';
englishTexts.red = 'Red';
englishTexts.orange = 'Orange';
englishTexts.purple = 'Purple';
// 底部导航
englishTexts.home = 'Home';
englishTexts.class = 'Class';
englishTexts.settings = 'Settings';
// 通知状态
englishTexts.enabled = 'Enabled';
englishTexts.disabled = 'Disabled';

// 全局设置服务
export class SettingsService {
  private static instance: SettingsService;
  private settings: SettingsModel;
  // 添加用户信息存储
  private userInfo: UserInfoModel;
  // 语言变化的回调函数
  private languageChangeCallbacks: (() => void)[] = [];
  // 颜色
  private colorChangeCallbacks: (() => void)[] = [];
  // 添加通知状态回调
  private notificationChangeCallbacks: (() => void)[] = [];
  // 添加用户信息变化回调
  private userInfoChangeCallbacks: (() => void)[] = [];

  private constructor() {
    // 初始化默认设置
    this.settings = new SettingsModel();
    // 初始化默认用户信息
    this.userInfo = new UserInfoModel();
  }
  
  // 获取当前最新版本号 - 与版本日志同步
  public getCurrentVersion(): string {
    return logManager.getLatestVersion();
  }
  
  // 获取所有版本日志
  public getVersionLogs(): VersionLogItem[] {
    return logManager.getVersionLogs();
  }
  
  // 添加新版本日志
  public addVersionLog(versionLog: VersionLogItem): void {
    logManager.addVersionLog(versionLog);
  }

  // 单例模式获取实例
  public static getInstance(): SettingsService {
    if (!SettingsService.instance) {
      SettingsService.instance = new SettingsService();
    }
    return SettingsService.instance;
  }

  // 获取当前设置
  public getSettings(): SettingsModel {
    return this.settings;
  }
  
  // 获取用户信息
  public getUserInfo(): UserInfoModel {
    return this.userInfo;
  }
  
  // 更新用户邮箱
  public updateUserEmail(email: string): void {
    if (email && email.trim() !== '') {
      const oldEmail = this.userInfo.email;
      this.userInfo.email = email.trim();
      
      // 记录用户邮箱变更
      logManager.info(LogCategory.USER, LogEventType.DATA_UPDATE, `Email updated: ${email}`);
      logManager.debug(LogCategory.USER, LogEventType.SETTING_CHANGE, 'userEmail', oldEmail, email);
      
      this.notifyUserInfoChange();
    }
  }
  
  // 更新用户名称
  public updateUserName(name: string): void {
    if (name && name.trim() !== '') {
      this.userInfo.name = name.trim();
      this.notifyUserInfoChange();
    }
  }
  
  // 更新用户头像
  public updateUserAvatar(avatar: string): void {
    if (avatar && avatar.trim() !== '') {
      this.userInfo.avatar = avatar.trim();
      this.notifyUserInfoChange();
    }
  }
  
  // 注册用户信息变化的回调
  public registerUserInfoChangeCallback(callback: () => void): void {
    this.userInfoChangeCallbacks.push(callback);
  }
  
  // 通知用户信息变化
  private notifyUserInfoChange(): void {
    for (const callback of this.userInfoChangeCallbacks) {
      callback();
    }
  }

  //注册颜色更新回调函数
  public registerColorChangeCallback(callback: () => void): void {
    this.colorChangeCallbacks.push(callback);
  }

  // 更新主题颜色
  public setThemeColor(color: ThemeColor): void {
    const oldColor = this.settings.themeColor;
    this.settings.themeColor = color;
    
    // 记录主题颜色变更
    logManager.info(LogCategory.SETTINGS, LogEventType.THEME_CHANGE, color);
    logManager.debug(LogCategory.SETTINGS, LogEventType.SETTING_CHANGE, 'themeColor', oldColor, color);
    
    this.notifyColorChange();
  }

  // 颜色更新后回调
  private notifyColorChange(): void {
    for (const callback of this.colorChangeCallbacks) {
      callback();
    }
  }

  // 更新语言
  public setLanguage(language: Language): void {
    const oldLanguage = this.settings.language;
    this.settings.language = language;
    
    // 记录语言变更
    logManager.info(LogCategory.SETTINGS, LogEventType.LANGUAGE_CHANGE, language);
    logManager.debug(LogCategory.SETTINGS, LogEventType.SETTING_CHANGE, 'language', oldLanguage, language);
    
    // 更新日志管理器的语言设置
    logManager.setLanguage(language);
    
    // 通知所有注册的回调，语言已更改
    this.notifyLanguageChange();
  }

  // 通知语言变化
  private notifyLanguageChange(): void {
    for (const callback of this.languageChangeCallbacks) {
      callback();
    }
  }

  // 更新通知设置
  public setNotificationEnabled(enabled: boolean): void {
    const oldValue = this.settings.notificationEnabled;
    this.settings.notificationEnabled = enabled;
    
    // 记录通知设置变更
    logManager.info(LogCategory.SETTINGS, LogEventType.NOTIFICATION_CHANGE, enabled ? 'enabled' : 'disabled');
    logManager.debug(LogCategory.SETTINGS, LogEventType.SETTING_CHANGE, 'notificationEnabled', oldValue, enabled);
    
    // 通知所有注册的回调，通知设置已更改
    this.notifyNotificationChange();
  }

  // 注册通知设置变化的回调
  public registerNotificationChangeCallback(callback: () => void): void {
    this.notificationChangeCallbacks.push(callback);
  }

  // 通知通知设置变化
  private notifyNotificationChange(): void {
    for (const callback of this.notificationChangeCallbacks) {
      callback();
    }
  }

  // 注册语言变化的回调
  public registerLanguageChangeCallback(callback: () => void): void {
    this.languageChangeCallbacks.push(callback);
  }

  // 获取当前语言的文本资源
  public getTextResources(): TextResources {
    return this.settings.language === Language.CHINESE ? chineseTexts : englishTexts;
  }

  // 获取主题颜色的友好名称
  public getThemeColorName(): string {
    const texts = this.getTextResources();
    switch (this.settings.themeColor) {
      case ThemeColor.BLUE:
        return texts.blue;
      case ThemeColor.GREEN:
        return texts.green;
      case ThemeColor.RED:
        return texts.red;
      case ThemeColor.ORANGE:
        return texts.orange;
      case ThemeColor.PURPLE:
        return texts.purple;
      default:
        return texts.blue;
    }
  }

  // 获取通知设置的友好名称
  public getNotificationStatusName(): string {
    const texts = this.getTextResources();
    return this.settings.notificationEnabled ? texts.enabled : texts.disabled;
  }
}

// 导出默认实例
export default SettingsService.getInstance(); 