package us.forkloop.trackor.view;

import us.forkloop.trackor.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class PullableListView extends ListView implements OnScrollListener, OnItemLongClickListener {

    private final String TAG = getClass().getSimpleName();
    private final int MAX_OVER_SCROLL = 200;

    private LayoutInflater inflater;
    private boolean hasHeader;
    
    // long click state
    private View longClickedView;
    private boolean isLongClicked;

    public PullableListView(Context context) {
        super(context);
    }

    public PullableListView(Context context, AttributeSet attributes) {
        super(context, attributes);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, 
            int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        return super.overScrollBy(0, deltaY, 0, scrollY, 0, scrollRangeY, 0, MAX_OVER_SCROLL, isTouchEvent);
    }

    @Override
    protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        /*
        if ( !hasHeader && scrollY > MAX_OVER_SCROLL - 20 ) {
            Log.d(TAG, "Adding a header. " + inflater);
            hasHeader = true;
            View header = inflater.inflate(R.layout.fillin_view, null);
            addHeaderView(header);
            setAdapter(getAdapter());
        }
        */
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (!isLongClicked) {
            Log.d(TAG, String.format("Long click position: %d id: %d", position, id));
            View overlay = view.findViewById(R.id.archive);
            overlay.bringToFront();
            view.invalidate();
            longClickedView = view;
            isLongClicked = true;
        }
        return true;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (isLongClicked) {
            View v = longClickedView.findViewById(R.id.carrier);
            v.bringToFront();
            longClickedView.invalidate();
            isLongClicked = false;
        }
    }
}

@@ -303,24 +303,57 @@
 utInflater;%0A
+import android.view.MotionEvent;%0A
 import andro
@@ -362,24 +362,24 @@
 .view.View;%0A
-
 import andro
@@ -829,39 +829,8 @@
 er;%0A
-    private boolean hasHeader;%0A
     
@@ -1726,333 +1726,8 @@
 Y);%0A
-        /*%0A        if ( !hasHeader && scrollY %3E MAX_OVER_SCROLL - 20 ) %7B%0A            Log.d(TAG, %22Adding a header. %22 + inflater);%0A            hasHeader = true;%0A            View header = inflater.inflate(R.layout.fillin_view, null);%0A            addHeaderView(header);%0A            setAdapter(getAdapter());%0A        %7D%0A        */%0A
     
@@ -2015,16 +2015,84 @@
 chive);%0A
+            overlay.setOnClickListener(new ArchiveClickListener());%0A
         
@@ -2677,32 +2677,32 @@
 w.invalidate();%0A
-
             isLo
@@ -2735,11 +2735,193 @@
  %7D%0A    %7D
+%0A%0A    private class ArchiveClickListener implements OnClickListener %7B%0A%0A        @Override%0A        public void onClick(View v) %7B%0A            Log.d(TAG, %22clicked %22 + v);%0A        %7D%0A    %7D
 %0A%7D%0A

Add listener to overlay.