package de.fau.cs.mad.fablab.android.view.fragments.categorysearch;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;

import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import de.fau.cs.mad.fablab.android.R;
import de.fau.cs.mad.fablab.android.view.common.fragments.BaseDialogFragment;
import de.fau.cs.mad.fablab.rest.core.Category;

public class CategoryDialogFragment extends BaseDialogFragment {

    @Bind(R.id.container_tree_view)
    RelativeLayout treeViewContainer;

    @Inject
    CategoryDialogFragmentViewModel mViewModel;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_category_search, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initializeCategoryTree();
    }

    private void initializeCategoryTree()
    {
        List<Category> roots = mViewModel.getRootCategories();
        HashMap<Long, Category> children = mViewModel.getChildrenCategories();

        TreeNode root = TreeNode.root();
        for(Category c : roots)
        {
            TreeNode node = new TreeNode(new TreeItemHolder.TreeItem(c));
            depthFirstSearch(node, c, children);
            root.addChild(node);
        }

        AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
        tView.setDefaultViewHolder(TreeItemHolder.class);
        tView.setDefaultAnimation(true);
        tView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);
        treeViewContainer.addView(tView.getView());
    }

    private void depthFirstSearch(TreeNode node, Category category, HashMap<Long, Category> children)
    {
        System.out.println(category.getName());
        List<Long> childrenID = category.getCategories();
        if(childrenID.isEmpty())
        {
            return;
        }
        for(Long id : childrenID)
        {
            Category child = children.get(id);
            TreeNode childNode = new TreeNode(new TreeItemHolder.TreeItem(child));
            depthFirstSearch(childNode, child, children);
            node.addChild(childNode);
        }
    }
}

@@ -128,16 +128,47 @@
 Bundle;%0A
+import android.text.TextUtils;%0A
 import a
@@ -195,16 +195,16 @@
 flater;%0A
-
 import a
@@ -880,16 +880,56 @@
 Model;%0A%0A
+    private AndroidTreeView mTreeView;%0A%0A
 %0A    @Ov
@@ -1490,16 +1490,242 @@
 yTree();
+%0A%0A        if (savedInstanceState != null) %7B%0A            String state = savedInstanceState.getString(%22tState%22);%0A            if (!TextUtils.isEmpty(state)) %7B%0A                mTreeView.restoreState(state);%0A            %7D%0A        %7D
 %0A    %7D%0A%0A
@@ -2177,25 +2177,13 @@
     
-AndroidTreeView t
+mTree
 View
@@ -2227,33 +2227,37 @@
  root);%0A        
-t
+mTree
 View.setDefaultV
@@ -2289,33 +2289,37 @@
 class);%0A        
-t
+mTree
 View.setDefaultA
@@ -2342,17 +2342,21 @@
         
-t
+mTree
 View.set
@@ -2441,17 +2441,21 @@
 addView(
-t
+mTree
 View.get
@@ -3023,24 +3023,24 @@
 childNode);%0A
-
         %7D%0A  
@@ -3042,11 +3042,196 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        outState.putString(%22tState%22, mTreeView.getSaveState());%0A    %7D
 %0A%7D%0A

save state of treeView