package com.trace.ar.drawing.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.trace.ar.drawing.ARApp;
import com.trace.ar.drawing.R;
import com.trace.ar.drawing.activity.DetailActivity;
import com.trace.ar.drawing.helper.FilePath;
import com.trace.ar.drawing.helper.ObjectBox;
import com.trace.ar.drawing.helper.Utils;
import com.trace.ar.drawing.listener.OnDeleteLikeListener;

import java.util.List;

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


    private Activity mCon;
    private List<FilePath> data;
    private OnDeleteLikeListener listener;

    public void setListener(OnDeleteLikeListener listener) {
        this.listener = listener;
    }

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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        FilePath filePath = data.get(position);
        String name = filePath.getPath();
        Log.d("---------tt", "---------favorite=" + name);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = Utils.loadImage(mCon, name);
                mCon.runOnUiThread(()->{
                    holder.previewIm.setImageBitmap(bitmap);
                });

            }
        }).start();


        holder.imFavorite.setSelected(true);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, DetailActivity.class);
                intent.putExtra(ARApp.Key_Camera, name);
                mCon.startActivity(intent);
            }
        });

        holder.cardFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.imFavorite.setSelected(false);
                ObjectBox.deleteLike(name);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                if(listener!= null){
                    listener.onDeleteItem(adapterPosition,name);
                }
            }
        });
    }

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

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

        private CardView cardView ;
        private FrameLayout cardFavorite;

        public HomeVH(@NonNull View itemView) {
            super(itemView);
            previewIm = itemView.findViewById(R.id.imPreview);
            cardView = itemView.findViewById(R.id.card_view);
            cardFavorite = itemView.findViewById(R.id.card_favorite);
            imFavorite = itemView.findViewById(R.id.im_favorite);
        }


    }
}
