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

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

  constructor() {
    this.loadBirdType()

  }
  birdList:CloudObject[]=[]
  async loadBirdType(){
    let query=new CloudQuery("BirdType")
    this.birdList=await query.find();
  }
  // 在组件中添加方法
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('☆')
  ];
}

  ngOnInit() {
    this.birdList = []; 
  }
  async importBrid(){
    const birdDataset = [
    {
        name: "红嘴相思鸟",
        scientificName: "Leiothrix lutea",
        rating: 4.5,
        distance: 1.2,
        image: "https://example.com/birds/red-billed-leiothrix.jpg",
        description: "红嘴相思鸟以其鲜艳的红色嘴喙和婉转的鸣叫声而闻名，是深受观鸟爱好者喜爱的鸟类。",
        conservationStatus: "国家二级保护动物",
        tags: ["鸣禽", "色彩鲜艳", "群居"],
        taxonomy: {
            order: "雀形目",
            family: "鹟科",
            genus: "相思鸟属"
        },
        characteristics: {
            size: "体长约15厘米",
            color: "橄榄绿色背部，橙黄色胸腹部，红色嘴喙",
            habitat: "常绿阔叶林、竹林和灌木丛"
        },
        observation: {
            season: "全年",
            time: "清晨和黄昏"
        }
    },
    {
        name: "白鹭",
        scientificName: "Egretta garzetta",
        rating: 3.8,
        distance: 0.8,
        image: "https://example.com/birds/little-egret.jpg",
        description: "白鹭是一种优雅的水鸟，常在湿地和浅水区觅食，飞行时颈部呈'S'形弯曲。",
        conservationStatus: "",
        tags: ["水鸟", "白色羽毛", "湿地"],
        taxonomy: {
            order: "鹈形目",
            family: "鹭科",
            genus: "白鹭属"
        },
        characteristics: {
            size: "体长约55-65厘米",
            color: "全身白色，喙和腿黑色，趾黄色",
            habitat: "湿地、河流、湖泊"
        },
        observation: {
            season: "春秋季",
            time: "早晨"
        }
    },
    {
        name: "红腹锦鸡",
        scientificName: "Chrysolophus pictus",
        rating: 5.0,
        distance: 3.5,
        image: "https://example.com/birds/golden-pheasant.jpg",
        description: "红腹锦鸡是中国特有鸟类，雄鸟羽毛色彩艳丽，是国家二级保护动物。",
        conservationStatus: "国家二级保护动物",
        tags: ["中国特有", "色彩艳丽", "珍稀"],
        taxonomy: {
            order: "鸡形目",
            family: "雉科",
            genus: "锦鸡属"
        },
        characteristics: {
            size: "体长约100厘米",
            color: "金色冠羽，红色胸腹部，多彩的背部羽毛",
            habitat: "山地森林、灌木丛"
        },
        observation: {
            season: "春季",
            time: "日出后"
        }
    },
    {
        name: "黑脸琵鹭",
        scientificName: "Platalea minor",
        rating: 4.7,
        distance: 2.1,
        image: "https://example.com/birds/black-faced-spoonbill.jpg",
        description: "黑脸琵鹭是濒危鸟类，以其独特的匙状嘴喙和黑色面部而闻名。",
        conservationStatus: "国家一级保护动物",
        tags: ["濒危", "湿地", "迁徙"],
        taxonomy: {
            order: "鹈形目",
            family: "鹮科",
            genus: "琵鹭属"
        },
        characteristics: {
            size: "体长约60-78厘米",
            color: "白色羽毛，黑色面部和嘴喙",
            habitat: "沿海湿地、河口"
        },
        observation: {
            season: "冬季",
            time: "白天"
        }
    },
    {
        name: "画眉",
        scientificName: "Garrulax canorus",
        rating: 4.2,
        distance: 0.5,
        image: "https://example.com/birds/hwamei.jpg",
        description: "画眉以其优美的鸣叫声而闻名，是中国传统的笼养鸟之一。",
        conservationStatus: "",
        tags: ["鸣禽", "笼养鸟", "常见"],
        taxonomy: {
            order: "雀形目",
            family: "鹟科",
            genus: "噪鹛属"
        },
        characteristics: {
            size: "体长约21-25厘米",
            color: "棕褐色，眼周有白色眉纹",
            habitat: "灌木丛、竹林、公园"
        },
        observation: {
            season: "全年",
            time: "全天"
        }
    }
    ];
    const BirdType=new CloudObject("BirdType");
    const query=new CloudQuery("Birdtype");
    // 1. 获取所有现有鸟类名称
    const existingBirds = await query.find();
    const existingNames = existingBirds.map(bird => bird.get('name'));

    // 2. 找出需要删除的重复记录
    const birdsToDelete = existingBirds.filter(bird => 
        birdDataset.some(newBird => newBird.name === bird.get('name'))
    );

    // 3. 删除重复记录
    if (birdsToDelete.length > 0) {
        console.log(`找到${birdsToDelete.length}条需要删除的重复记录`);
        await Promise.all(birdsToDelete.map(bird => bird.destroy()));
        console.log('已删除所有重复鸟类记录');
    }

    for(const bird of birdDataset){
      try{
        //检查是否已存在同名鸟类
        query.equalTo("name",bird.name);
        const existing=await query.first();

        if(existing){
          console.log(`鸟类${bird.name}"已存在，保存跳过`);
          continue;
        }
        //创建新鸟类对象
        const newBird=new CloudObject("BirdType");
        newBird.set(bird);
        //保存到数据库
        await newBird.save();
        console.log(`鸟类${bird.name}保存成功`);
      }catch(error){
        console.error(`保存鸟类${bird.name}时出错`,error);
      }
    }
    console.log("所有鸟类数据处理完成");
  }

}

