package com.ttstd.dialer.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.qweather.sdk.response.weather.WeatherDaily;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.R;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.utils.DateUtil;

import java.util.List;
import java.util.Locale;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.WeatherHolder> {
    private static final String TAG = "WeatherAdapter";

    private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);

    private FragmentActivity mContext;
    private List<WeatherDaily> mWeatherDailyList;

    public void setWeatherDailyList(List<WeatherDaily> weatherDailyList) {
        mWeatherDailyList = weatherDailyList;
        notifyDataSetChanged();
    }

    @Override
    public WeatherHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = (FragmentActivity) parent.getContext();
        return new WeatherHolder(LayoutInflater.from(mContext).inflate(R.layout.item_weather, parent, false));
    }

    @Override
    public void onBindViewHolder(WeatherHolder holder, int position) {
        WeatherDaily weatherDaily = mWeatherDailyList.get(position);
        String date = weatherDaily.getFxDate();
        if (DateUtil.isTodayAll(date)) {
            holder.tv_date.setText("今天");
        } else {
            String week = DateUtil.convertToChineseWeekdayAll(date);
            holder.tv_date.setText(week);
        }

        String tempMin = weatherDaily.getTempMin();
        holder.tv_temp_min.setText(tempMin + "°");
        String tempMax = weatherDaily.getTempMax();
        holder.tv_temp_max.setText(tempMax + "°");

        String iconDay = weatherDaily.getIconDay();
    }

    @Override
    public int getItemCount() {
        return mWeatherDailyList == null ? 0 : mWeatherDailyList.size();
    }

    public class WeatherHolder extends RecyclerView.ViewHolder {

        TextView tv_date, tv_temp_min, tv_temp_max;
        ImageView iv_icon;

        public WeatherHolder(View itemView) {
            super(itemView);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_temp_min = itemView.findViewById(R.id.tv_temp_min);
            tv_temp_max = itemView.findViewById(R.id.tv_temp_max);
            iv_icon = itemView.findViewById(R.id.iv_icon);
        }
    }
}
