package com.ar.drawing.assistant.paint.tooladapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.ar.drawing.assistant.paint.tooldb.LocalData;
import com.ar.drawing.assistant.paint.utils.MyApp;
import com.ar.drawing.assistant.paint.ui_activity.ActPreview;
import com.ar.drawing.assistant.paint.utils.Common;
import com.ar.drawing.assistant.paint.R;

import java.util.List;

public class AdFavorite extends RecyclerView.Adapter<AdFavorite.HomeVH> {


    private Activity mCon;
    private List<LocalData> data;


    public void setData(List<LocalData> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    public AdFavorite(Activity mCon) {
        this.mCon = mCon;
    }

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        LocalData localData = data.get(position);
        String name = localData.getPath();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = Common.loadImage(mCon, name);
                mCon.runOnUiThread(()->{
                    holder.previewIm.setImageBitmap(bitmap);
                });

            }
        }).start();


        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, ActPreview.class);
                intent.putExtra(MyApp.Key_Camera, name);
                mCon.startActivity(intent);
            }
        });

    }

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

    public static class HomeVH extends RecyclerView.ViewHolder {
        private ImageView previewIm, imFavorite;

        private CardView cardView ;


        public HomeVH(@NonNull View itemView) {
            super(itemView);
            previewIm = itemView.findViewById(R.id.imPreview);
            cardView = itemView.findViewById(R.id.card_view);

            imFavorite = itemView.findViewById(R.id.im_favorite);
        }


    }
}
