/**
 * 小程序版本配置文件
 * 用于控制不同版本显示不同内容
 *
 * 版本号格式：主版本.次版本.修订版本
 * 示例：'1.0.0', '2.0.0'
 *
 * 使用方式：
 * - 修改 CURRENT_VERSION 来切换当前开发版本
 * - 在 pages 中使用 getVersion() 获取当前版本
 * - 使用 getContent(version) 获取对应版本的内容配置
 */

const CURRENT_VERSION = '1.0.0'

const VERSION_CONFIG = {
  '1.0.0': {
    name: 'V1基础版',
    description: '初始版本',
    categoryList: [
      {
        id: 1,
        name: '兴趣搭子',
        icon: '/images/icon-interest.png',
        url: '/pages/interest-partner/interest-partner'
      },
      {
        id: 2,
        name: '同城活动',
        icon: '/images/icon-city.png',
        url: '/pages/city-activities/city-activities'
      },
      {
        id: 3,
        name: '户外郊游',
        icon: '/images/icon-outdoor.png',
        url: '/pages/outdoor-activities/outdoor-activities'
      },
      {
        id: 4,
        name: '高端定制',
        icon: '/images/icon-travel.png',
        url: '/pages/theme-travel/theme-travel'
      },
      {
        id: 5,
        name: '快乐学堂',
        icon: '/images/icon-checkin.png',
        url: '/pages/happy-school/happy-school'
      },
      {
        id: 6,
        name: '单身聚会',
        icon: '/images/icon-love.png',
        url: '/pages/singles-party/singles-party'
      }
    ],
    features: {
      showNewFeature: false,
      showBetaFeature: false,
      useNewUI: false,
      useOldUI: true
    }
  },

  '2.0.0': {
    name: 'V2升级版',
    description: '全新UI设计，新增功能入口',
    categoryList: [
      {
        id: 1,
        name: '兴趣搭子',
        icon: '/images/icon-interest.png',
        url: '/pages/interest-partner/interest-partner'
      },
      {
        id: 2,
        name: '同城活动',
        icon: '/images/icon-city.png',
        url: '/pages/city-activities/city-activities'
      },
      {
        id: 3,
        name: '户外郊游',
        icon: '/images/icon-outdoor.png',
        url: '/pages/outdoor-activities/outdoor-activities'
      },
      {
        id: 4,
        name: '主题旅行',
        icon: '/images/icon-travel.png',
        url: '/pages/theme-travel/theme-travel'
      },
      {
        id: 5,
        name: '快乐学堂',
        icon: '/images/icon-checkin.png',
        url: '/pages/happy-school/happy-school'
      },
      {
        id: 6,
        name: '爱心传递',
        icon: '/images/icon-love.png',
        url: '/pages/love-transmission/love-transmission'
      }
    ],
    features: {
      showNewFeature: true,
      showBetaFeature: true,
      useNewUI: true,
      useOldUI: false
    }
  }
}

function getVersion() {
  const debugVersion = wx.getStorageSync('debugVersion')
  if (debugVersion && VERSION_CONFIG[debugVersion]) {
    return debugVersion
  }
  return CURRENT_VERSION
}

function getVersionInfo(version = CURRENT_VERSION) {
  return VERSION_CONFIG[version] || VERSION_CONFIG['1.0.0']
}

function getCategoryList(version = CURRENT_VERSION) {
  const config = getVersionInfo(version)
  return config ? config.categoryList : []
}

function getFeatureFlag(featureName, version = CURRENT_VERSION) {
  const config = getVersionInfo(version)
  return config ? config.features[featureName] : false
}

function compareVersion(v1, v2) {
  const v1s = v1.split('.')
  const v2s = v2.split('.')
  const len = Math.max(v1s.length, v2s.length)
  for (let i = 0; i < len; i++) {
    const n1 = parseInt(v1s[i]) || 0
    const n2 = parseInt(v2s[i]) || 0
    if (n1 > n2) return 1
    if (n1 < n2) return -1
  }
  return 0
}

function isVersionOrAbove(targetVersion, currentVersion = CURRENT_VERSION) {
  return compareVersion(currentVersion, targetVersion) >= 0
}

function isVersionBelow(targetVersion, currentVersion = CURRENT_VERSION) {
  return compareVersion(currentVersion, targetVersion) < 0
}

function getAllVersions() {
  return Object.keys(VERSION_CONFIG)
}

module.exports = {
  CURRENT_VERSION,
  VERSION_CONFIG,
  getVersion,
  getVersionInfo,
  getCategoryList,
  getFeatureFlag,
  compareVersion,
  isVersionOrAbove,
  isVersionBelow,
  getAllVersions
}
