package net.micode.notes.ui;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import net.micode.notes.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;


public class WeatherActivity extends Activity
{
    private String responseData;
    private TextView temperature; // 声明为成员变量
    private TextView location;
    private TextView wea;
    private TextView date;
    private TextView humidity;
    private TextView win;
    private TextView pressure;
    private TextView pm25;
    private TextView airlevel; // 声明为成员变量
    OkHttpClient client = new OkHttpClient();//新建一个OkHttpClient对象
    //api地址http://tianqiapi.com/index/doc?version=day
//注册后获取参数拼接在url上
    private static String weatherUrl = "https://v1.yiketianqi.com/free/day?appid=94339557&version=v61&appsecret=9bIWWwDL&unescape=1";
    @SuppressLint("StaticFieldLeak")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        setContentView(R.layout.activity_weather);
        temperature = this.findViewById(R.id.temperature_tv);
        location = this.findViewById(R.id.location_tv);
        wea= this.findViewById(R.id.weather_tv);
        date= this.findViewById(R.id.date_tv);
        humidity= this.findViewById(R.id.humidity_tv);
        win= this.findViewById(R.id.win_tv);
        pressure= this.findViewById(R.id.pressure_tv);
        pm25= this.findViewById(R.id.pm2_5_tv);
        airlevel= this.findViewById(R.id.airlevel_tv);


        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(weatherUrl)
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.d("NetworkData", responseData);
                    return responseData;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            protected void onPostExecute(String responseData) {
//                if (responseData != null) {
//                    Gson gson = new Gson();
//                    HashMap map = gson.fromJson(responseData, HashMap.class);
//                    temperature.setText("温度："+(CharSequence) map.get("tem"));
//                    location.setText("城市："+(CharSequence) map.get("city"));
//                    wea.setText("天气："+map.get("wea").toString());
//                    date.setText("日期："+map.get("date").toString());
//                    humidity.setText("湿度："+map.get("humidity").toString());
//                    win.setText("风向："+map.get("win").toString());
//                    pressure.setText("气压："+map.get("pressure").toString());
//                    pm25.setText("PM2.5："+map.get("air_pm25").toString());
//                    airlevel.setText("空气等级："+map.get("air_level").toString());
//                }

                if (responseData != null) {
                    Gson gson = new Gson();
                    HashMap map = gson.fromJson(responseData, HashMap.class);
                    // 在此之后，检查 map 中每个键对应的值是否为 null
                    if (map.get("tem") != null) {
                        temperature.setText("温度： " + map.get("tem").toString());
                    }
                    if (map.get("city") != null) {
                        location.setText("城市： " + map.get("city").toString());
                    }
                    // 对其他字段也做相同的检查
                    if (map.get("wea") != null) {
                        wea.setText("天气： " + map.get("wea").toString());
                    }
                    if (map.get("date") != null) {
                        date.setText("日期： " + map.get("date").toString());
                    }
                    if (map.get("humidity") != null) {
                        humidity.setText("湿度： " + map.get("humidity").toString());
                    }
                    if (map.get("win") != null) {
                        win.setText("风向： " + map.get("win").toString());
                    }
                    if (map.get("pressure") != null) {
                        pressure.setText("气压： " + map.get("pressure").toString());
                    }
                    if (map.get("air_pm25") != null) {
                        pm25.setText("PM2.5： " + map.get("air_pm25").toString());
                    }
                    if (map.get("air_level") != null) {
                        airlevel.setText("空气等级： " + map.get("air_level").toString());
                    }
                }

            }
        }.execute();
    }
}
