package com.draw.painting.canvas.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.draw.painting.canvas.R;
import com.draw.painting.canvas.core.SketchRepository;
import com.draw.painting.canvas.onClickListener;

import java.util.List;

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

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

    private Context mCon;


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

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

    @NonNull
    @Override
    public HomeVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sketch_gallery, 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));


        LinearLayoutManager linearLayoutcore = new LinearLayoutManager(mCon);
        linearLayoutcore.setOrientation(RecyclerView.HORIZONTAL);
        holder.preRecycler.setLayoutManager(linearLayoutcore);
        SketchItemAdapter sketchItemAdapter = new SketchItemAdapter(mCon, dir);
        sketchItemAdapter.setListener(listener);
        holder.preRecycler.setAdapter(sketchItemAdapter);


        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;

        public HomeVH(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            preRecycler = itemView.findViewById(R.id.pre_recycler);
            seeAll = itemView.findViewById(R.id.tv_seeAll);
        }


    }
}

