package com.exquisite.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.exquisite.R;
import com.exquisite.activity.PreviewActivity;
import com.exquisite.data.Mydata;
import com.exquisite.data.StaticValue;
import com.exquisite.tools.Mytools;

import java.util.List;

public class ThumAdapter extends RecyclerView.Adapter<ThumVH> {
    private Context myContext;
    private List<Mydata> myDataList;

    public ThumAdapter(Context context, List<Mydata> list) {
        myContext = context;
        myDataList = list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ThumVH holder, int position) {
        Mydata myData = myDataList.get(position);
        String thumbnail = myData.getThumb();

        if (position == getItemCount() - 1) {
            holder.getView().setVisibility(View.VISIBLE);
        } else if (position == getItemCount() - 2) {
            holder.getView().setVisibility(View.VISIBLE);
        } else {
            holder.getView().setVisibility(View.GONE);
        }


        Glide.with(myContext)
                .load(thumbnail)
                .placeholder(R.mipmap.logo)
                .transform(new RoundedCorners(Mytools.dp2Px(12)))
                .into(holder.getImageViewThumb());
        holder.getConstraintLayout().setOnClickListener(v -> {
            Intent intent = new Intent(myContext, PreviewActivity.class);
            intent.putExtra(StaticValue.KEY_NAME, myData.getTitle());
            intent.putExtra(StaticValue.KEY_URL, myData.getZipUrl());
            intent.putExtra(StaticValue.KEY_PRE, myData.getPreview());
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myContext.startActivity(intent);
        });
    }


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