/**
 * 宠物领养数据管理工具类
 * 负责领养宠物数据的存储、筛选、搜索等管理
 */

class AdoptionManager {
	constructor() {
		this.storageKey = 'adoption_pets'
		
		// 宠物类型配置
		this.petTypes = {
			cat: {
				name: '猫咪',
				icon: '🐱',
				breeds: {
					'british-shorthair': '英国短毛猫',
					'american-shorthair': '美国短毛猫',
					'persian': '波斯猫',
					'ragdoll': '布偶猫',
					'siamese': '暹罗猫',
					'maine-coon': '缅因猫',
					'scottish-fold': '苏格兰折耳猫',
					'russian-blue': '俄罗斯蓝猫',
					'bengal': '孟加拉猫',
					'mixed': '混血猫',
					'unknown': '品种不明'
				}
			},
			dog: {
				name: '狗狗',
				icon: '🐶',
				breeds: {
					'golden-retriever': '金毛寻回犬',
					'labrador': '拉布拉多',
					'husky': '哈士奇',
					'german-shepherd': '德国牧羊犬',
					'poodle': '贵宾犬',
					'chihuahua': '吉娃娃',
					'bulldog': '斗牛犬',
					'shiba-inu': '柴犬',
					'corgi': '柯基',
					'border-collie': '边境牧羊犬',
					'mixed': '混血犬',
					'unknown': '品种不明'
				}
			},
			rabbit: {
				name: '兔子',
				icon: '🐰',
				breeds: {
					'holland-lop': '荷兰垂耳兔',
					'mini-lop': '迷你垂耳兔',
					'lionhead': '狮子头兔',
					'dutch': '荷兰兔',
					'angora': '安哥拉兔',
					'mixed': '混血兔',
					'unknown': '品种不明'
				}
			},
			other: {
				name: '其他',
				icon: '🐾',
				breeds: {
					'hamster': '仓鼠',
					'guinea-pig': '豚鼠',
					'bird': '鸟类',
					'turtle': '乌龟',
					'fish': '鱼类',
					'other': '其他'
				}
			}
		}
		
		// 地区数据（简化版三级联动）
		this.regions = {
			'beijing': {
				name: '北京市',
				cities: {
					'beijing': {
						name: '北京市',
						districts: {
							'chaoyang': '朝阳区',
							'haidian': '海淀区',
							'dongcheng': '东城区',
							'xicheng': '西城区',
							'fengtai': '丰台区',
							'shijingshan': '石景山区'
						}
					}
				}
			},
			'shanghai': {
				name: '上海市',
				cities: {
					'shanghai': {
						name: '上海市',
						districts: {
							'huangpu': '黄浦区',
							'xuhui': '徐汇区',
							'changning': '长宁区',
							'jingan': '静安区',
							'putuo': '普陀区',
							'hongkou': '虹口区'
						}
					}
				}
			},
			'guangdong': {
				name: '广东省',
				cities: {
					'guangzhou': {
						name: '广州市',
						districts: {
							'tianhe': '天河区',
							'yuexiu': '越秀区',
							'liwan': '荔湾区',
							'haizhu': '海珠区',
							'baiyun': '白云区',
							'panyu': '番禺区'
						}
					},
					'shenzhen': {
						name: '深圳市',
						districts: {
							'futian': '福田区',
							'luohu': '罗湖区',
							'nanshan': '南山区',
							'yantian': '盐田区',
							'baoan': '宝安区',
							'longgang': '龙岗区'
						}
					}
				}
			},
			'jiangsu': {
				name: '江苏省',
				cities: {
					'nanjing': {
						name: '南京市',
						districts: {
							'xuanwu': '玄武区',
							'qinhuai': '秦淮区',
							'jianye': '建邺区',
							'gulou': '鼓楼区',
							'pukou': '浦口区',
							'qixia': '栖霞区'
						}
					},
					'suzhou': {
						name: '苏州市',
						districts: {
							'gusu': '姑苏区',
							'wuzhong': '吴中区',
							'xiangcheng': '相城区',
							'kunshan': '昆山市',
							'changshu': '常熟市',
							'zhangjiagang': '张家港市'
						}
					}
				}
			}
		}
		
		// 领养状态
		this.adoptionStatus = {
			available: { name: '可领养', color: '#4CAF50', icon: '✅' },
			reserved: { name: '已预约', color: '#FF9800', icon: '⏰' },
			adopted: { name: '已领养', color: '#9E9E9E', icon: '❤️' },
			pending: { name: '审核中', color: '#2196F3', icon: '📋' }
		}
	}

