package com.trace.drawing.people.item;

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 androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.trace.drawing.people.bean.BeanUpload;
import com.trace.drawing.people.bean.MyDataBase;
import com.trace.drawing.people.callbacl.OnUploadRemoveCallback;
import com.trace.drawing.people.databinding.RecyclerUploadBinding;
import com.trace.drawing.people.page.PreviewPage;
import com.trace.drawing.people.tools.App;

import java.util.ArrayList;
import java.util.List;

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


    private Activity mCon;
    private OnUploadRemoveCallback listener;
    private List<BeanUpload> data = new ArrayList<>();


    public void setData(List<BeanUpload> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    public UploadItem(Activity mCon, OnUploadRemoveCallback listener) {
        this.mCon = mCon;
        this.listener = listener;
    }

    public void addData(BeanUpload newData){
        data.add(newData);
        int i = data.size() - 1;
        notifyItemInserted(i);
        notifyItemChanged(i);

    }

    @NonNull
    @Override
    public HomeVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new HomeVH(RecyclerUploadBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        BeanUpload beanUpload = data.get(position);
        String path = beanUpload.getUri();
         new Thread(new Runnable() {
             @Override
             public void run() {
                 Bitmap bitmap = BitmapFactory.decodeFile(path);
                 mCon.runOnUiThread(()->{
                     holder.binding.imPreview.setImageBitmap(bitmap);
                 });

             }
         }).start();



        holder.binding.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, PreviewPage.class);
                intent.putExtra(App.Key_Camera, path);
                mCon.startActivity(intent);
            }
        });
        holder.binding.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = holder.getAdapterPosition();
                MyDataBase.deleteImport(path);
                data.remove(adapterPosition);
                notifyItemRemoved(adapterPosition);
                listener.onRemoveUpload();
            }
        });



    }

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

    public static class HomeVH extends RecyclerView.ViewHolder {


        private RecyclerUploadBinding binding;


        public HomeVH(@NonNull RecyclerUploadBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }


    }
}
