package com.study.sketch.learning.item;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
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.study.sketch.learning.activity.ActivitySketching;
import com.study.sketch.learning.database.BeanImport;
import com.study.sketch.learning.database.MyDb;
import com.study.sketch.learning.R;
import com.study.sketch.learning.SketchingApp;

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

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


    private Activity mCon;
    private List<BeanImport> data = new ArrayList<>();


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

    public void addData(BeanImport newData){
        data.add(newData);
        int i = data.size() - 1;
        Log.d(SketchingApp.TAG, "---------addData  i="+i);
        notifyItemInserted(i);
        notifyItemChanged(i);

    }

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        BeanImport beanImport = data.get(position);
        String path = beanImport.getUri();
        Log.d(SketchingApp.TAG, "---------ImportAdapter  position="+position+"------" + path);
         new Thread(new Runnable() {
             @Override
             public void run() {
                 Bitmap bitmap = BitmapFactory.decodeFile(path);
                 mCon.runOnUiThread(()->{
                     holder.previewIm.setImageBitmap(bitmap);
                 });

             }
         }).start();



        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, ActivitySketching.class);
                intent.putExtra(SketchingApp.Key_Camera, path);
                mCon.startActivity(intent);
            }
        });
        holder.imDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = holder.getAdapterPosition();
                MyDb.deleteImport(path);
                data.remove(adapterPosition);
                notifyItemRemoved(adapterPosition);
                Log.d(SketchingApp.TAG,"-------adapterPosition="+adapterPosition);
            }
        });



    }

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

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

        private CardView cardView;


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

        }


    }
}
