const app = getApp();
const apiClient = require('../../../utils/api-client.js');
const imageCacheManager = require('../../../utils/image-cache-manager.js');

Page({
  data: {
    userInfo: {
      avatar: '',
      nickname: '未设置昵称',
      customId: '123456789',
      signature: '',
      career: '',
      education: '',
      gender: '',
      birthday: '',
      hometown: '',
      constellation: '',
      height: '',
      personalityType: '',
      sleepHabit: '',
      socialActivity: ''
    },
    isEditingNickname: false,
    isEditingSignature: false,
    tempNickname: '',
    tempSignature: '',
    showConstellationPicker: false,
    showPersonalityPicker: false,
    showCareerPicker: false,
    showEducationPicker: false,
    showHometownPicker: false,
    showBirthdayPicker: false,
    showHeightPicker: false,
    showGenderPicker: false,
    showSleepHabitPicker: false,
    showSocialActivityPicker: false,
    constellations: ['水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座'],
    personalityTypes: ['INTJ', 'INTP', 'ENTJ', 'INFP', 'ENTP', 'INFJ', 'ENFP', 'ENFJ', 'ISTJ', 'ISFJ', 'ISTP', 'ISFP', 'ESTJ', 'ESFJ', 'ESTP', 'ESFP'],
    careers: ['初中生', '高中生', '大学生', '研究生', '留学生', '科研', '警察', '医生', '护士', '程序员', '老师', '化妆师', '摄影师', '音乐', '美术', '金融', '厨师', '工程师', '公务员', '互联网', '产品经理', '模特', '演员', '导演', '律师', '创业者', '其他'],
    educations: ['北京大学', '清华大学', '复旦大学', '上海交通大学', '浙江大学', '南京大学', '武汉大学', '中山大学', '四川大学', '哈尔滨工业大学', '大专', '中专', '高职', '高中'],
    genders: ['男', '女'],
    sleepHabits: ['早起鸟儿', '夜猫子', '规律型', '深度睡眠追求者', '碎片化睡眠者', '失眠困扰者', '咖啡因敏感型', '数字戒断者', '运动调节型', '挑战打卡型', '鼾声监测者', '生物钟调节者', '社区分享型'],
    socialActivities: ['内容创作者', '观察者', '吃瓜者', '潜水者', '机器人', '社群型用户', 'KOL', 'KOC', '普通用户', '算法依赖型用户', '事件驱动型用户', '季节性活跃用户', '社交维系型用户', '兴趣社群型用户', '职业网络型用户', '娱乐消遣型用户', '购物种草型用户', '互动型用户'],
    selectedConstellation: '',
    selectedPersonality: '',
    selectedCareer: '',
    selectedEducation: '',
    selectedGender: '',
    selectedHeight: 170,
    selectedSleepHabit: '',
    selectedSocialActivity: '',
    searchCareerText: '',
    searchEducationText: '',
    filteredCareers: [],
    filteredEducations: [],
    provinces: [],
    cities: [],
    selectedProvince: '',
    selectedCity: '',
    selectedYear: '',
    selectedMonth: '',
    selectedDay: '',
    years: [],
    months: [],
    days: []
  },

  onAvatarUpdated(payload = {}) {
    const displayUrl = payload.cachedUrl || payload.avatarUrl || payload.serverUrl || '';
    const latestProfile = payload.userInfo?.user || {};
    const baseProfile = this.data.userInfo || {};
    const mergedProfile = Object.assign({}, baseProfile, latestProfile);

    if (displayUrl) {
      mergedProfile.avatar = displayUrl;
      mergedProfile.avatarUrl = displayUrl;
    }

    const updates = {};
    if (displayUrl) {
      updates.avatar = displayUrl;
    }
    if (Object.keys(mergedProfile).length > 0) {
      updates.userInfo = mergedProfile;
    }

    if (Object.keys(updates).length > 0) {
      this.setData(updates);
    }
  },

  onLoad: function() {
    this.loadUserData();
    this.initDatePicker();
    this.initLocationData();
  },

  loadUserData: function() {
    const authInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {};
    const profile = authInfo.user || {};
    const normalizedProfile = {
      avatar: profile.avatar || '',
      nickname: profile.nickname || '未设置昵称',
      customId: profile.customId || (profile.id ? 'findme_' + profile.id : '123456789'),
      signature: profile.signature || '',
      career: profile.career || '',
      education: profile.education || '',
      gender: profile.gender || '',
      birthday: profile.birthday || '',
      hometown: profile.hometown || '',
      constellation: profile.constellation || '',
      height: profile.height || '',
      personalityType: profile.personalityType || '',
      sleepHabit: profile.sleepHabit || '',
      socialActivity: profile.socialActivity || ''
    };

    this.setData({
      userInfo: normalizedProfile,
      tempNickname: normalizedProfile.nickname,
      tempSignature: normalizedProfile.signature
    });
  },

  // 头像相关功能
  changeAvatar: function() {
    wx.showActionSheet({
      itemList: ['拍照', '从相册选择'],
      success: (res) => {
        const sourceType = res.tapIndex === 0 ? ['camera'] : ['album'];
        this.chooseImage(sourceType);
      }
    });
  },

  chooseImage: function(sourceType) {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: sourceType,
      success: (res) => {
        if (sourceType[0] === 'camera') {
          this.setData({
            tempAvatarPath: res.tempFilePaths[0],
            showCameraPreview: true
          });
        } else {
          this.uploadAvatar(res.tempFilePaths[0]);
        }
      }
    });
  },

  retakePhoto: function() {
    this.setData({ showCameraPreview: false });
    this.chooseImage(['camera']);
  },

  usePhoto: function() {
    this.uploadAvatar(this.data.tempAvatarPath);
    this.setData({ showCameraPreview: false });
  },

  uploadAvatar: async function(tempFilePath) {
    wx.showLoading({ title: '上传中...' });
    try {
      const uploadResult = await apiClient.uploadAvatar(tempFilePath);
      if (!uploadResult?.success) {
        throw new Error(uploadResult?.message || '上传失败');
      }
      const displayAvatar = uploadResult.cachedUrl || uploadResult.fileUrl;
      if (displayAvatar) {
        this.persistProfile({
          avatar: displayAvatar,
          avatarUrl: displayAvatar
        });
      }
      wx.showToast({ title: '头像更新成功', icon: 'success' });
    } catch (error) {
      console.error('上传头像失败:', error);
      wx.showToast({ title: error.message || '上传失败', icon: 'none' });
    } finally {
      wx.hideLoading();
    }
  },

  // 昵称编辑
  startEditNickname: function() {
    this.setData({
      isEditingNickname: true,
      tempNickname: this.data.userInfo.nickname
    });
  },

  confirmEditNickname: function() {
    if (this.data.tempNickname.length > 30) {
      wx.showToast({ title: '昵称不能超过30字节', icon: 'none' });
      return;
    }

    this.setData({
      isEditingNickname: false
    });
    this.persistProfile({ nickname: this.data.tempNickname });
  },

  cancelEditNickname: function() {
    this.setData({ isEditingNickname: false });
  },

  // 签名编辑
  startEditSignature: function() {
    this.setData({
      isEditingSignature: true,
      tempSignature: this.data.userInfo.signature
    });
  },

  confirmEditSignature: function() {
    if (this.data.tempSignature.length > 200) {
      wx.showToast({ title: '简介不能超过200字节', icon: 'none' });
      return;
    }

    this.setData({
      isEditingSignature: false
    });
    this.persistProfile({ signature: this.data.tempSignature });
  },

  cancelEditSignature: function() {
    this.setData({ isEditingSignature: false });
  },

  // 星座选择
  openConstellationPicker: function() {
    this.setData({
      showConstellationPicker: true,
      selectedConstellation: this.data.userInfo.constellation
    });
  },

  selectConstellation: function(e) {
    const constellation = e.currentTarget.dataset.value;
    this.setData({
      selectedConstellation: constellation
    });
  },

  confirmConstellation: function() {
    this.persistProfile({ constellation: this.data.selectedConstellation });
    this.setData({
      showConstellationPicker: false
    });
  },

  // 人格类型选择
  openPersonalityPicker: function() {
    this.setData({
      showPersonalityPicker: true,
      selectedPersonality: this.data.userInfo.personalityType
    });
  },

  selectPersonality: function(e) {
    const personality = e.currentTarget.dataset.value;
    this.setData({
      selectedPersonality: personality
    });
  },

  confirmPersonality: function() {
    this.persistProfile({ personalityType: this.data.selectedPersonality });
    this.setData({
      showPersonalityPicker: false
    });
  },

  // 职业选择
  openCareerPicker: function() {
    this.setData({
      showCareerPicker: true,
      searchCareerText: '',
      filteredCareers: this.data.careers
    });
  },

  searchCareer: function(e) {
    const text = e.detail.value;
    const filtered = this.data.careers.filter(career =>
      career.includes(text)
    );
    this.setData({
      searchCareerText: text,
      filteredCareers: filtered
    });
  },

  selectCareer: function(e) {
    const career = e.currentTarget.dataset.value;
    this.persistProfile({ career });
    this.setData({
      showCareerPicker: false
    });
  },

  // 教育背景选择
  openEducationPicker: function() {
    this.setData({
      showEducationPicker: true,
      searchEducationText: '',
      filteredEducations: this.data.educations
    });
  },

  searchEducation: function(e) {
    const text = e.detail.value;
    const filtered = this.data.educations.filter(edu =>
      edu.includes(text)
    );
    this.setData({
      searchEducationText: text,
      filteredEducations: filtered
    });
  },

  selectEducation: function(e) {
    const education = e.currentTarget.dataset.value;
    this.persistProfile({ education });
    this.setData({
      showEducationPicker: false
    });
  },

  // 家乡选择
  openHometownPicker: function() {
    this.setData({
      showHometownPicker: true,
      selectedProvince: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[0] : '',
      selectedCity: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[1] : ''
    });
  },

  confirmHometown: function() {
    this.persistProfile({
      hometown: `${this.data.selectedProvince} ${this.data.selectedCity}`
    });
    this.setData({
      showHometownPicker: false
    });
  },

  // 生日选择
  openBirthdayPicker: function() {
    this.setData({
      showBirthdayPicker: true
    });
  },

  confirmBirthday: function() {
    const formatted = `${this.data.selectedYear}-${this.data.selectedMonth.toString().padStart(2, '0')}-${this.data.selectedDay.toString().padStart(2, '0')}`;
    this.persistProfile({ birthday: formatted });
    this.setData({
      showBirthdayPicker: false
    });
  },

  // 身高选择
  openHeightPicker: function() {
    this.setData({
      showHeightPicker: true,
      selectedHeight: this.data.userInfo.height ? parseInt(this.data.userInfo.height) : 170
    });
  },

  // 性别选择
  openGenderPicker: function() {
    this.setData({
      showGenderPicker: true,
      selectedGender: this.data.userInfo.gender
    });
  },

  // 睡眠习惯选择
  openSleepHabitPicker: function() {
    this.setData({
      showSleepHabitPicker: true,
      selectedSleepHabit: this.data.userInfo.sleepHabit
    });
  },

  // 社交活跃度选择
  openSocialActivityPicker: function() {
    this.setData({
      showSocialActivityPicker: true,
      selectedSocialActivity: this.data.userInfo.socialActivity
    });
  },

  // 初始化位置数据
  initLocationData: function() {
    // 模拟省市数据
    this.setData({
      provinces: ['北京市', '上海市', '广东省', '江苏省', '浙江省'],
      cities: {
        '北京市': ['北京市'],
        '上海市': ['上海市'],
        '广东省': ['广州市', '深圳市', '珠海市'],
        '江苏省': ['南京市', '苏州市', '无锡市'],
        '浙江省': ['杭州市', '宁波市', '温州市']
      },
      selectedProvince: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[0] : '',
      selectedCity: this.data.userInfo.hometown ? this.data.userInfo.hometown.split(' ')[1] : '',
      hometownValue: [0, 0] // 默认选中第一项
    });
  },

  // 家乡选择变化处理
  onHometownChange: function(e) {
    const value = e.detail.value;
    const province = this.data.provinces[value[0]];
    const city = this.data.cities[province][value[1]];
    this.setData({
      selectedProvince: province,
      selectedCity: city,
      hometownValue: value
    });
  },

  initDatePicker: function() {
    const years = [];
    const currentYear = new Date().getFullYear();
    for (let i = currentYear; i >= 1950; i--) {
      years.push(i);
    }

    const months = [];
    for (let i = 1; i <= 12; i++) {
      months.push(i);
    }

    const days = [];
    for (let i = 1; i <= 31; i++) {
      days.push(i);
    }

    // 设置默认日期
    let defaultYear = currentYear - 20;
    let defaultMonth = 1;
    let defaultDay = 1;

    if (this.data.userInfo.birthday) {
      const parts = this.data.userInfo.birthday.split('-');
      if (parts.length === 3) {
        defaultYear = parseInt(parts[0]);
        defaultMonth = parseInt(parts[1]);
        defaultDay = parseInt(parts[2]);
      }
    }

    // 计算默认值的索引
    const yearIndex = years.indexOf(defaultYear);
    const monthIndex = months.indexOf(defaultMonth);
    const dayIndex = days.indexOf(defaultDay);

    this.setData({
      years: years,
      months: months,
      days: days,
      selectedYear: defaultYear,
      selectedMonth: defaultMonth,
      selectedDay: defaultDay,
      birthdayValue: [yearIndex, monthIndex, dayIndex]
    });
  },

  // 生日选择变化处理
  onBirthdayChange: function(e) {
    const value = e.detail.value;
    const year = this.data.years[value[0]];
    const month = this.data.months[value[1]];
    const day = this.data.days[value[2]];
    this.setData({
      selectedYear: year,
      selectedMonth: month,
      selectedDay: day,
      birthdayValue: value
    });
  },  bindProvinceChange: function(e) {
    const province = this.data.provinces[e.detail.value];
    this.setData({
      selectedProvince: province,
      selectedCity: this.data.cities[province][0]
    });
  },

  bindCityChange: function(e) {
    this.setData({
      selectedCity: this.data.cities[this.data.selectedProvince][e.detail.value]
    });
  },

  bindYearChange: function(e) {
    this.setData({
      selectedYear: this.data.years[e.detail.value]
    });
  },

  bindMonthChange: function(e) {
    this.setData({
      selectedMonth: this.data.months[e.detail.value]
    });
  },

  bindDayChange: function(e) {
    this.setData({
      selectedDay: this.data.days[e.detail.value]
    });
  },  adjustHeight: function(e) {
    this.setData({
      selectedHeight: e.detail.value
    });
  },

  // 睡眠习惯选择处理
  // 社交活跃度选择处理

  selectSleepHabit: function(e) {
    const sleepHabit = e.currentTarget.dataset.value;
    this.setData({
      showSleepHabitPicker: false
    });
    this.persistProfile({ sleepHabit });
  },

  selectSocialActivity: function(e) {
    const socialActivity = e.currentTarget.dataset.value;
    this.setData({
      showSocialActivityPicker: false
    });
    this.persistProfile({ socialActivity });
  },

  // 身高选择确认
  confirmHeight: function() {
    this.setData({
      showHeightPicker: false
    });
    this.persistProfile({ height: this.data.selectedHeight });
  },

  // 性别选择确认
  selectGender: function(e) {
    const gender = e.currentTarget.dataset.value;
    this.setData({
      showGenderPicker: false,
      showHeightGenderPicker: false
    });
    this.persistProfile({ gender });
  },

  // 关闭所有弹出层
  closeAllPickers: function() {
    this.setData({
      showConstellationPicker: false,
      showPersonalityPicker: false,
      showCareerPicker: false,
      showEducationPicker: false,
      showHometownPicker: false,
      showBirthdayPicker: false,
      showHeightPicker: false,
      showGenderPicker: false,
      showSleepHabitPicker: false,
      showSocialActivityPicker: false,
      showCameraPreview: false
    });
  },

  persistProfile(patch = {}) {
    const mergedProfile = Object.assign({}, this.data.userInfo || {}, patch);
    this.setData({ userInfo: mergedProfile });

    const authInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {};
    const updatedAuth = Object.assign({}, authInfo, {
      user: Object.assign({}, authInfo.user || {}, mergedProfile)
    });

    app.globalData.userInfo = updatedAuth;
    if (updatedAuth.token) {
      app.globalData.isLoggedIn = true;
    }
    wx.setStorageSync('userInfo', updatedAuth);
  },

  // 保存所有修改
  saveChanges: async function() {
    wx.showLoading({ title: '保存中...' });
    try {
      const profilePayload = Object.assign({}, this.data.userInfo);
      await apiClient.updateUserProfile(profilePayload);
      await apiClient.refreshUserInfoCache();
      wx.showToast({ title: '资料保存成功', icon: 'success' });
      wx.navigateBack();
    } catch (error) {
      console.error('保存资料失败:', error);
      wx.showToast({
        title: error.message || '保存失败，请稍后再试',
        icon: 'none'
      });
    } finally {
      wx.hideLoading();
    }
  }
});