	/**
	 * 获取所有领养宠物数据
	 * @returns {Array} 领养宠物数组
	 */
	getAdoptionPets() {
		try {
			let pets = uni.getStorageSync(this.storageKey) || []
			
			// 如果没有数据，初始化一些测试数据
			if (pets.length === 0) {
				pets = this.initializeTestData()
				uni.setStorageSync(this.storageKey, pets)
			}
			
			return pets
		} catch (error) {
			console.error('获取领养宠物数据失败:', error)
			return this.initializeTestData()
		}
	}

	/**
	 * 初始化测试数据
	 * @returns {Array} 测试数据数组
	 */
	initializeTestData() {
		const testData = []

		// 猫咪数据
		testData.push({
			id: Date.now() + 1,
			name: '小橘',
			type: 'cat',
			breed: 'british-shorthair',
			age: 2,
			gender: 'male',
			photos: ['https://images.unsplash.com/photo-1574158622682-e40e69881006?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1592194996308-7b43878e84a6?w=400&h=300&fit=crop'],
			description: '小橘是一只非常温顺的英国短毛猫，性格亲人，喜欢和人互动。已经完成绝育手术和疫苗接种，身体健康。适合有爱心的家庭领养，希望能给它一个温暖的家。',
			personality: ['温顺', '亲人', '安静', '乖巧'],
			health: '健康良好，已绝育，疫苗齐全',
			location: {
				province: 'beijing',
				city: 'beijing',
				district: 'chaoyang',
				address: '朝阳区宠物救助中心'
			},
			status: 'available',
			requirements: [
				'有稳定收入',
				'有养猫经验',
				'家中无其他宠物',
				'同意定期回访'
			],
			contact: {
				name: '北京爱心救助站',
				phone: '138****1234',
				wechat: 'rescue_station_bj',
				type: 'organization'
			},
			publishTime: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString()
		})
		
		testData.push({
			id: Date.now() + 2,
			name: '小白',
			type: 'dog',
			breed: 'golden-retriever',
			age: 1,
			gender: 'female',
			photos: ['https://images.unsplash.com/photo-1552053831-71594a27632d?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1583337130417-3346a1be7dee?w=400&h=300&fit=crop'],
			description: '小白是一只活泼可爱的金毛幼犬，性格温和友善，非常聪明好训练。喜欢和小朋友玩耍，是很好的家庭伴侣犬。目前疫苗接种进行中，身体健康活泼。',
			personality: ['活泼', '聪明', '友善', '温和'],
			health: '健康良好，疫苗接种中',
			location: {
				province: 'shanghai',
				city: 'shanghai',
				district: 'xuhui',
				address: '徐汇区宠物医院'
			},
			status: 'available',
			requirements: [
				'有足够空间',
				'每天遛狗',
				'有耐心训练',
				'定期体检'
			],
			contact: {
				name: '张医生',
				phone: '139****5678',
				wechat: 'dr_zhang_sh',
				type: 'individual'
			},
			publishTime: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000).toISOString()
		})
		
