package com.app.wallpaper.myada;

import android.annotation.SuppressLint;
import android.content.Context;
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.app.wallpaper.R;
import com.app.wallpaper.getdata.ReadFile;

import com.app.wallpaper.myOnitem.OnFavority;

import com.app.wallpaper.mydao.MyEntity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

public class MyfavorityAdapter extends RecyclerView.Adapter<MyfavorityAdapter.LoadDataHolder> {
    private static int VIEW_TYPE_1 = 0;
    private static final int VIEW_TYPE_2 = 1;
    private final Context context;
    private OnFavority onFavority;
    private final List<MyEntity> datas;
    public MyfavorityAdapter(Context context, List<MyEntity> datas) {
        this.context = context;
        this.datas = datas;
    }
    public void setInfoWallPaperListener(OnFavority onFavority) {
        this.onFavority = onFavority;
    }

    public static class LoadDataHolder extends RecyclerView.ViewHolder {
        public ImageView img_one;
        public LoadDataHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            if (viewType == VIEW_TYPE_1) {
                img_one = itemView.findViewById(R.id.img_one);
            } else {
                img_one = itemView.findViewById(R.id.img_two);
            }

        }
    }
    @NonNull
    @Override
    public LoadDataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_1) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item_layout, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item_layout2, parent, false);
        }
        return new LoadDataHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull LoadDataHolder holder, @SuppressLint("RecyclerView") int position) {
            Glide.with(context)
                    .load(datas.get(position).getImageUrl1())
                    .centerCrop()
                    .placeholder(R.drawable.ic_fail)
                    .into(holder.img_one);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onFavority != null) {
                    onFavority.OnitemClick(datas.get(position),position);
                }
            }
        });
    }
    @Override
    public int getItemViewType(int position) {
        int row = position / 2;
        if (row % 2 != 0) {
            if (position % 2 == 0) {
                return VIEW_TYPE_1; // 偶数位置
            } else {
                return VIEW_TYPE_2; // 奇数位置
            }
        } else {
            if (position % 2 == 0) {
                return VIEW_TYPE_2; // 偶数位置
            } else {
                return VIEW_TYPE_1; // 奇数位置
            }
        }
    }
    @Override
    public int getItemCount() {
        return datas.size();
    }

}

