package com.picture.ar.drawing.list;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.picture.ar.drawing.listener.BitmapListener;
import com.picture.ar.drawing.activity.PreviewActivity;
import com.picture.ar.drawing.utils.App;
import com.picture.ar.drawing.utils.ComUtils;
import com.picture.ar.drawing.data.BeanLocal;
import com.picture.ar.drawing.R;

import java.util.List;

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


    private Activity mCon;
    private List<BeanLocal> data;


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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        BeanLocal beanLocal = data.get(position);
        String path = beanLocal.getPath();
        ComUtils.loadImageFromAssets(mCon, path, holder.previewIm, new BitmapListener() {
            @Override
            public void onResultBitmap(Bitmap bitmap) {
                if(bitmap == null){
                    Bitmap bitmap1 = BitmapFactory.decodeFile(path);
                    holder.previewIm.setImageBitmap(bitmap1);
                }

            }
        });



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

    }

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

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

        private CardView cardView ;

//        private View spaceView ;
        public HomeVH(@NonNull View itemView) {
            super(itemView);
            previewIm = itemView.findViewById(R.id.imPreview);
            cardView = itemView.findViewById(R.id.card_view);
//            spaceView = itemView.findViewById(R.id.view);
            imFavorite = itemView.findViewById(R.id.im_favorite);
        }


    }
}
