package com.hackathon.hackathon2014.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hackathon.hackathon2014.R;
import com.hackathon.hackathon2014.adapter.CategoryListAdapter;
import com.hackathon.hackathon2014.adapter.OptionListAdapter;
import com.hackathon.hackathon2014.model.Category;
import com.hackathon.hackathon2014.model.Option;
import com.hackathon.hackathon2014.model.Question;

import org.springframework.util.CollectionUtils;

import java.util.List;


public class CategoryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.answerContainer, new CategoryListFragment())
                    .commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.answer, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class CategoryListFragment extends Fragment
    {
        public CategoryListFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_answer, container, false);

            Question question = getQuestion();

            CategoryListAdapter optionListAdapter = new CategoryListAdapter(this.getActivity(), question.getCategories());

            ListView listView = (ListView) rootView.findViewById(R.id.answerListView);
            listView.setAdapter(optionListAdapter);

            listView.setOnItemClickListener(new OpenNestedAnswerEvent(question.getCategories()));

            return rootView;
        }

        private Question getQuestion() {
            return (Question) getActivity().getIntent().getSerializableExtra("question");
        }

        private class OpenNestedAnswerEvent implements android.widget.AdapterView.OnItemClickListener {

            private List<Category> categories;

            private OpenNestedAnswerEvent(List<Category> categories) {
                this.categories = categories;
            }

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Category category = categories.get(i);

                if( !CollectionUtils.isEmpty(category.getOptions()) ){
                    displayOption(category);
                }
            }

        }

        private void displayOption(Category category) {
//            final CategoryListFragment fragment = new CategoryListFragment();
//
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("answer", option);
//
//            fragment.setArguments(bundle);
//
//            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
//            transaction.replace(R.id.answerContainer, fragment, "AnswerFragment");
//            transaction.addToBackStack(null);
//            transaction.commit();
        }
    }

}

@@ -1954,59 +1954,8 @@
   %7B%0A
-        public CategoryListFragment() %7B%0A        %7D%0A%0A
     
@@ -1960,32 +1960,32 @@
       @Override%0A
+
         public V
@@ -2280,22 +2280,24 @@
 Adapter 
-option
+category
 ListAdap
@@ -2493,22 +2493,24 @@
 Adapter(
-option
+category
 ListAdap
@@ -3468,16 +3468,17 @@
 gory) %7B%0A
+%0A
 //      
@@ -3487,22 +3487,183 @@
     
-final Category
+getFragmentManager().beginTransaction()%0A//                    .add(R.id.answerContainer, new CategoryListFragment())%0A//                    .commit();%0A%0A            final Option
 List
@@ -3682,32 +3682,30 @@
 gment = new 
-Category
+Option
 ListFragment
@@ -3708,21 +3708,17 @@
 ment();%0A
-//%0A//
+%0A
         
@@ -3747,26 +3747,24 @@
 w Bundle();%0A
-//
             
@@ -3791,31 +3791,31 @@
 le(%22
-answer%22, option);%0A//%0A//
+category%22, category);%0A%0A
     
@@ -3857,13 +3857,9 @@
 e);%0A
-//%0A//
+%0A
     
@@ -3947,18 +3947,16 @@
 tion();%0A
-//
         
@@ -4030,18 +4030,16 @@
 ment%22);%0A
-//
         
@@ -4076,18 +4076,16 @@
 (null);%0A
-//
         
@@ -4127,10 +4127,887 @@
 %0A    %7D%0A%0A
+    public static class OptionListFragment extends Fragment%0A    %7B%0A        @Override%0A        public View onCreateView(LayoutInflater inflater, ViewGroup container,%0A                                 Bundle savedInstanceState) %7B%0A            View rootView = inflater.inflate(R.layout.fragment_answer, container, false);%0A%0A            Category category = getCategory();%0A%0A            OptionListAdapter optionListAdapter = new OptionListAdapter(this.getActivity(), category.getOptions());%0A%0A            ListView listView = (ListView) rootView.findViewById(R.id.answerListView);%0A            listView.setAdapter(optionListAdapter);%0A%0A            return rootView;%0A        %7D%0A%0A        private Category getCategory()%7B%0A            if( getArguments() != null )%7B%0A                return (Category) getArguments().getSerializable(%22category%22);%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Implement options list view.