		testData.push({
			id: Date.now() + 3,
			name: '花花',
			type: 'cat',
			breed: 'ragdoll',
			age: 3,
			gender: 'female',
			photos: ['https://images.unsplash.com/photo-1596854407944-bf87f6fdd49e?w=400&h=300&fit=crop'],
			description: '花花是一只美丽的布偶猫，拥有柔软的长毛和温和的性格。她非常优雅安静，适合喜欢安静环境的家庭。已经完成绝育手术，身体健康。',
			personality: ['温和', '优雅', '独立', '安静'],
			health: '健康良好，已绝育',
			location: {
				province: 'guangdong',
				city: 'guangzhou',
				district: 'tianhe',
				address: '天河区个人救助'
			},
			status: 'reserved',
			requirements: [
				'有养猫经验',
				'家庭环境稳定',
				'经济条件良好',
				'同意家访'
			],
			contact: {
				name: '李女士',
				phone: '137****9012',
				wechat: 'cat_lover_li',
				type: 'individual'
			},
			publishTime: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString()
		})
		
		testData.push({
			id: Date.now() + 4,
			name: '豆豆',
			type: 'rabbit',
			breed: 'holland-lop',
			age: 1,
			gender: 'male',
			photos: ['https://images.unsplash.com/photo-1585110396000-c9ffd4e4b308?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1589952283406-b53a7d1347e8?w=400&h=300&fit=crop'],
			description: '豆豆是一只可爱的荷兰垂耳兔，性格温顺亲人，很适合新手饲养。它喜欢安静的环境，也喜欢和人互动。身体健康，食欲良好。',
			personality: ['可爱', '亲人', '安静', '温顺'],
			health: '健康良好',
			location: {
				province: 'jiangsu',
				city: 'nanjing',
				district: 'xuanwu',
				address: '玄武区小动物救助中心'
			},
			status: 'available',
			requirements: [
				'了解兔子习性',
				'提供合适笼具',
				'定期清洁',
				'适当运动空间'
			],
			contact: {
				name: '南京小动物救助中心',
				phone: '025****3456',
				wechat: 'animal_rescue_nj',
				type: 'organization'
			},
			publishTime: new Date(Date.now() - 10 * 24 * 60 * 60 * 1000).toISOString()
		})
		
		testData.push({
			id: Date.now() + 5,
			name: '黑黑',
			type: 'cat',
			breed: 'mixed',
			age: 4,
			gender: 'male',
			photos: ['https://images.unsplash.com/photo-1561948955-570b270e7c36?w=400&h=300&fit=crop'],
			description: '黑黑是一只成年混血猫，性格稳重独立，已经完全社会化。它很适合有经验的猫奴，不需要太多关注但很忠诚。已完成绝育和疫苗。',
			personality: ['稳重', '独立', '温顺', '忠诚'],
			health: '健康良好，已绝育',
			location: {
				province: 'guangdong',
				city: 'shenzhen',
				district: 'nanshan',
				address: '南山区流浪动物救助站'
			},
			status: 'adopted',
			requirements: [
				'有养猫经验',
				'室内饲养',
				'定期体检',
				'终生负责'
			],
			contact: {
				name: '深圳流浪动物救助',
				phone: '0755****7890',
				wechat: 'sz_stray_rescue',
				type: 'organization'
			},
			publishTime: new Date(Date.now() - 15 * 24 * 60 * 60 * 1000).toISOString()
		})

		// 添加更多宠物数据
		testData.push({
			id: Date.now() + 6,
			name: '咪咪',
			type: 'cat',
			breed: 'siamese',
			age: 2,
			gender: 'female',
			photos: ['https://images.unsplash.com/photo-1513245543132-31f507417b26?w=400&h=300&fit=crop'],
			description: '咪咪是一只美丽的暹罗猫，拥有独特的蓝色眼睛和优雅的身姿。性格活泼好奇，喜欢探索新事物，也很喜欢和人交流。',
			personality: ['活泼', '好奇', '聪明', '粘人'],
			health: '健康良好，已绝育',
			location: {
				province: 'beijing',
				city: 'beijing',
				district: 'haidian',
				address: '海淀区宠物救助中心'
			},
			status: 'available',
			requirements: [
				'有养猫经验',
				'能提供足够陪伴',
				'室内饲养',
				'定期体检'
			],
			contact: {
				name: '海淀宠物救助中心',
				phone: '010****2468',
				wechat: 'haidian_pet_rescue',
				type: 'organization'
			},
			publishTime: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString()
		})

