import { ref, computed } from 'vue'

// 城市数据类型
export interface City {
  name?: string
  pinyin?: string
  code?: string
  city?: string
  city_id?: number
}

// 搜索关键词
export const searchKeyword = ref('')

// 当前定位城市
export const currentCity = ref<City | null>(null)

// 滚动定位ID
export const scrollToId = ref('')

// 热门城市列表
export const hotCities = ref<City[]>([
  { name: '北京', pinyin: 'beijing', code: '110000' },
  { name: '上海', pinyin: 'shanghai', code: '310000' },
  { name: '广州', pinyin: 'guangzhou', code: '440100' },
  { name: '深圳', pinyin: 'shenzhen', code: '440300' },
  { name: '杭州', pinyin: 'hangzhou', code: '330100' },
  { name: '成都', pinyin: 'chengdu', code: '510100' },
  { name: '重庆', pinyin: 'chongqing', code: '500000' },
  { name: '武汉', pinyin: 'wuhan', code: '420100' }
])

//已开通城市
export const openCities = ref<City[]>([])


// 所有城市数据（示例数据，实际应从接口获取）
export const allCities = ref<City[]>([
  // A-G
  { name: '安庆', pinyin: 'anqing' },
  { name: '蚌埠', pinyin: 'bengbu' },
  { name: '包头', pinyin: 'baotou' },
  { name: '北京', pinyin: 'beijing', code: '110000' },
  { name: '常州', pinyin: 'changzhou' },
  { name: '成都', pinyin: 'chengdu', code: '510100' },
  { name: '重庆', pinyin: 'chongqing', code: '500000' },
  { name: '大连', pinyin: 'dalian' },
  { name: '东莞', pinyin: 'dongguan' },
  // F
  { name: '佛山', pinyin: 'foshan' },
  { name: '福州', pinyin: 'fuzhou' },
  // G
  { name: '广州', pinyin: 'guangzhou', code: '440100' },
  { name: '贵阳', pinyin: 'guiyang' },
  // H
  { name: '杭州', pinyin: 'hangzhou', code: '330100' },
  { name: '哈尔滨', pinyin: 'haerbin' },
  { name: '合肥', pinyin: 'hefei' },
  { name: '湖州', pinyin: 'huzhou' },
  // J
  { name: '嘉兴', pinyin: 'jiaxing' },
  { name: '金华', pinyin: 'jinhua' },
  { name: '济南', pinyin: 'jinan' },
  // K
  { name: '昆明', pinyin: 'kunming' },
  // L
  { name: '兰州', pinyin: 'lanzhou' },
  // N
  { name: '南昌', pinyin: 'nanchang' },
  { name: '南京', pinyin: 'nanjing' },
  { name: '南宁', pinyin: 'nanning' },
  { name: '宁波', pinyin: 'ningbo' },
  // Q
  { name: '青岛', pinyin: 'qingdao' },
  // S
  { name: '上海', pinyin: 'shanghai', code: '310000' },
  { name: '深圳', pinyin: 'shenzhen', code: '440300' },
  { name: '沈阳', pinyin: 'shenyang' },
  { name: '石家庄', pinyin: 'shijiazhuang' },
  { name: '苏州', pinyin: 'suzhou' },
  // T
  { name: '唐山', pinyin: 'tangshan' },
  { name: '天津', pinyin: 'tianjin' },
  // W
  { name: '潍坊', pinyin: 'weifang' },
  { name: '温州', pinyin: 'wenzhou' },
  { name: '武汉', pinyin: 'wuhan', code: '420100' },
  { name: '无锡', pinyin: 'wuxi' },
  // X
  { name: '厦门', pinyin: 'xiamen' },
  { name: '西安', pinyin: 'xian' },
  // Y
  { name: '烟台', pinyin: 'yantai' },
  { name: '扬州', pinyin: 'yangzhou' },
  { name: '宜昌', pinyin: 'yichang' },
  // Z
  { name: '郑州', pinyin: 'zhengzhou' },
  { name: '珠海', pinyin: 'zhuhai' }
])

// 搜索结果
export const searchResults = computed(() => {
  if (!searchKeyword.value) return []
  const keyword = searchKeyword.value.toLowerCase()
  return openCities.value.filter((city:any) => {
    return city.city?.includes(keyword)
  })
})

// 索引字母列表
export const indexLetters = computed(() => {
  return Object.keys(cityGroups.value).sort()
})

// 城市按首字母分组
export const cityGroups = computed(() => {
  const groups: Record<string, City[]> = {}
  allCities.value.forEach((city:any) => {
    const letter = getPinyinFirstLetter(city?.pinyin || city?.name)
    if (!groups[letter]) {
      groups[letter] = []
    }
    groups[letter].push(city)
  })
  return groups
})

// 获取拼音首字母（简化版，实际应使用拼音库）
const getPinyinFirstLetter = (str: string) => {
  const firstChar = str.charAt(0).toUpperCase()
  return firstChar
}

export default {
  searchKeyword,
  currentCity,
  scrollToId,
  hotCities,
  allCities,
  searchResults,
  indexLetters,
  cityGroups,
  openCities
}
