package com.ar.drawing.art.adapter;

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.art.activity.UIDetail;
import com.ar.drawing.art.dataman.MyBean;
import com.ar.drawing.art.utils.ArtApp;
import com.ar.drawing.art.utils.Common;
import com.ar.drawing.art.R;

import java.util.List;

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


    private Activity mCon;
    private List<MyBean> data;


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

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

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

            }
        }).start();

        if(position == data.size()-1){
            holder.spaceView.setVisibility(View.VISIBLE);
        }else {
            holder.spaceView.setVisibility(View.GONE);
        }

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, UIDetail.class);
                intent.putExtra(ArtApp.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 ;

        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);
        }


    }
}
