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

import android.util.Log;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.baidu.location.BDLocation;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.qweather.sdk.response.weather.WeatherDaily;
import com.qweather.sdk.response.weather.WeatherHourly;
import com.qweather.sdk.response.weather.WeatherNow;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.R;
import com.ttstd.dialer.adapter.HourlyWeatherAdapter;
import com.ttstd.dialer.adapter.WeatherAdapter;
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.databinding.ActivityWeatherMainBinding;
import com.ttstd.dialer.utils.DateUtil;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

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

    private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
    private WeatherAdapter mWeatherAdapter;
    private HourlyWeatherAdapter mHourlyWeatherAdapter;

//    @Override
//    public boolean setfitWindow() {
//        return true;
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_weather_main;
    }

    @Override
    protected void initDataBinding() {
        mViewModel.setContext(this);
        mViewModel.setVDBinding(mViewDataBinding);
        mViewModel.setLifecycle(getLifecycleSubject());
        mViewDataBinding.setClick(new BtnClick());
    }

    @Override
    protected void initView() {
        mWeatherAdapter = new WeatherAdapter();
        mViewDataBinding.recyclerView.setAdapter(mWeatherAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WeatherMainActivity.this, LinearLayoutManager.VERTICAL, false);
        mViewDataBinding.recyclerView.setLayoutManager(linearLayoutManager);

        mHourlyWeatherAdapter = new HourlyWeatherAdapter();
        mViewDataBinding.rvHourly.setAdapter(mHourlyWeatherAdapter);
        LinearLayoutManager horizontalLinearLayoutManager = new LinearLayoutManager(WeatherMainActivity.this, LinearLayoutManager.HORIZONTAL, false);
        mViewDataBinding.rvHourly.setLayoutManager(horizontalLinearLayoutManager);


    }

    @Override
    protected void initData() {
        Log.e(TAG, "initData: ");

        mViewModel.mWeatherNowData.observe(this, new Observer<WeatherNow>() {
            @Override
            public void onChanged(WeatherNow weatherNow) {
                mViewDataBinding.setWeatherNow(weatherNow);
            }
        });
        mViewModel.mWeather24hData.observe(this, new Observer<List<WeatherHourly>>() {
            @Override
            public void onChanged(List<WeatherHourly> weatherHourlies) {
                mHourlyWeatherAdapter.setWeatherHourlyList(weatherHourlies);
            }
        });
        mViewModel.mWeatherDailyListData.observe(this, new Observer<List<WeatherDaily>>() {
            @Override
            public void onChanged(List<WeatherDaily> weatherDailies) {
                Optional<WeatherDaily> dailyOptional = weatherDailies.stream().filter(new Predicate<WeatherDaily>() {
                    @Override
                    public boolean test(WeatherDaily weatherDaily) {
                        return DateUtil.isTodayAll(weatherDaily.getFxDate());
                    }
                }).findAny();
                if (dailyOptional.isPresent()) {
                    WeatherDaily weatherDaily = dailyOptional.get();
                    mViewDataBinding.tvTempRange.setText("最高" + weatherDaily.getTempMax() + "°" + "\t" + "最低" + weatherDaily.getTempMin() + "°");
                } else {

                }
                mWeatherAdapter.setWeatherDailyList(weatherDailies);
            }
        });

        boolean manually = mMMKV.decodeBool(CommonConfig.MANUALLY_SELECT_LOCATION, false);
        if (manually) {

        } else {
            LiveEventBus.get(CommonConfig.LOCATION_CHANGED_KEY, BDLocation.class)
                    .observeSticky(this, new Observer<BDLocation>() {
                        @Override
                        public void onChanged(BDLocation bdLocation) {
                            mViewDataBinding.tvLocation.setText(bdLocation.getDistrict());
                            mViewModel.getWeatherNow(bdLocation.getAdCode());
                            mViewModel.getWeather24h(bdLocation.getAdCode());
                            mViewModel.getWeather10D(bdLocation.getAdCode());
                        }
                    });

        }
    }

    public class BtnClick {

    }
}
