package com.lh.wallpaper2.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.lh.wallpaper2.R;
import com.lh.wallpaper2.action.InfoWallpaperListener;
import com.lh.wallpaper2.file.ReadFile;
import com.lh.wallpaper2.json.Info;
import com.lh.wallpaper2.json.Url;
import com.lh.wallpaper2.recycleView.InfoWallpaperVH;

import java.util.List;

public class InfoWallpaperAdapter extends RecyclerView.Adapter<InfoWallpaperVH> {
    private List<Info> dataInfo;
    private Context myContext;
    private InfoWallpaperListener infoWallpaperListener;

    public void setInfoWallpaperListener(InfoWallpaperListener infoWallpaperListener) {
        this.infoWallpaperListener = infoWallpaperListener;
    }

    public InfoWallpaperAdapter(List<Info> infoList, Context myCon) {
        dataInfo = infoList;
        myContext = myCon;
    }

    @NonNull
    @Override
    public InfoWallpaperVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.home_wallpaper_adapter_list, parent, false);
        return new InfoWallpaperVH(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InfoWallpaperVH holder, int position) {
        Info info = dataInfo.get(position);
        String name = info.getName();

        if (name != null && !name.isEmpty()) {
            // 将首字母转换为大写，其他字母保持不变
            String capitalized = name.substring(0, 1).toUpperCase() + name.substring(1);
            holder.getText().setText(capitalized);
        } else {
            holder.getText().setText(name); // 如果 name 为 null 或空，直接设置
        }

        ImageView leftImage = holder.getLeftImage();
        ImageView rightImage = holder.getRightImage();
        Url homeLeftUrl = info.getList().get(0);
        Url homeRightUrl = info.getList().get(1);


        Glide.with(myContext)
                .load(info.getList().get(0).getPreUrl())
                .transform(new RoundedCorners(ReadFile.dp2Px(7)))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(ReadFile.dp2Px(7))))
                .skipMemoryCache(true)
                .placeholder(R.drawable.preview)
                .into(leftImage);
        Glide.with(myContext)
                .load(info.getList().get(1).getPreUrl())
                .transform(new RoundedCorners(ReadFile.dp2Px(7)))
                .skipMemoryCache(true)
                .placeholder(R.drawable.preview)
                .into(rightImage);

        RelativeLayout seeAll = holder.getSeeAll();
        leftImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                infoWallpaperListener.onHomeInfoClickAction(homeLeftUrl);
            }
        });
        rightImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                infoWallpaperListener.onHomeInfoClickAction(homeRightUrl);
            }
        });

        seeAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                infoWallpaperListener.onItemClickAction(info);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataInfo.size();
    }
}
