package com.paint.trace.drawing.item;

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.paint.trace.drawing.R;
import com.paint.trace.drawing.other.DataKey;
import com.paint.trace.drawing.other.ItemDec;
import com.paint.trace.drawing.other.Utils;
import com.paint.trace.drawing.alllistener.onItemListener;

import java.util.List;

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

    private List<String> nameList = DataKey.getAllDir();
    private onItemListener listener;

    private Context mCon;


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

    public HomeAdapter(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_home, parent, false);
        return new HomeVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String dir = nameList.get(position);

        holder.tvName.setText(dir.substring(dir.lastIndexOf("_") + 1));


        GridLayoutManager gridLayoutManager = new GridLayoutManager(mCon,2);


        HomeChildAdapter homeChildAdapter = new HomeChildAdapter(mCon, dir);
        homeChildAdapter.setListener(listener);
        holder.preRecycler.setAdapter(homeChildAdapter);
        ItemDec itemDec = new ItemDec(10,10,15);
//        holder.preRecycler.addItemDecoration(itemDecoration);
        holder.preRecycler.setLayoutManager(gridLayoutManager);


        String filePath = dir + "/" + 1+ ".jpg";

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


    }
}
