package com.lh.wallpaper2.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.lh.wallpaper2.R;
import com.lh.wallpaper2.action.InfoWallpaperListener;
import com.lh.wallpaper2.action.UrlWallpaperListener;
import com.lh.wallpaper2.file.ReadFile;
import com.lh.wallpaper2.json.Info;
import com.lh.wallpaper2.json.Url;
import com.lh.wallpaper2.recycleView.DetailsWallpaperVH;

import java.util.ArrayList;
import java.util.List;


public class DetailsAdapter extends RecyclerView.Adapter<DetailsWallpaperVH> {
    private ArrayList<Url> dataInfo;
    private Context myContext;
    private UrlWallpaperListener urlWallpaperListener;
    @NonNull
    @Override
    public DetailsWallpaperVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.details_vh, parent, false);
        return new DetailsWallpaperVH(itemView);
    }

    public DetailsAdapter(ArrayList<Url> dataInfo, Context myContext) {
        this.dataInfo = dataInfo;
        this.myContext = myContext;
    }
    public void setUrlWallpaperListener(UrlWallpaperListener urlWallpaperListener) {
        this.urlWallpaperListener = urlWallpaperListener;
    }
    @Override
    public void onBindViewHolder(@NonNull DetailsWallpaperVH holder, @SuppressLint("RecyclerView") int position) {
        Url info = dataInfo.get(position);
        Log.d("2323232323232", "onBindViewHolder: "+dataInfo.get(1).getPreUrl());
        Log.d("99999999999", "onBindViewHolder: "+dataInfo.get(position));
        ImageView imageView = holder.getImageView();
        Glide.with(myContext)
                .load(info.getPreUrl())
                .transform(new RoundedCorners(ReadFile.dp2Px(7)))
                .placeholder(R.drawable.preview)
                .into(imageView);
        holder.getItemRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (urlWallpaperListener != null) {
                    urlWallpaperListener.onItemClickAction(dataInfo, position);
                }
            }
        });
    }

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