package com.draw.painting.test.adapter;

import android.content.Context;
import android.content.Intent;
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.test.PaintingApp;
import com.draw.painting.test.R;
import com.draw.painting.test.manager.Keys;
import com.draw.painting.test.onClickListener;
import com.draw.painting.test.ui.CameraActivity;
import com.draw.painting.test.ui.PreViewActivity;

import java.util.List;

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

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

    private Context mCon;


    public void setListener(onClickListener 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);
        Log.d("-------------tt", "------------dir=" + dir);
        holder.tvName.setText(dir.substring(dir.lastIndexOf("_") + 1));


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mCon);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        holder.preRecycler.setLayoutManager(linearLayoutManager);
        HomeChildAdapter homeChildAdapter = new HomeChildAdapter(mCon, dir);
        homeChildAdapter.setListener(listener);
        holder.preRecycler.setAdapter(homeChildAdapter);


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


    }
}
