package com.paint.ar.drawing.adapter;

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.paint.ar.drawing.R;
import com.paint.ar.drawing.ob.ObManager;
import com.paint.ar.drawing.ob.DataImport;
import com.paint.ar.drawing.MyApp;
import com.paint.ar.drawing.activity.CameraActivity;
import com.paint.ar.drawing.topon.AdManager;
import com.paint.ar.drawing.topon.onActionListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

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



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


    public void setData(List<DataImport> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    public ImportAdapter(Activity mCon) {
        this.mCon = new WeakReference<>(mCon);
    }

    public void addData(DataImport newData){
        data.add(newData);
        int i = data.size() - 1;
        Log.d(MyApp.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_import, parent, false);
        return new HomeVH(view);
    }

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

             }
         }).start();



        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdManager.showTopOn(mCon.get(), new onActionListener() {
                    @Override
                    public void onAction() {
                        Intent intent = new Intent(mCon.get(), CameraActivity.class);
                        intent.putExtra(MyApp.Key_Camera, path);
                        mCon.get().startActivity(intent);
                    }
                });

            }
        });
        holder.imDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = holder.getAdapterPosition();
                ObManager.deleteImport(path);
                data.remove(adapterPosition);
                notifyItemRemoved(adapterPosition);
                Log.d(MyApp.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);

        }


    }
}
