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.GetWalls;
import com.app.wallpaper.getdata.ReadFile;
import com.app.wallpaper.getdata.Urldata;
import com.app.wallpaper.myOnitem.OnItemHome;
import com.app.wallpaper.myOnitem.OnLoaditem;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

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

public class LoadDataAdapter extends RecyclerView.Adapter<LoadDataAdapter.LoadDataHolder> {
    private static int VIEW_TYPE_1 = 0;
    private static final int VIEW_TYPE_2 = 1;
    private List<GetWalls> datas = new ArrayList<>();
    private final int posi;
    private final Context context;
    private List<Urldata> urldata = new ArrayList<>();
    private OnItemHome onItemHome;
    private OnLoaditem onLoaditem;


    public LoadDataAdapter(List<GetWalls> datas, Context context, int position, List<Urldata> urldata) {
        this.datas = datas;
        this.context = context;
        this.posi = position;
        this.urldata = urldata;
    }

    public void setInfoWallPaperListener(OnItemHome onItemHome, OnLoaditem onLoaditem) {
        this.onItemHome = onItemHome;
        this.onLoaditem = onLoaditem;
    }

    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) {
        if (posi == 0) {
            Glide.with(context)
                    .load(urldata.get(position).getPreUrl())
                    .placeholder(R.drawable.place)
                    .centerCrop()
                    .into(holder.img_one);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemHome != null) {
                        onItemHome.OnitemClick(position);
                    }
                }
            });
        } else {
            Glide.with(context)
                    .load(datas.get(posi - 1).getList().get(position).getPreUrl())
                    .centerCrop()
                    .placeholder(R.drawable.place)
                    .into(holder.img_one);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onLoaditem != null) {
                        onLoaditem.OnitemClick(posi - 1, 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() {
        if (posi == 0) {
            return urldata.size();
        } else {
            return datas.get(posi - 1).getList().size();
        }
    }
}

