package com.key.simplekeyboard.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import com.key.simplekeyboard.R;
import com.key.simplekeyboard.activity.PreviewActivity;
import com.key.simplekeyboard.data.Otherdata;
import com.key.simplekeyboard.tools.Mytools;
import com.key.simplekeyboard.tools.StaticValue;

import java.util.List;

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

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

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

    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(@NonNull ThumAdapter.ThumVH holder, int position) {
        Otherdata myData = myDataList.get(position);
        String thumbnail = myData.getThumb();
        Glide.with(myContext)
                .load(thumbnail)
                .placeholder(R.color.pre_load_color)
                .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();
    }

    public class ThumVH extends RecyclerView.ViewHolder {
        private ImageView imageViewThumb;
        private ConstraintLayout constraintLayout;

        public ThumVH(@NonNull View itemView) {
            super(itemView);
            imageViewThumb = itemView.findViewById(R.id.image_view_thumb_id);
            constraintLayout = itemView.findViewById(R.id.constraint_layout_id);
        }

        public ImageView getImageViewThumb() {
            return imageViewThumb;
        }

        public ConstraintLayout getConstraintLayout() {
            return constraintLayout;
        }
    }
}
