package edu.deanza.calendar;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import edu.deanza.calendar.activities.MeetingsAdapter;
import edu.deanza.calendar.activities.SubscribableAdapter;
import edu.deanza.calendar.domain.Subscribable;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class SimpleSectionedRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context mContext;
    private static final int SECTION_TYPE = 100;

    private boolean mValid = true;
    private int mSectionResourceId;
    private int mTextResourceId;
    private LayoutInflater mLayoutInflater;
    private SubscribableAdapter mBaseAdapter;
    private SparseArray<Section> mSections = new SparseArray<Section>();


    public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
                                              SubscribableAdapter baseAdapter) {

        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mSectionResourceId = sectionResourceId;
        mTextResourceId = textResourceId;
        mBaseAdapter = baseAdapter;
        mContext = context;

        mBaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                mValid = mBaseAdapter.getItemCount()>0;
                notifyDataSetChanged();
            }

            @Override
            public void onItemRangeChanged(int positionStart, int itemCount) {
                mValid = mBaseAdapter.getItemCount()>0;
                notifyItemRangeChanged(positionStart, itemCount);
            }

            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                mValid = mBaseAdapter.getItemCount()>0;
                notifyItemRangeInserted(positionStart, itemCount);
            }

            @Override
            public void onItemRangeRemoved(int positionStart, int itemCount) {
                mValid = mBaseAdapter.getItemCount()>0;
                notifyItemRangeRemoved(positionStart, itemCount);
            }
        });
    }


    public static class SectionViewHolder extends RecyclerView.ViewHolder {

        public TextView title;

        public SectionViewHolder(View view,int mTextResourceid) {
            super(view);
            title = (TextView) view.findViewById(mTextResourceid);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int typeView) {
        if (typeView == SECTION_TYPE) {
            final View view = LayoutInflater.from(mContext).inflate(mSectionResourceId, parent, false);
            return new SectionViewHolder(view,mTextResourceId);
        }else{
            return mBaseAdapter.onCreateViewHolder(parent, toOriginalViewType(typeView));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder sectionOrContentViewHolder, int position) {
        if (isSectionHeaderPosition(position)) {
            ((SectionViewHolder) sectionOrContentViewHolder).title.setText(mSections.get(position).title);
        }else{
            mBaseAdapter.onBindViewHolder((SubscribableAdapter.SubscribableItemViewHolder) sectionOrContentViewHolder,sectionedPositionToPosition(position));
        }

    }

    @Override
    public int getItemViewType(int position) {
        return isSectionHeaderPosition(position)
                ? SECTION_TYPE
                : toNonSectionViewType(position);
    }

    public int toNonSectionViewType(int position) {
        return mBaseAdapter.getItemViewType(sectionedPositionToPosition(position)) + SECTION_TYPE;
    }

    public int toOriginalViewType(int viewType) {
        return viewType - SECTION_TYPE;
    }

    public static class Section {
        int firstPosition;
        int sectionedPosition;
        CharSequence title;

        public Section(int firstPosition, CharSequence title) {
            this.firstPosition = firstPosition;
            this.title = title;
        }

        public CharSequence getTitle() {
            return title;
        }
    }


    public void setSections(List<Section> sections) {
        mSections.clear();

        int offset = 0; // offset positions for the headers we're adding
        for (Section section : sections) {
            section.sectionedPosition = section.firstPosition + offset;
            mSections.append(section.sectionedPosition, section);
            ++offset;
        }

        notifyDataSetChanged();
    }

    public int positionToSectionedPosition(int position) {
        int offset = 0;
        for (int i = 0; i < mSections.size(); i++) {
            if (mSections.valueAt(i).firstPosition > position) {
                break;
            }
            ++offset;
        }
        return position + offset;
    }

    public int sectionedPositionToPosition(int sectionedPosition) {
        if (isSectionHeaderPosition(sectionedPosition)) {
            return RecyclerView.NO_POSITION;
        }

        int offset = 0;
        for (int i = 0; i < mSections.size(); i++) {
            if (mSections.valueAt(i).sectionedPosition > sectionedPosition) {
                break;
            }
            --offset;
        }
        return sectionedPosition + offset;
    }

    public boolean isSectionHeaderPosition(int position) {
        return mSections.get(position) != null;
    }


    @Override
    public long getItemId(int position) {
        return isSectionHeaderPosition(position)
                ? Integer.MAX_VALUE - mSections.indexOfKey(position)
                : mBaseAdapter.getItemId(sectionedPositionToPosition(position));
    }

    @Override
    public int getItemCount() {
        return (mValid ? mBaseAdapter.getItemCount() + mSections.size() : 0);
    }

}
@@ -514,57 +514,204 @@
 )%0A *
-/%0Apublic class SimpleSectionedRecyclerViewAdapter
+ Modified by Karina Antonio (karinafantonio@gmail.com)%0A */%0A%0Apublic class SimpleSectionedRecyclerViewAdapter%0A        %3CVH extends RecyclerView.ViewHolder, A extends RecyclerView.Adapter%3CVH%3E%3E%0A       
  ext
@@ -970,71 +970,9 @@
 ate 
-LayoutInflater mLayoutInflater;%0A    private SubscribableAdapter
+A
  mBa
@@ -1047,22 +1047,14 @@
 ray%3C
-Section
 %3E();%0A%0A
-%0A
     
@@ -1138,28 +1138,8 @@
 eId,
- int textResourceId,
 %0A   
@@ -1185,145 +1185,44 @@
     
-SubscribableAdapter baseAdapter) %7B%0A%0A        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+int textResourceId, A baseAdapter) %7B
 %0A   
@@ -2964,36 +2964,46 @@
 ceId);%0A        %7D
+%0A        
 else
+ 
 %7B%0A            re
@@ -3319,22 +3319,64 @@
 wHolder)
-.title
+%0A                    .title%0A                    
 .setText
@@ -3417,20 +3417,30 @@
        %7D
+%0A        
 else
+ 
 %7B%0A      
@@ -3480,54 +3480,10 @@
 er((
-SubscribableAdapter.SubscribableItemViewHolder
+VH
 ) se
@@ -3507,16 +3507,17 @@
 wHolder,
+ 
 sectione
@@ -5665,25 +5665,24 @@
 ull;%0A    %7D%0A%0A
-%0A
     @Overrid

Make SimpleSectionedRecyclerViewAdapter generic