import { Component, OnInit } from '@angular/core';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';

@Component({
  selector: 'app-way',
  templateUrl: './way.page.html',
  styleUrls: ['./way.page.scss'],
  standalone:false,
})
export class WayPage implements OnInit {

  routes:CloudObject[]=[];
  birdTypes: CloudObject[] = [];
  isLoading: boolean = false;
  showCreateModal: boolean = false;
  selectedBird: any = null; // 存储当前选中的鸟类信息
  showBirdModal: boolean = false; // 控制鸟类详情模态框显示
  newRouteData: any = {
  title: '',
  description: '',
  routelength: null, // 数字类型
  difficulty: null,
  thumbnail: '',
  bestMonths: [],
  startPoint: '',
  endPoint: '',
  facilities: [],
  rating: 0,
  birdCount: 0,
  birds: [],       // BirdType 的 Pointer 数组
  featureTags: [], // 字符串数组
  region: null     // Region 的 Pointer 对象
};

  constructor() { }

  ngOnInit() {
    this.loadRoutes();
  }
  // 显示创建表单
  showCreateForm() {
    this.showCreateModal = true;
  }

  // 关闭模态框
  closeModal() {
    this.showCreateModal = false;
    this.resetForm();
  }
  // 重置表单
  resetForm() {
    this.newRouteData = {
      title: '',
      location: '',
      duration: '',
      description: '',
      thumbnail: '',
      birdCount: 0,
      rating: 0,
      birds: [],
      featureTags: []
    };
  }

  async confirmCreate() {
  if (!this.newRouteData.title) {
    alert('请填写标题');
    return;
  }

  try {
    // 创建鸟类指针数组
    const birdPointers = [];
    for (const birdId of this.newRouteData.birds) {
      const birdQuery = new CloudQuery("BirdType");
      birdQuery.equalTo("objectId", birdId);
      
      const bird = await birdQuery.first();
      if (bird) {
        birdPointers.push(bird.toPointer());
      }
    }

    const newRoute = new CloudObject("Route");
    newRoute.set({
      title: this.newRouteData.title,
      description: this.newRouteData.description || '',
      length: parseFloat(this.newRouteData.routelength) || 0,
      difficulty: parseInt(this.newRouteData.difficulty) || 0,
      thumbnail: this.newRouteData.thumbnail || 
               `https://picsum.photos/600/400?random=${Math.floor(Math.random() * 1000)}`,
      bestMonths: Array.isArray(this.newRouteData.bestMonths) ? this.newRouteData.bestMonths : [],
      startPoint: this.newRouteData.startPoint || '',
      endPoint: this.newRouteData.endPoint || '',
      facilities: Array.isArray(this.newRouteData.facilities) ? this.newRouteData.facilities : [],
      rating: parseFloat(this.newRouteData.rating) || 0,
      birdCount: birdPointers.length,
      birds: birdPointers,
      tags: this.newRouteData.featureTags || [],
      region: this.newRouteData.region || null
    });

    await newRoute.save();
    this.closeModal();
    await this.loadRoutes();
    console.log('路线创建成功');
  } catch (error) {
    console.error('创建路线失败', error);
    alert('创建路线失败，请重试');
  }
}

  // 删除路线
  async deleteRoute(route: CloudObject) {
    if (!confirm('确定要删除这条路线吗？')) {
      return;
    }

    try {
      await route.destroy();
      this.routes = this.routes.filter(r => r.id !== route.id);
      console.log('路线删除成功');
    } catch (error) {
      console.error('删除路线失败', error);
      alert('删除路线失败，请重试');
    }
  }

