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.ImageView;
import android.widget.TextView;

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

import com.trace.ar.drawing.R;
import com.trace.ar.drawing.helper.ClassName;
import com.trace.ar.drawing.helper.ItemDecoration;
import com.trace.ar.drawing.helper.Utils;
import com.trace.ar.drawing.listener.onClickListener;

import java.util.List;

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

    private List<String> nameList = ClassName.getAllDir();
    private onClickListener listener;

    private Context mCon;


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

    public HomeFragmentAdapter(Context mCon) {
        this.mCon = mCon;
    }

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String dir = nameList.get(position);
        Log.d("-------------tt", "------------dir=" + dir);
        holder.tvName.setText(dir.substring(dir.lastIndexOf("_") + 1));


        GridLayoutManager gridLayoutManager = new GridLayoutManager(mCon,2);


        ChildAdapter childAdapter = new ChildAdapter(mCon, dir);
        childAdapter.setListener(listener);
        holder.preRecycler.setAdapter(childAdapter);
        ItemDecoration itemDecoration = new ItemDecoration(10,10,15);
//        holder.preRecycler.addItemDecoration(itemDecoration);
        holder.preRecycler.setLayoutManager(gridLayoutManager);


        String filePath = dir + "/" + 1+ ".jpg";
        Log.d("---------tt","---------filePath="+filePath);
        Bitmap bitmap = Utils.loadImageFromAssets(mCon, filePath);
        holder.imMain.setImageBitmap(bitmap);

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


            }
        });
    }

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

    public static class HomeVH extends RecyclerView.ViewHolder {
        private TextView tvName;

        private RecyclerView preRecycler;
        private TextView seeAll;
        private ImageView imMain;
        private CardView cardView;

        public HomeVH(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            preRecycler = itemView.findViewById(R.id.recycler);
            seeAll = itemView.findViewById(R.id.tv_seeAll);
            imMain = itemView.findViewById(R.id.im_main);
            cardView = itemView.findViewById(R.id.card1);
        }


    }
}
