import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { pinyin } from 'pinyin-pro';
import { addIcons } from 'ionicons';
import { checkmark} from 'ionicons/icons';
addIcons({checkmark})

@Component({
  selector: 'weather',
  templateUrl: './weather.component.html',
  styleUrls: ['./weather.component.scss'],
  standalone: true,
  imports:[CommonModule]
})
export class WeatherComponent  implements OnInit {

//  

  time: string="";
  yulu: string="";
  
  juti: string="";
  loading: HTMLIonLoadingElement | null = null; 
  weather: any = [
  ];

  async fetchWeatherData() {
    // 第一个请求，获取 adcode
    // const apiUrl = '/api/api/weather/city/101240101';
    // try {
    //   const response: any = await this.http.get(apiUrl).toPromise(); // 使用 toPromise()
    //   const adcode = response; // 获取 adcode 字段
    //   if (adcode) {
    //     this.weather = adcode; // 赋值给 weather
    //     console.log(this.weather);
    //   } else {
    //     console.error('adcode not found in response:', response);
    //   }
    // } catch (error) {
    //   console.error('Error fetching adcode:', error);
    // }
    this.weather={
      "message": "success感谢又拍云(upyun.com)提供CDN赞助",
      "status": 200,
      "date": "20241225",
      "time": "2024-12-25 17:35:09",
      "cityInfo": {
          "city": "南昌市",
          "citykey": "101240101",
          "parent": "江西",
          "updateTime": "13:32"
      },
      "data": {
          "shidu": "43%",
          "pm25": 110,
          "pm10": 137,
          "quality": "轻度",
          "wendu": "11.3",
          "ganmao": "儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼",
          "forecast": [
              {
                  "date": "25",
                  "high": "高温 13℃",
                  "low": "低温 5℃",
                  "ymd": "2024-12-25",
                  "week": "星期三",
                  "sunrise": "07:07",
                  "sunset": "17:26",
                  "aqi": 93,
                  "fx": "北风",
                  "fl": "2级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "26",
                  "high": "高温 12℃",
                  "low": "低温 6℃",
                  "ymd": "2024-12-26",
                  "week": "星期四",
                  "sunrise": "07:07",
                  "sunset": "17:26",
                  "aqi": 75,
                  "fx": "北风",
                  "fl": "3级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "27",
                  "high": "高温 12℃",
                  "low": "低温 7℃",
                  "ymd": "2024-12-27",
                  "week": "星期五",
                  "sunrise": "07:07",
                  "sunset": "17:27",
                  "aqi": 61,
                  "fx": "北风",
                  "fl": "3级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "28",
                  "high": "高温 10℃",
                  "low": "低温 3℃",
                  "ymd": "2024-12-28",
                  "week": "星期六",
                  "sunrise": "07:08",
                  "sunset": "17:28",
                  "aqi": 48,
                  "fx": "东北风",
                  "fl": "3级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "29",
                  "high": "高温 12℃",
                  "low": "低温 1℃",
                  "ymd": "2024-12-29",
                  "week": "星期日",
                  "sunrise": "07:08",
                  "sunset": "17:28",
                  "aqi": 59,
                  "fx": "东北风",
                  "fl": "1级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "30",
                  "high": "高温 13℃",
                  "low": "低温 7℃",
                  "ymd": "2024-12-30",
                  "week": "星期一",
                  "sunrise": "07:08",
                  "sunset": "17:29",
                  "aqi": 65,
                  "fx": "西北风",
                  "fl": "1级",
                  "type": "阴",
                  "notice": "不要被阴云遮挡住好心情"
              },
              {
                  "date": "31",
                  "high": "高温 15℃",
                  "low": "低温 6℃",
                  "ymd": "2024-12-31",
                  "week": "星期二",
                  "sunrise": "07:09",
                  "sunset": "17:30",
                  "aqi": 57,
                  "fx": "东北风",
                  "fl": "2级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "01",
                  "high": "高温 13℃",
                  "low": "低温 7℃",
                  "ymd": "2025-01-01",
                  "week": "星期三",
                  "sunrise": "07:09",
                  "sunset": "17:30",
                  "aqi": 50,
                  "fx": "东北风",
                  "fl": "2级",
                  "type": "多云",
                  "notice": "阴晴之间，谨防紫外线侵扰"
              },
              {
                  "date": "02",
                  "high": "高温 9℃",
                  "low": "低温 5℃",
                  "ymd": "2025-01-02",
                  "week": "星期四",
                  "sunrise": "07:09",
                  "sunset": "17:31",
                  "aqi": 45,
                  "fx": "东北风",
                  "fl": "3级",
                  "type": "阴",
                  "notice": "不要被阴云遮挡住好心情"
              },
              {
                  "date": "03",
                  "high": "高温 11℃",
                  "low": "低温 4℃",
                  "ymd": "2025-01-03",
                  "week": "星期五",
                  "sunrise": "07:09",
                  "sunset": "17:32",
                  "aqi": 43,
                  "fx": "东北风",
                  "fl": "3级",
                  "type": "多云",
                  "notice": "阴晴之间，谨防紫外线侵扰"
              },
              {
                  "date": "04",
                  "high": "高温 12℃",
                  "low": "低温 4℃",
                  "ymd": "2025-01-04",
                  "week": "星期六",
                  "sunrise": "07:10",
                  "sunset": "17:32",
                  "aqi": 47,
                  "fx": "北风",
                  "fl": "2级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "05",
                  "high": "高温 14℃",
                  "low": "低温 6℃",
                  "ymd": "2025-01-05",
                  "week": "星期日",
                  "sunrise": "07:10",
                  "sunset": "17:33",
                  "aqi": 56,
                  "fx": "北风",
                  "fl": "2级",
                  "type": "阴",
                  "notice": "不要被阴云遮挡住好心情"
              },
              {
                  "date": "06",
                  "high": "高温 12℃",
                  "low": "低温 7℃",
                  "ymd": "2025-01-06",
                  "week": "星期一",
                  "sunrise": "07:10",
                  "sunset": "17:34",
                  "aqi": 43,
                  "fx": "东北风",
                  "fl": "3级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "07",
                  "high": "高温 12℃",
                  "low": "低温 5℃",
                  "ymd": "2025-01-07",
                  "week": "星期二",
                  "sunrise": "07:10",
                  "sunset": "17:35",
                  "aqi": 46,
                  "fx": "东北风",
                  "fl": "2级",
                  "type": "晴",
                  "notice": "愿你拥有比阳光明媚的心情"
              },
              {
                  "date": "08",
                  "high": "高温 14℃",
                  "low": "低温 6℃",
                  "ymd": "2025-01-08",
                  "week": "星期三",
                  "sunrise": "07:10",
                  "sunset": "17:35",
                  "aqi": 61,
                  "fx": "北风",
                  "fl": "1级",
                  "type": "阴",
                  "notice": "不要被阴云遮挡住好心情"
              }
          ],
          "yesterday": {
              "date": "24",
              "high": "高温 12℃",
              "low": "低温 3℃",
              "ymd": "2024-12-24",
              "week": "星期二",
              "sunrise": "07:06",
              "sunset": "17:25",
              "aqi": 94,
              "fx": "北风",
              "fl": "2级",
              "type": "晴",
              "notice": "愿你拥有比阳光明媚的心情"
          }
      }
  }


  }
  image:string=""
  constructor(private http: HttpClient) {
    this.fetchWeatherData().then(() => {
      const dateMap = ["周天", "周一", "周二", "周三", "周四", "周五", "周六"];
      const weekdayMap = ["零", "一", "两", "三", "四", "五"];
      const date = new Date();
      this.time = date.getMonth() + 1 + "-" + date.getDate() + " " + dateMap[date.getDay()];
      if (date.getDay() === 6 || date.getDay() === 0) {
        this.yulu = "好好享受周末吧~";
      } else {
        this.yulu = "再坚持" + weekdayMap[(6 - date.getDay())] + "天就到周末啦~";
      }
      console.log("weather.cityInfo:::", this.weather); // 这里打印的 weather 应该不再是空的
       const weather_log = this.weather.data.forecast[0].type;
      const daytemp = this.weather.data.forecast[0].high;
      const nighttemp = this.weather.data.forecast[0].low;
      
      console.log("nighttemp",nighttemp);
      
      this.juti=nighttemp  + daytemp  
      console.log(this.juti);
      this.image="assets/durian/" + this.convertToPinyin(this.weather?.data?.forecast[0].type) + ".png" 
    });
  }
  convertToPinyin(text: string): string {
    const result = pinyin(text, {
      toneType: 'none', // 不带声调
      type: 'array', // 返回拼音数组
    });
    return result.join(''); // 将拼音数组转换为字符串
  }
  
  
  ngOnInit() {
   

     
      
    
  }
  public gptre:string = "";
  public progress = 0;
  public Complete=false
  


}
