package com.sipai.jspatrol.Statement;

import android.content.Context;
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.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.Nodes;
import com.sipai.jspatrol.R;

import java.util.List;

/**
 * created by lvliming on 2021/1/19
 */
public class NodesAdapter extends RecyclerView.Adapter<NodesAdapter.ViewHolder> {
    private Context context;
    private List<Nodes> list;

    public NodesAdapter(Context context, List<Nodes> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nodes,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.node.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                Nodes item = list.get(position);
                if (item.getNodes()!=null&&item.getNodes().size()>0){
                    if (viewHolder.rc.getVisibility()==View.GONE) {
                        viewHolder.rc.setVisibility(View.VISIBLE);
                        viewHolder.iv_arrow.setImageResource(R.mipmap.arrow_down);
                    }else {
                        viewHolder.iv_arrow.setImageResource(R.mipmap.arrow_right);
                        viewHolder.rc.setVisibility(View.GONE);
                    }
                }else {
                    StatementListActivity.actionStart(context,item.getId(),item.getText());
                }
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Nodes item = list.get(position);
        holder.tv_title.setText(item.getText());
        if (item.getNodes()!=null&&item.getNodes().size()>0){
            holder.rc.setLayoutManager(new LinearLayoutManager(context));
            NodesAdapter nodesAdapter = new NodesAdapter(context,item.getNodes());
            holder.rc.setAdapter(nodesAdapter);
        }else {
            holder.rc.setVisibility(View.GONE);
            holder.iv_arrow.setImageResource(R.mipmap.arrow_right);
        }
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        View node;
        ImageView iv_arrow;
        TextView tv_title;
        RecyclerView rc;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            node = itemView;
            iv_arrow = itemView.findViewById(R.id.iv_arrow);
            tv_title = itemView.findViewById(R.id.tv_title);
            rc = itemView.findViewById(R.id.rc_children);
        }
    }

}
