/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.apps.mytracks;

import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.maps.mytracks.R;

import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A section based resource cursor adapter. Currently only supports one
 * additional section header, "Shared with me".
 * 
 * @author Jimmy Shih
 */
public abstract class SectionResourceCursorAdapter extends ResourceCursorAdapter {

  private enum ItemType {
    TRACK, HEADER
  }

  private final LayoutInflater layoutInflater;
  private int sharedWithMeIndex = -1;

  public SectionResourceCursorAdapter(Context context, int layout, Cursor cursor, int flags) {
    super(context, layout, cursor, flags);
    this.layoutInflater = LayoutInflater.from(context);

    if (cursor != null) {
      sharedWithMeIndex = getSharedWithMeIndex();
      cursor.registerDataSetObserver(dataSetObserver);
    }
  }

  private DataSetObserver dataSetObserver = new DataSetObserver() {
      @Override
    public void onChanged() {
      sharedWithMeIndex = getSharedWithMeIndex();
    };

      @Override
    public void onInvalidated() {
      sharedWithMeIndex = -1;
    };
  };

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (getItemViewType(position) == ItemType.TRACK.ordinal()) {
      return super.getView(getCursorPosition(position), convertView, parent);
    } else {
      TextView textView;
      if (convertView == null) {
        textView = (TextView) layoutInflater.inflate(R.layout.track_list_header, parent, false);
      } else {
        textView = (TextView) convertView;
      }
      return textView;
    }
  }

  @Override
  public int getViewTypeCount() {
    return ItemType.values().length;
  }

  @Override
  public int getCount() {
    int count = super.getCount();
    return count != 0 && sharedWithMeIndex != -1 ? count + 1 : count;
  }

  @Override
  public boolean isEnabled(int position) {
    return getItemViewType(position) == ItemType.TRACK.ordinal();
  }

  @Override
  public Object getItem(int position) {
    if (getItemViewType(position) == ItemType.TRACK.ordinal()) {
      return super.getItem(getCursorPosition(position));
    }
    return null;
  }

  @Override
  public long getItemId(int position) {
    if (getItemViewType(position) == ItemType.TRACK.ordinal()) {
      return super.getItemId(getCursorPosition(position));
    }
    return -1L;
  }

  @Override
  public int getItemViewType(int position) {
    if (sharedWithMeIndex != -1 && sharedWithMeIndex == position) {
      return ItemType.HEADER.ordinal();
    }
    return ItemType.TRACK.ordinal();
  }

  @Override
  public Cursor swapCursor(Cursor newCursor) {
    if (getCursor() != null) {
      getCursor().unregisterDataSetObserver(dataSetObserver);
    }

    Cursor oldCursor = super.swapCursor(newCursor);
    if (newCursor != null) {
      sharedWithMeIndex = getSharedWithMeIndex();
      newCursor.registerDataSetObserver(dataSetObserver);
    }
    return oldCursor;
  }

  /**
   * Gets the Shared with me header index.
   */
  private int getSharedWithMeIndex() {
    int i = 0;
    Cursor cursor = getCursor();
    if (cursor != null && cursor.moveToFirst()) {
      int index = cursor.getColumnIndex(TracksColumns.SHAREDWITHME);
      do {
        int sharedWithMe = cursor.getInt(index);
        if (sharedWithMe == 1) {
          // Returns the index where sharedWithMe == 1 
          return i;
        }
        i++;
      } while (cursor.moveToNext());
    }
    return -1;
  }

  /**
   * Gets the underlying cursor position.
   * 
   * @param position the requested position
   */
  private int getCursorPosition(int position) {
    if (sharedWithMeIndex != -1 && position > sharedWithMeIndex) {
      return position - 1;
    }
    return position;
  }
}

@@ -803,49 +803,8 @@
 or;%0A
-import android.database.DataSetObserver;%0A
 impo
@@ -1552,414 +1552,11 @@
 t);%0A
-%0A    if (cursor != null) %7B%0A      sharedWithMeIndex = getSharedWithMeIndex();%0A      cursor.registerDataSetObserver(dataSetObserver);%0A    %7D%0A  %7D%0A%0A  private DataSetObserver dataSetObserver = new DataSetObserver() %7B%0A      @Override%0A    public void onChanged() %7B%0A      sharedWithMeIndex = getSharedWithMeIndex();%0A    %7D;%0A%0A      @Override%0A    public void onInvalidated() %7B%0A      sharedWithMeIndex = -1;%0A    %7D;%0A  %7D;
+  %7D
 %0A%0A  
@@ -3092,108 +3092,8 @@
 ) %7B%0A
-    if (getCursor() != null) %7B%0A      getCursor().unregisterDataSetObserver(dataSetObserver);%0A    %7D%0A%0A
     
@@ -3144,39 +3144,8 @@
 r);%0A
-    if (newCursor != null) %7B%0A  
     
@@ -3192,72 +3192,8 @@
 ();%0A
-      newCursor.registerDataSetObserver(dataSetObserver);%0A    %7D%0A
     
@@ -3623,17 +3623,16 @@
 hMe == 1
- 
 %0A       

Fix memory leak in SectionResourceCursorAdapter.