package com.trace.ar.drawing.adapter;

import android.content.Context;
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.R;
import com.trace.ar.drawing.helper.Utils;
import com.trace.ar.drawing.listener.onClickListener;

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


    private Context mCon;
    private String dir;

    private int count = 4;
    private onClickListener listener;
    public ChildAdapter(Context mCon, String dirStr) {
        this.mCon = mCon;
        dir = dirStr;
    }

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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String filePath = dir + "/" + (position+2) + ".jpg";
//        if(position %2==0){
//           holder.fl.setBackgroundColor(mCon.getColor(R.color.tab_true));
//        }else {
//            holder.fl.setBackgroundColor(mCon.getColor(R.color.white));
//        }

        Log.d("---------tt","---------filePath="+filePath);
        Bitmap bitmap = Utils.loadImageFromAssets(mCon, filePath);
        holder.previewIm.setImageBitmap(bitmap);

        ViewGroup.LayoutParams layoutParams = holder.previewIm.getLayoutParams();

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!= null){
                    listener.onClickItem(false,filePath);
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return count;
    }

    public static class HomeVH extends RecyclerView.ViewHolder {
        private ImageView previewIm;
        private CardView cardView;
        private FrameLayout fl;

        public HomeVH(@NonNull View itemView) {
            super(itemView);
            previewIm = itemView.findViewById(R.id.imPreview);
           cardView = itemView.findViewById(R.id.card);
            fl = itemView.findViewById(R.id.frame);
        }


    }
}