  // 在组件类中添加
routeDetailedBirdsMap = new Map<string, CloudObject[]>(); // 存储路线ID到鸟类数组的映射
// 添加获取路线鸟类的方法
  getBirdsForRoute(routeId: string): CloudObject[] {
    return this.routeDetailedBirdsMap.get(routeId) || [];
  }

async loadRoutes() {
  this.isLoading = true;
  try {
    const query = new CloudQuery("Route");
    query.include("birds"); // 如果birds是指针数组，可能需要这个
    
    const routes: CloudObject[] = await query.find();
    this.routeDetailedBirdsMap.clear(); // 清空旧数据
    
    // 并行获取所有路线的鸟类数据
    await Promise.all(routes.map(async route => {
      if (!route.id) return;

      const detailedBirds = [];
      const birdPointers = route.get('birds') || [];
      
      for (const birdPointer of birdPointers) {
        const birdQuery = new CloudQuery("BirdType");
        birdQuery.equalTo("objectId", birdPointer.objectId);
        const bird = await birdQuery.first();
        if (bird) detailedBirds.push(bird);
      }
      this.routeDetailedBirdsMap.set(route.id, detailedBirds);
     
    }));
    
    this.routes = routes;
  } catch (error) {
    console.error("加载路线数据失败", error);
  } finally {
    this.isLoading = false;
  }
}

getStars(rating: number): string[] {
    const fullStars = Math.floor(rating);
    const halfStar = rating % 1 >= 0.5 ? 1 : 0;
    const emptyStars = 5 - fullStars - halfStar;

    return [
      ...Array(fullStars).fill('★'),
      ...(halfStar ? ['½'] : []),
      ...Array(emptyStars).fill('☆')
    ];
  }

async importSampleRoutes() {
  const routeDataset = [
    {
      name: "鄱阳湖冬季候鸟观赏路线",
      description: "冬季观赏候鸟的最佳路线，可观察到多种珍稀候鸟",
      routelength: 8.5,
      difficulty: 3,
      estimatedTime: 3,
      bestMonths: ["12", "1", "2"],
      startPoint: "鄱阳湖东门入口",
      endPoint: "观鸟塔",
      rating: 4.5,
      facilities: ["观鸟塔", "休息区", "停车场"],
      routeImage: "https://picsum.photos/600/400?random=101",
      popularity: 85,
      region: {
        name: "鄱阳湖自然保护区",
        description: "中国最大的淡水湖，冬季候鸟重要栖息地",
        climate: "亚热带湿润气候",
        terrain: "湿地",
        areaSize: 4125,
        bestSeason: "冬季",
        featureTags: ["湿地", "候鸟", "自然保护区"],
        mapImage: "https://picsum.photos/600/400?random=201",
        popularity: 90
      },
      commonBirds: [
        { 
          name: "红嘴相思鸟"
        },
        { 
          name: "白鹭"
        }
      ]
    },
    {
      name: "庐山森林鸟类观察路线",
      description: "穿越庐山森林，观察多种留鸟和森林鸟类",
      routelength: 12.3,
      difficulty: 4,
      estimatedTime: 5,
      bestMonths: ["4", "5", "9", "10"],
      startPoint: "庐山植物园",
      endPoint: "五老峰",
      rating: 4.2,
      facilities: ["休息区", "观景台"],
      routeImage: "https://picsum.photos/600/400?random=102",
      popularity: 75,
      region: {
        name: "庐山国家公园",
        description: "世界文化遗产地，森林覆盖率高",
        climate: "山地气候",
        terrain: "山地",
        areaSize: 302,
        bestSeason: "春秋季",
        featureTags: ["森林", "山地", "世界遗产"],
        mapImage: "https://picsum.photos/600/400?random=202",
        popularity: 85
      },
      commonBirds: [
        { 
          name: "红腹锦鸡"
        },
        { 
          name: "黑脸琵鹭"
        }
      ]
    }
  ];

  for (const routeData of routeDataset) {
    try {
      // 检查是否已存在同名路线
      const routeQuery = new CloudQuery("Route");
      routeQuery.equalTo("title", routeData.name);
      const existingRoute = await routeQuery.first();

      if (existingRoute) {
        console.log(`路线"${routeData.name}"已存在，跳过保存`);
        continue;
      }

      // 创建或获取Region
      let regionPointer = null;
      if (routeData.region) {
        const regionQuery = new CloudQuery("Region");
        regionQuery.equalTo("name", routeData.region.name);
        let region = await regionQuery.first();
        
        if (!region) {
          region = new CloudObject("Region");
          region.set(routeData.region);
          await region.save();
        }
        regionPointer = region.toPointer();
      }

      // 创建CommonBirds指针数组
      const birdPointers = [];
      for (const birdData of routeData.commonBirds) {
        const birdQuery = new CloudQuery("BirdType");
        birdQuery.equalTo("name", birdData.name);
        let bird = await birdQuery.first();
        
        if (bird) {
          birdPointers.push(bird.toPointer());
          console.log(`找到鸟类：${birdData.name}`);
        } else {
          console.warn(`未找到鸟类: ${birdData.name}`);
        }
      }

      // 创建新路线对象
      const newRoute = new CloudObject("Route");
      newRoute.set({
        title: routeData.name,
        location: routeData.region?.name || routeData.startPoint,
        duration: `${routeData.estimatedTime}小时`,
        description: routeData.description,
        thumbnail: routeData.routeImage,
        birdCount: routeData.commonBirds?.length || 0,
        rating: routeData.rating,
        birds: birdPointers,
        tags: routeData.region?.featureTags || [],
        region: regionPointer,
        routelength: routeData.routelength,
        difficulty: routeData.difficulty,
        bestMonths: routeData.bestMonths,
        startPoint: routeData.startPoint,
        endPoint: routeData.endPoint,
        facilities: routeData.facilities,
        popularity: routeData.popularity
      });
      
      // 保存到数据库
      await newRoute.save();
      console.log(`路线"${routeData.name}"保存成功`);
    } catch (error) {
      console.error(`保存路线"${routeData.name}"时出错`, error);
    }
  }

  console.log("所有路线数据处理完成");
  await this.loadRoutes(); // 刷新列表
}

async handleUploadClick() {
  const newRoute = new CloudObject("Route");
  newRoute.set({
      title: "新建路线",
      location: "未设置位置",
      duration: "0小时",
      birdCount: 0,
      rating: 0,
      thumbnail: "https://picsum.photos/600/400?random=" + Math.floor(Math.random() * 1000),
      birds: [],
      tags: []
    });
    
    try {
      await newRoute.save();
      await this.loadRoutes();
      console.log('路线已创建并保存！');
    } catch (error) {
      console.error('创建路线失败', error);
    }
}
 // 点击鸟类头像的方法
  openBirdModal(bird: CloudObject) {
    this.selectedBird = {
      name: bird.get('name'),
      scientificName: bird.get('scientificName'),
      image: bird.get('image') || 'assets/default-bird.png',
      description: bird.get('description'),
      conservationStatus: bird.get('conservationStatus'),
      tags: bird.get('tags') || [],
      taxonomy: bird.get('taxonomy') || {},
      rating: bird.get('rating') || 0,
      characteristics: bird.get('characteristics') || {},
      observation: bird.get('observation') || {}
    };
    this.showBirdModal = true;
  }

  // 关闭鸟类详情模态框
  closeBirdModal() {
    this.showBirdModal = false;
    this.selectedBird = null;
  }
  
}