		testData.push({
			id: Date.now() + 7,
			name: '旺财',
			type: 'dog',
			breed: 'corgi',
			age: 3,
			gender: 'male',
			photos: ['https://images.unsplash.com/photo-1518717758536-85ae29035b6d?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1583337130417-3346a1be7dee?w=400&h=300&fit=crop'],
			description: '旺财是一只可爱的柯基犬，拥有短腿和大屁股的经典柯基特征。性格开朗活泼，很喜欢和人玩耍，是很好的家庭伴侣。',
			personality: ['开朗', '活泼', '友善', '忠诚'],
			health: '健康良好，已绝育',
			location: {
				province: 'jiangsu',
				city: 'suzhou',
				district: 'gusu',
				address: '姑苏区个人救助'
			},
			status: 'available',
			requirements: [
				'有养狗经验',
				'每天遛狗',
				'有足够空间',
				'耐心训练'
			],
			contact: {
				name: '王先生',
				phone: '151****7890',
				wechat: 'corgi_lover_wang',
				type: 'individual'
			},
			publishTime: new Date(Date.now() - 6 * 24 * 60 * 60 * 1000).toISOString()
		})

		testData.push({
			id: Date.now() + 8,
			name: '雪球',
			type: 'cat',
			breed: 'persian',
			age: 1,
			gender: 'female',
			photos: ['https://images.unsplash.com/photo-1571566882372-1598d88abd90?w=400&h=300&fit=crop'],
			description: '雪球是一只纯白色的波斯猫幼猫，毛发柔软蓬松，眼睛是漂亮的蓝色。性格温和安静，喜欢被人抚摸，是很好的陪伴猫咪。',
			personality: ['温和', '安静', '亲人', '乖巧'],
			health: '健康良好，疫苗接种中',
			location: {
				province: 'shanghai',
				city: 'shanghai',
				district: 'jingan',
				address: '静安区宠物医院'
			},
			status: 'available',
			requirements: [
				'有养长毛猫经验',
				'定期梳毛',
				'室内饲养',
				'定期美容'
			],
			contact: {
				name: '静安宠物医院',
				phone: '021****5678',
				wechat: 'jingan_pet_hospital',
				type: 'organization'
			},
			publishTime: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000).toISOString()
		})

		testData.push({
			id: Date.now() + 9,
			name: '小柴',
			type: 'dog',
			breed: 'shiba-inu',
			age: 2,
			gender: 'male',
			photos: ['https://images.unsplash.com/photo-1605568427561-40dd23c2acea?w=400&h=300&fit=crop'],
			description: '小柴是一只帅气的柴犬，拥有经典的柴犬笑容和独立的性格。它很聪明但有时候有点固执，需要有经验的主人来训练。',
			personality: ['独立', '聪明', '固执', '忠诚'],
			health: '健康良好，已绝育',
			location: {
				province: 'guangdong',
				city: 'guangzhou',
				district: 'yuexiu',
				address: '越秀区个人救助'
			},
			status: 'reserved',
			requirements: [
				'有养狗经验',
				'了解柴犬特性',
				'有耐心训练',
				'定期运动'
			],
			contact: {
				name: '陈女士',
				phone: '138****9012',
				wechat: 'shiba_lover_chen',
				type: 'individual'
			},
			publishTime: new Date(Date.now() - 8 * 24 * 60 * 60 * 1000).toISOString()
		})

		testData.push({
			id: Date.now() + 10,
			name: '毛毛',
			type: 'rabbit',
			breed: 'angora',
			age: 2,
			gender: 'female',
			photos: ['https://images.unsplash.com/photo-1585110396000-c9ffd4e4b308?w=400&h=300&fit=crop'],
			description: '毛毛是一只安哥拉兔，拥有非常柔软蓬松的长毛。性格温顺安静，喜欢被人轻柔地抚摸。需要定期梳毛和护理。',
			personality: ['温顺', '安静', '乖巧', '敏感'],
			health: '健康良好',
			location: {
				province: 'jiangsu',
				city: 'nanjing',
				district: 'qinhuai',
				address: '秦淮区小动物救助'
			},
			status: 'available',
			requirements: [
				'了解长毛兔护理',
				'定期梳毛',
				'安静环境',
				'耐心照料'
			],
			contact: {
				name: '秦淮动物救助',
				phone: '025****1357',
				wechat: 'qinhuai_animal_rescue',
				type: 'organization'
			},
			publishTime: new Date(Date.now() - 12 * 24 * 60 * 60 * 1000).toISOString()
		})

		return testData
	}

	/**
	 * 搜索宠物
	 * @param {string} keyword 关键词
	 * @param {Array} pets 宠物数组
	 * @returns {Array} 搜索结果
	 */
	searchPets(keyword, pets = null) {
		if (!pets) {
			pets = this.getAdoptionPets()
		}
		
		if (!keyword) return pets
		
		const lowerKeyword = keyword.toLowerCase()
		return pets.filter(pet => {
			return pet.name.toLowerCase().includes(lowerKeyword) ||
				   pet.description.toLowerCase().includes(lowerKeyword) ||
				   this.getPetTypeName(pet.type).includes(keyword) ||
				   this.getPetBreedName(pet.type, pet.breed).includes(keyword) ||
				   pet.personality.some(trait => trait.includes(keyword))
		})
	}

	/**
	 * 筛选宠物
	 * @param {Object} filters 筛选条件
	 * @param {Array} pets 宠物数组
	 * @returns {Array} 筛选结果
	 */
	filterPets(filters, pets = null) {
		if (!pets) {
			pets = this.getAdoptionPets()
		}
		
		return pets.filter(pet => {
			// 宠物类型筛选
			if (filters.type && pet.type !== filters.type) {
				return false
			}
			
			// 品种筛选
			if (filters.breed && pet.breed !== filters.breed) {
				return false
			}
			
			// 地区筛选
			if (filters.province && pet.location.province !== filters.province) {
				return false
			}
			
			if (filters.city && pet.location.city !== filters.city) {
				return false
			}
			
			if (filters.district && pet.location.district !== filters.district) {
				return false
			}
			
			// 状态筛选
			if (filters.status && pet.status !== filters.status) {
				return false
			}
			
			// 性别筛选
			if (filters.gender && pet.gender !== filters.gender) {
				return false
			}
			
			// 年龄筛选
			if (filters.ageRange) {
				const [minAge, maxAge] = filters.ageRange
				if (pet.age < minAge || pet.age > maxAge) {
					return false
				}
			}
			
			return true
		})
	}

	/**
	 * 获取宠物类型名称
	 * @param {string} type 类型代码
	 * @returns {string} 类型名称
	 */
	getPetTypeName(type) {
		return this.petTypes[type]?.name || '未知类型'
	}

	/**
	 * 获取宠物品种名称
	 * @param {string} type 类型代码
	 * @param {string} breed 品种代码
	 * @returns {string} 品种名称
	 */
	getPetBreedName(type, breed) {
		return this.petTypes[type]?.breeds[breed] || '未知品种'
	}

	/**
	 * 获取地区名称
	 * @param {string} province 省份代码
	 * @param {string} city 城市代码
	 * @param {string} district 区县代码
	 * @returns {string} 地区名称
	 */
	getLocationName(province, city = null, district = null) {
		let locationName = this.regions[province]?.name || province
		
		if (city) {
			const cityName = this.regions[province]?.cities[city]?.name
			if (cityName) {
				locationName += ' ' + cityName
			}
		}
		
		if (district) {
			const districtName = this.regions[province]?.cities[city]?.districts[district]
			if (districtName) {
				locationName += ' ' + districtName
			}
		}
		
		return locationName
	}

	/**
	 * 获取状态信息
	 * @param {string} status 状态代码
	 * @returns {Object} 状态信息
	 */
	getStatusInfo(status) {
		return this.adoptionStatus[status] || {
			name: '未知状态',
			color: '#999999',
			icon: '❓'
		}
	}


}

export default new AdoptionManager()
