package com.ttstd.dialer.adapter;

import android.graphics.drawable.PictureDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.qweather.sdk.response.weather.WeatherHourly;
import com.ttstd.dialer.R;
import com.ttstd.dialer.glide.GlideApp;
import com.ttstd.dialer.glide.svg.SvgSoftwareLayerSetter;
import com.ttstd.dialer.utils.TimeUtils;

import java.util.List;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

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

    private FragmentActivity mContext;
    private List<WeatherHourly> mWeatherHourlyList;
    private RequestBuilder<PictureDrawable> requestBuilder;

    public void setWeatherHourlyList(List<WeatherHourly> weatherHourlyList) {
        mWeatherHourlyList = weatherHourlyList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public HourlyWeather onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = (FragmentActivity) parent.getContext();
        requestBuilder = GlideApp.with(mContext)
                .as(PictureDrawable.class)
                .placeholder(R.drawable.image_loading)
                .error(R.drawable.not_applicable)
                .transition(withCrossFade())
                .listener(new SvgSoftwareLayerSetter());
        return new HourlyWeather(LayoutInflater.from(mContext).inflate(R.layout.item_hourly_weather, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HourlyWeather holder, int position) {
        WeatherHourly weatherHourly = mWeatherHourlyList.get(position);
        String fxTime = weatherHourly.getFxTime();
        holder.tv_time.setText(TimeUtils.formatToHourDescription(fxTime));
        String icon = weatherHourly.getIcon();
        // 获取资源ID
        String fileName = "qweather_" + icon; // 替换为你的文件名
        int resId = mContext.getResources().getIdentifier(fileName, "raw", mContext.getPackageName());
        if (resId != 0) {
            // 使用Glide加载，假设已配置SVG支持
            Glide.with(mContext)
                    .as(PictureDrawable.class)
                    .load(resId) // 加载raw资源ID
                    .diskCacheStrategy(DiskCacheStrategy.NONE) // raw资源通常不缓存
                    .into(holder.iv_icon);
        } else {
            // 处理错误
            Log.e("GlideLoad", "Raw resource not found: " + fileName);
        }

        holder.tv_temp.setText(weatherHourly.getTemp() + "°");
    }

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

    public class HourlyWeather extends RecyclerView.ViewHolder {
        TextView tv_time, tv_temp;
        ImageView iv_icon;

        public HourlyWeather(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_temp = itemView.findViewById(R.id.tv_temp);
            iv_icon = itemView.findViewById(R.id.iv_icon);
        }
    }
}
