package com.haven.wallpapers.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

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

import com.bumptech.glide.Glide;
import com.haven.wallpapers.R;
import com.haven.wallpapers.MyApp;
import com.haven.wallpapers.listener.InfoListener;
import com.haven.wallpapers.jsonbean.Data;
import com.haven.wallpapers.jsonbean.Info;
import com.haven.wallpapers.myviewholder.InfoVH;

import java.util.Collections;
import java.util.List;

public class InfoAdapter extends RecyclerView.Adapter<InfoVH> {
    private List<Info> dataInfo;
    private Context myContext;
    private InfoListener infoListener;

    public void setInfoWallpaperListener(InfoListener infoListener) {
        this.infoListener = infoListener;
    }

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

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

    @Override
    public void onBindViewHolder(@NonNull InfoVH holder, int position) {
        Info info = dataInfo.get(position);
        List<Data> list = info.getList();
        Collections.shuffle(list);
        String name = info.getName();

        holder.getText().setText(name);
        ImageView leftImage = holder.getLeftImage();
        ImageView rightImage = holder.getRightImage();
        Data data = list.get(0);
        Data data1 = list.get(1);


        Glide.with(myContext)
                .load(data.getPreviewUrl400())
                .placeholder(MyApp.getPlaceImg())
                .transform(MyApp.getMultiTransformation())
                .skipMemoryCache(true)
                .placeholder(MyApp.getPlaceImg())
                .into(leftImage);
        Glide.with(myContext)
                .load(data1.getPreviewUrl400())
                .placeholder(MyApp.getPlaceImg())
                .transform(MyApp.getMultiTransformation())
                .skipMemoryCache(true)
                .placeholder(MyApp.getPlaceImg())
                .into(rightImage);

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

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

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