package com.ttstd.dialer.activity.weather.main;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.qweather.sdk.Callback;
import com.qweather.sdk.response.error.ErrorResponse;
import com.qweather.sdk.response.weather.WeatherDaily;
import com.qweather.sdk.response.weather.WeatherDailyResponse;
import com.qweather.sdk.response.weather.WeatherHourly;
import com.qweather.sdk.response.weather.WeatherHourlyResponse;
import com.qweather.sdk.response.weather.WeatherNow;
import com.qweather.sdk.response.weather.WeatherNowResponse;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ttstd.dialer.base.mvvm.BaseViewModel;
import com.ttstd.dialer.databinding.ActivityWeatherMainBinding;
import com.ttstd.dialer.gson.GsonUtils;
import com.ttstd.dialer.manager.WeatherManager;

import java.util.List;

public class WeatherMainViewModel extends BaseViewModel<ActivityWeatherMainBinding, ActivityEvent> {
    private static final String TAG = "WeatherMainViewModel";

    public MutableLiveData<WeatherNow> mWeatherNowData = new MutableLiveData<>();

    public void getWeatherNow(String adCode) {
        Log.e(TAG, "getWeatherNow: " + adCode);
        WeatherManager.getInstance().getWeatherNow(adCode, new Callback<WeatherNowResponse>() {
            @Override
            public void onSuccess(WeatherNowResponse response) {
                Log.e("getWeatherNow", "onSuccess: " + response);
                WeatherNow weatherNow = response.getNow();
                Log.e("getWeatherNow", "onSuccess: " + weatherNow);
                mWeatherNowData.postValue(weatherNow);
            }

            @Override
            public void onFailure(ErrorResponse errorResponse) {
                Log.e("getWeatherNow", "onFailure: " + errorResponse.getError().getStatus());
                Log.e("getWeatherNow", "onFailure: " + errorResponse.getError().getDetail());
            }

            @Override
            public void onException(Throwable e) {
                Log.e("getWeatherNow", "onException: " + e.getMessage());
            }
        });
    }

    public MutableLiveData<List<WeatherHourly>> mWeather24hData = new MutableLiveData<>();

    public void getWeather24h(String adCode) {
        Log.e(TAG, "getWeather24h: " + adCode);
        WeatherManager.getInstance().getWeather24h(adCode, new Callback<WeatherHourlyResponse>() {
            @Override
            public void onSuccess(WeatherHourlyResponse weatherHourlyResponse) {
                Log.e("getWeather24h", "onSuccess: " + weatherHourlyResponse);
                List<WeatherHourly> weatherHourlies = weatherHourlyResponse.getHourly();
                Log.e("getWeather24h", "onSuccess: " + GsonUtils.toJSONString(weatherHourlies));
                mWeather24hData.postValue(weatherHourlies);
            }

            @Override
            public void onFailure(ErrorResponse errorResponse) {
                Log.e("getWeather24h", "onFailure: " + errorResponse.getError().getStatus());
                Log.e("getWeather24h", "onFailure: " + errorResponse.getError().getDetail());
            }

            @Override
            public void onException(Throwable throwable) {
                Log.e("getWeather24h", "onException: " + throwable.getMessage());
            }
        });
    }


    public MutableLiveData<List<WeatherDaily>> mWeatherDailyListData = new MutableLiveData<>();

    public void getWeather10D(String adCode) {
        Log.e(TAG, "getWeather10D: " + adCode);
        WeatherManager.getInstance().getWeather10D(adCode, new Callback<WeatherDailyResponse>() {
            @Override
            public void onSuccess(WeatherDailyResponse weatherDailyResponse) {
                Log.e("getWeather10D", "onSuccess: ");
                List<WeatherDaily> weatherDailyList = weatherDailyResponse.getDaily();
                Log.e("getWeather10D", "onSuccess: " + GsonUtils.toJSONString(weatherDailyList));
                mWeatherDailyListData.postValue(weatherDailyList);
            }

            @Override
            public void onFailure(ErrorResponse errorResponse) {
                Log.e("getWeather10D", "onFailure: ");
            }

            @Override
            public void onException(Throwable throwable) {
                Log.e("getWeather10D", "onException: " + throwable.getMessage());
            }
        });
    }
}
