package com.study.sketch.learning.item;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.study.sketch.learning.SketchingApp;
import com.study.sketch.learning.databinding.AdapterMainBinding;
import com.study.sketch.learning.listener.OnClickListener;
import com.study.sketch.learning.manager.Common;

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

public class AdapterMain extends RecyclerView.Adapter<AdapterMain.HomeVH> {
    private List<String> nameList = SketchingApp.getAllDir();

    private Context mCon;

    private OnClickListener listener;

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

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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        int adapterPosition = holder.getAdapterPosition();

        if (adapterPosition == nameList.size() - 1) {
            holder.itemBinding.space.setVisibility(View.VISIBLE);
        } else {
            holder.itemBinding.space.setVisibility(View.GONE);
        }
        String dir = nameList.get(position);
        String filePath = dir + "/1.jpg";
        String filePath2 = dir + "/2.jpg";
        String filePath3 = dir + "/3.jpg";
        String filePath4 = dir + "/4.jpg";
        Bitmap bitmap = Common.loadImageFromAssets(mCon, filePath);
        String substring = dir.substring(dir.lastIndexOf("_") + 1);

        holder.itemBinding.name.setText(substring);
        List<String> data = new ArrayList<>();
        data.add(filePath);
        data.add(filePath2);
        data.add(filePath3);
        data.add(filePath4);
        AdapterMainCHild listAdapter = new AdapterMainCHild(mCon, data, dir);
        listAdapter.setListener(listener);
        holder.itemBinding.childRecycler.setAdapter(listAdapter);
        holder.itemBinding.childRecycler.setLayoutManager(new LinearLayoutManager(mCon, LinearLayoutManager.HORIZONTAL, false));

        if (holder.itemBinding.childRecycler.getItemDecorationCount() <= 0) {
            SpacingItemDecoration spacingItemDecoration = new SpacingItemDecoration(5, true);
            holder.itemBinding.childRecycler.addItemDecoration(spacingItemDecoration);
        }


    }


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

    public static class HomeVH extends RecyclerView.ViewHolder {
        private AdapterMainBinding itemBinding;

        public HomeVH(@NonNull AdapterMainBinding itemHomeBinding) {
            super(itemHomeBinding.getRoot());
            itemBinding = itemHomeBinding;
        }


    }
}
