package com.paint.trace.drawing.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.FrameLayout;
import android.widget.ImageView;

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

import com.paint.trace.drawing.topon.AdManager;
import com.paint.trace.drawing.topon.onActionListener;
import com.paint.trace.drawing.view.MyApplication;
import com.paint.trace.drawing.R;
import com.paint.trace.drawing.other.ImportData;
import com.paint.trace.drawing.view.PreViewActivity;
import com.paint.trace.drawing.db.ObjectBox;

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

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


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


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

    public void addData(ImportData newData){
//        if(data.size() == 0){
//            Log.d(ARApp.TAG, "---------notifyDataSetChanged--");
//
//            notifyDataSetChanged();
//            return;
//        }
        data.add(newData);
        int i = data.size() - 1;
        Log.d(MyApplication.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_thumb, parent, false);
        return new HomeVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        ImportData importData = data.get(position);
        String path = importData.getUri();
        Log.d(MyApplication.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();


        boolean b = ObjectBox.queryIsLike(path);
        holder.imFavorite.setSelected(b);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdManager.showTopOn(mCon, new onActionListener() {
                    @Override
                    public void onAction() {
                        Intent intent = new Intent(mCon, PreViewActivity.class);
                        intent.putExtra(MyApplication.Key_Camera, path);
                        mCon.startActivity(intent);
                    }
                });
            }
        });

        holder.cardFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean selected = holder.imFavorite.isSelected();
                holder.imFavorite.setSelected(!selected);
                boolean newSelect = holder.imFavorite.isSelected();
                if(newSelect){
                    ObjectBox.insertOrUpdateLike(path);
                }else {
                    ObjectBox.deleteLike(path);

                }
            }
        });


    }

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


    }
}
