package me.texy.treeview.treeview;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

/**
 * Created by xinyuanzhong on 2017/4/21.
 */

public class BaseNodeAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<TreeNode> treeNodeList;
    private BaseNodeViewFactory baseNodeViewFactory;
    private View EMPTY_PARAMETOR;

    public BaseNodeAdapter(Context context, List<TreeNode> treeNodeList, BaseNodeViewFactory baseNodeViewFactory) {
        this.context = context;
        this.treeNodeList = treeNodeList;
        this.baseNodeViewFactory = baseNodeViewFactory;
        this.EMPTY_PARAMETOR = new View(context);
        initNodesBinder();
    }

    private void initNodesBinder() {
        for (TreeNode node : treeNodeList) {
            node.setViewBinder(baseNodeViewFactory.getNodeViewBinder(EMPTY_PARAMETOR, node.getLevel()));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(
                treeNodeList.get(0).getViewBinder().getLayoutId(treeNodeList.get(0).getLevel()), parent, false);

        LinearLayout container = new LinearLayout(context);
        container.setOrientation(LinearLayout.VERTICAL);
        container.addView(view);

        return baseNodeViewFactory.getNodeViewBinder(container, treeNodeList.get(0).getLevel());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final TreeNode treeNode = treeNodeList.get(position);
        final ViewGroup nodeContainer = (ViewGroup) holder.itemView;

        treeNode.setViewBinder((NodeViewBinder) holder);

        if (treeNode.isItemClickEnable()) {
            View view = nodeContainer.getChildAt(0);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("onBindViewHolder", "点击了树形结构" + treeNode.getValue().toString());
                    Log.d("onBindViewHolder", "点击position" + position);

                    treeNode.setExpanded(!treeNode.isExpanded());
                    notifyDataSetChanged();
                }
            });
        }

        if (treeNode.hasChild()) {
            if (treeNode.isExpanded() && nodeContainer.getChildCount() < 2) {
                Log.d("onBindViewHolder", "展开树形结构" + treeNode.getValue().toString());
                RecyclerView childrenView = new RecyclerView(context);
                childrenView.setLayoutManager(new LinearLayoutManager(context));
                BaseNodeAdapter adapter = new BaseNodeAdapter(context, treeNode.getChildren(), baseNodeViewFactory);
                childrenView.setAdapter(adapter);
                nodeContainer.addView(childrenView);
            } else {
                Log.d("onBindViewHolder", "收起树形结构" + treeNode.getValue().toString());
                if (nodeContainer.getChildCount() > 1) {
                    nodeContainer.removeViewAt(1);
                }
            }
        }

        treeNodeList.get(0).getViewBinder().bindView(
                holder.itemView, treeNode);
    }

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

    public static class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

    }
}

@@ -2567,31 +2567,33 @@
     
-if (treeNode.is
+boolean isAlready
 Expanded
 () &
@@ -2592,13 +2592,10 @@
 nded
-() &&
+ =
  nod
@@ -2625,15 +2625,100 @@
 t() 
-%3C 2) %7B%0A
+%3E 1;%0A            if (treeNode.isExpanded()) %7B%0A                if (!isAlreadyExpanded) %7B%0A    
     
@@ -2815,16 +2815,20 @@
         
+  
+  
 Recycler
@@ -2866,32 +2866,36 @@
 rView(context);%0A
+    
                 
@@ -2975,16 +2975,20 @@
         
+  
+  
 BaseNode
@@ -3088,32 +3088,36 @@
                 
+    
 childrenView.set
@@ -3134,16 +3134,20 @@
 apter);%0A
+    
         
@@ -3207,16 +3207,13 @@
     
-%7D else %7B
+    %7D
 %0A   
@@ -3225,135 +3225,54 @@
     
-    Log.d(%22onBindViewHolder%22, %22%E6%94%B6%E8%B5%B7%E6%A0%91%E5%BD%A2%E7%BB%93%E6%9E%84%22 + treeNode.getValue().toString());%0A                if (nodeContainer.getChildCount() %3E 1
+%7D else %7B%0A                if (isAlreadyExpanded
 ) %7B%0A

fix node adapter click event bug