package com.sketch.art.trace.adapter;

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.sketch.art.trace.listener.OnResultListener;
import com.sketch.art.trace.activity.CameraActivity;
import com.sketch.art.trace.tools.App;
import com.sketch.art.trace.tools.ComManager;
import com.sketch.art.trace.mydata.BeanLocal;
import com.sketch.art.trace.R;

import java.util.List;

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


    private Activity mCon;
    private List<BeanLocal> data;


    public void setData(List<BeanLocal> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    public AdapterLike(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_like_layout, parent, false);
        return new HomeVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        BeanLocal beanLocal = data.get(position);
        String path = beanLocal.getPath();
        ComManager.loadImageFromAssets(mCon, path, holder.previewIm, new OnResultListener() {
            @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, CameraActivity.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);
        }


    }
}
