package edu.rpi.cs.yacs.adapters;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.truizlop.sectionedrecyclerview.SimpleSectionedAdapter;

import java.util.List;

import edu.rpi.cs.yacs.R;
import edu.rpi.cs.yacs.models.Department;
import edu.rpi.cs.yacs.models.School;
import edu.rpi.cs.yacs.viewholders.ItemViewHolder;

/**
 * Created by Mark Robinson on 10/15/16.
 */
public class SchoolsAdapter extends SimpleSectionedAdapter<ItemViewHolder> {

    private List<School> schoolList = null;
    private RecyclerView recyclerView = null;

    public SchoolsAdapter(final RecyclerView recyclerView, List<School> schoolList) {
        this.recyclerView = recyclerView;
        this.schoolList = schoolList;
    }

    @Override
    protected String getSectionHeaderTitle(int section) {
        if (schoolList != null) {
            return schoolList.get(section).getName();
        }

        return "";
    }

    @Override
    protected int getSectionCount() {
        if (schoolList != null) {
            return schoolList.size();
        }

       return 0;
    }

    @Override
    protected int getItemCountForSection(int section) {
        if (schoolList != null) {
            return schoolList.get(section).getDepartments().size();
        }

        return 0;
    }

    @Override
    protected ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.school_view_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    protected void onBindItemViewHolder(ItemViewHolder holder, final int section, int position) {
        final School school = schoolList.get(section);
        final Department department = school.getDepartments().get(position);

        holder.render(department.getName(), department.getCode());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Adapter", "Clicked");

                v.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        schoolList.clear();

                        recyclerView.getRecycledViewPool().clear();
                        recyclerView.removeAllViews();

                        notifyDataSetChanged();
                    }
                }, 500);
            }
        });
    }
}
@@ -32,55 +32,8 @@
 s;%0A%0A
-import android.support.v7.widget.RecyclerView;%0A
 impo
@@ -264,16 +264,71 @@
 yacs.R;%0A
+import edu.rpi.cs.yacs.fragments.RecyclerViewFragment;%0A
 import e
@@ -434,16 +434,22 @@
 holders.
+School
 ItemView
@@ -565,16 +565,22 @@
 Adapter%3C
+School
 ItemView
@@ -650,32 +650,40 @@
 ate RecyclerView
+Fragment
  recyclerView = 
@@ -679,16 +679,24 @@
 clerView
+Fragment
  = null;
@@ -723,22 +723,16 @@
 Adapter(
-final 
 Recycler
@@ -735,16 +735,24 @@
 clerView
+Fragment
  recycle
@@ -756,16 +756,24 @@
 clerView
+Fragment
 , List%3CS
@@ -810,72 +810,88 @@
 his.
-recyclerView = recyclerView;%0A        this.schoolList = schoolLis
+schoolList = schoolList;%0A        this.recyclerViewFragment = recyclerViewFragmen
 t;%0A 
@@ -1488,16 +1488,22 @@
 otected 
+School
 ItemView
@@ -1722,16 +1722,17 @@
 false);%0A
+%0A
         
@@ -1742,16 +1742,22 @@
 urn new 
+School
 ItemView
@@ -1831,16 +1831,28 @@
 wHolder(
+final School
 ItemView
@@ -1884,16 +1884,22 @@
 section,
+ final
  int pos
@@ -2403,24 +2403,26 @@
 oid run() %7B%0A
+//
             
@@ -2437,198 +2437,978 @@
     
+int modPosition = holder.getLayoutPosition();%0A//%0A//                        Log.d(%22
 school
-List.clear();%0A%0A                        recyclerView.getRecycledViewPool().clear();%0A                        recyclerView.removeAllViews();%0A%0A                        notifyDataSetChanged(
+ list 0 0%22, schoolList.get(0).getDepartments().get(0).getName());%0A//%0A//                        List%3CDepartment%3E departments =  schoolList.get(0).getDepartments();%0A//                        departments.remove(0);%0A//                        schoolList.get(0).setDepartments(departments);%0A//%0A//                        Log.d(%22school list 0 0%22, schoolList.get(0).getDepartments().get(0).getName());%0A//%0A//                        notifyItemRemoved(modPosition);%0A//                        recyclerViewFragment.getMAdapter().notifyItemRemoved(modPosition);%0A%0A%0A//                        Expected behavior: Clear list, animate removal of all items%0A%0A                        int count = getItemCount();%0A%0A                        schoolList.clear();%0A%0A                        notifyItemRangeRemoved(0, count);%0A                        recyclerViewFragment.getMAdapter().notifyItemRangeRemoved(0, count
 );%0A 

Modify SchoolsAdapter