// pages/settingss/settingss.js
const app = getApp();

Page({
  data: {
    currentTheme: '浅色',
    notificationStatus: '已开启',
    currentLanguage: '中文',
    isDarkMode: false,
    // 初始化默认值，避免null导致的错误
    menuButtonInfo: {
      height: 32, // 默认高度
      width: 32,  // 默认宽度
      top: 0
    },
    statusBarHeight: 0,
    // 聊天设置数据
    chatSettings: {
      fontSize: 'medium',
      backgroundName: '默认背景',
      showPreview: true
    }
  },

  onLoad() {
    try {
      // 1. 获取系统信息（含状态栏高度）
      const systemInfo = wx.getSystemInfoSync();
      // 2. 获取胶囊按钮位置信息（用于导航栏对齐）
      const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
      
      // 初始化全局设置对象
      if (!app.globalData.settings) {
        app.globalData.settings = {};
      }
      
      // 更新数据
      this.setData({
        isDarkMode: systemInfo.theme === 'dark',
        statusBarHeight: systemInfo.statusBarHeight,
        menuButtonInfo: menuButtonInfo || this.data.menuButtonInfo,
        currentTheme: app.globalData.settings.theme || '浅色',
        notificationStatus: app.globalData.settings.notification || '已开启',
        currentLanguage: app.globalData.settings.language || '中文'
      });
    } catch (e) {
      console.error('获取系统信息失败:', e);
    }
  },
  
  // 返回上一页
  navigateBack() {
    wx.navigateBack();
  },
  
  // 切换主题
  toggleTheme(e) {
    const isDark = e.detail.value;
    const newTheme = isDark ? '深色' : '浅色';
    
    this.setData({
      currentTheme: newTheme,
      isDarkMode: isDark
    });
    
    app.globalData.settings.theme = newTheme;
    
    wx.showToast({
      title: `已切换到${newTheme}主题`,
      icon: 'success'
    });
  },

  // 跳转到账号安全
  openAccountSecurity() {
    wx.navigateTo({
      url: '/pages/settings/account-security/account-security'
    });
  },

  // 跳转到隐私设置
  openPrivacySettings() {
    wx.navigateTo({
      url: '/pages/settings/privacy/privacy'
    });
  },

  // 主题设置
  openThemeSettings() {
    const themes = ['浅色', '深色', '自动'];
    wx.showActionSheet({
      itemList: themes,
      success: (res) => {
        const selectedTheme = themes[res.tapIndex];
        this.setData({ currentTheme: selectedTheme });
        app.globalData.settings.theme = selectedTheme;
        wx.showToast({
          title: `已切换到${selectedTheme}主题`,
          icon: 'success'
        });
      }
    });
  },

  // 通知设置
  openNotificationSettings() {
    const options = ['已开启', '已关闭', '勿扰模式'];
    wx.showActionSheet({
      itemList: options,
      success: (res) => {
        const selectedStatus = options[res.tapIndex];
        this.setData({ notificationStatus: selectedStatus });
        app.globalData.settings.notification = selectedStatus;
        wx.showToast({
          title: `通知已${selectedStatus === '已开启' ? '开启' : selectedStatus === '已关闭' ? '关闭' : '设为勿扰'}`,
          icon: 'success'
        });
      }
    });
  },

  // 语言设置
  openLanguageSettings() {
    const languages = ['中文', 'English'];
    wx.showActionSheet({
      itemList: languages,
      success: (res) => {
        const selectedLanguage = languages[res.tapIndex];
        this.setData({ currentLanguage: selectedLanguage });
        app.globalData.settings.language = selectedLanguage;
        wx.showToast({
          title: `已切换到${selectedLanguage}`,
          icon: 'success'
        });
      }
    });
  },

  // 聊天设置
  openChatSettings() {
    const options = ['字体大小', '聊天背景', '信息预览'];
    wx.showActionSheet({
      itemList: options,
      success: (res) => {
        switch(res.tapIndex) {
          case 0: // 字体大小
            this.selectFontSize();
            break;
          case 1: // 聊天背景
            this.selectChatBackground();
            break;
          case 2: // 信息预览
            this.toggleMessagePreview();
            break;
        }
      }
    });
  },
  
  // 选择字体大小
  selectFontSize() {
    const fontSizeOptions = ['小', '中', '大'];
    wx.showActionSheet({
      itemList: fontSizeOptions,
      success: (res) => {
        const fontSize = ['small', 'medium', 'large'][res.tapIndex];
        const chatSettings = this.data.chatSettings;
        chatSettings.fontSize = fontSize;
        
        this.setData({
          chatSettings: chatSettings
        });
        
        // 保存设置
        wx.setStorageSync('chatSettings', this.data.chatSettings);
        
        wx.showToast({
          title: `字体大小已设为${fontSizeOptions[res.tapIndex]}`,
          icon: 'success'
        });
      }
    });
  },
  
  // 选择聊天背景
  selectChatBackground() {
    const options = ['默认背景', '渐变蓝', '渐变紫', '自然风光', '抽象艺术'];
    wx.showActionSheet({
      itemList: options,
      success: (res) => {
        const backgroundName = options[res.tapIndex];
        const chatSettings = this.data.chatSettings;
        chatSettings.backgroundName = backgroundName;
        
        this.setData({
          chatSettings: chatSettings
        });
        
        // 保存设置
        wx.setStorageSync('chatSettings', this.data.chatSettings);
        
        wx.showToast({
          title: `已切换到${backgroundName}`,
          icon: 'success'
        });
      }
    });
  },
  
  // 切换消息预览
  toggleMessagePreview() {
    const showPreview = !this.data.chatSettings.showPreview;
    const chatSettings = this.data.chatSettings;
    chatSettings.showPreview = showPreview;
    
    this.setData({
      chatSettings: chatSettings
    });
    
    // 保存设置
    wx.setStorageSync('chatSettings', this.data.chatSettings);
    
    wx.showToast({
      title: `消息预览已${showPreview ? '开启' : '关闭'}`,
      icon: 'success'
    });
  },

  // 意见反馈
  openFeedback() {
    wx.navigateTo({
      url: '/pages/settings/feedback/feedback'
    });
  },

  // 关于
  openAbout() {
    wx.navigateTo({
      url: '/pages/settings/about/about'
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '退出登录',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 模拟退出登录
          wx.showLoading({
            title: '退出中...',
          });
          
          // 模拟网络请求延迟
          setTimeout(() => {
            wx.hideLoading();
            wx.reLaunch({
              url: '/pages/login/login'
            });
          }, 1000);
        }
      }
    });
  }
});