package com.dailystudio.app.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

import com.dailystudio.app.widget.DeferredHandler;
import com.dailystudio.development.Logger;

public abstract class AbsAdapterDialogFragment<Item, ItemSet> extends AbsLoaderDialogFragment<ItemSet>
	implements OnItemClickListener {
	
	public interface OnListItemSelectedListener {
		
        public void onListItemSelected(Object itemData);
        
    }

	private AdapterView<BaseAdapter> mAdapterView;
	private BaseAdapter mAdapter;
	
    private OnListItemSelectedListener mOnListItemSelectedListener;

	@Override
	protected void setupViewsOnDialog(Dialog dialog) {
		super.setupViewsOnDialog(dialog);

		bindAdapterView();

		restartLoader();
	}

	@Override
	public void onItemClick(AdapterView<?> l, View v, int position, long id) {
		if (mOnListItemSelectedListener != null) {
			final BaseAdapter adapter = getAdapter();
			if (adapter == null) {
				return;
			}
			
			Object data = adapter.getItem(position);
			
			mOnListItemSelectedListener.onListItemSelected(data);
		}
	}

	protected int getAdapterViewId() {
		return android.R.id.list;
	}

	@SuppressWarnings("unchecked")
    protected void bindAdapterView() {
		final Dialog dialog = getDialog();
		Logger.debug("dialog = %s", dialog);
		if (dialog == null) {
			return;
		}

		AdapterView<?> oldAdapterView = mAdapterView;

		if (oldAdapterView != null) {
			oldAdapterView.clearDisappearingChildren();
			oldAdapterView.clearAnimation();
			oldAdapterView.setAdapter(null);
			oldAdapterView.setOnItemClickListener(null);
			oldAdapterView.setVisibility(View.GONE);
			oldAdapterView.setEmptyView(null);
		}
		
		mAdapter = onCreateAdapter();
		
		mAdapterView = (AdapterView<BaseAdapter>) dialog.findViewById(
				getAdapterViewId());
		Logger.debug("adpater view = %s", mAdapterView);
		if (mAdapterView != null) {
			mAdapterView.setAdapter(mAdapter);
			mAdapterView.setOnItemClickListener(this);
			mAdapterView.setVisibility(View.VISIBLE);
			mAdapterView.scheduleLayoutAnimation();
			
			final View emptyView = dialog.findViewById(getEmptyViewId());
			if (emptyView != null) {
				mAdapterView.setEmptyView(emptyView);
			}
		}
	}
	
	public BaseAdapter getAdapter() {
		return mAdapter;
	}
	
	public AdapterView<BaseAdapter> getAdapterView() {
		return mAdapterView;
	}
	
 	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        if (activity instanceof OnListItemSelectedListener) {
        	mOnListItemSelectedListener = (OnListItemSelectedListener) activity;
        } else {
        	Logger.warn("host activity does not implements: %s",
        			OnListItemSelectedListener.class.getSimpleName());
        }
    }

 	@Override
 	public void onLoadFinished(Loader<ItemSet> loader, ItemSet data) {
 		bindData(mAdapter, data);
 	};
 	
 	@Override
 	public void onLoaderReset(Loader<ItemSet> loader) {
 		bindData(mAdapter, null);
 	};
 	
 	protected void removeCallbacks(Runnable r) {
 		mHandler.removeCallbacks(r);
 	}
 	
 	protected void post(Runnable r) {
 		mHandler.post(r);
 	}
 	
 	protected void postDelayed(Runnable r, long delayMillis) {
 		mHandler.postDelayed(r, delayMillis);
 	}
 	
	protected void notifyAdapterChanged() {
		post(notifyAdapterChangedRunnable);
	}
 	
	protected void notifyAdapterChangedOnIdle() {
		mDeferredHandler.postIdle(notifyAdapterChangedRunnable);
	}
 	
	abstract protected void bindData(BaseAdapter adapter, ItemSet data);

	abstract protected BaseAdapter onCreateAdapter();
	
	private Handler mHandler = new Handler();
	private DeferredHandler mDeferredHandler = new DeferredHandler();
	
	private Runnable notifyAdapterChangedRunnable = new Runnable() {
		
		@Override
		public void run() {
			if (mAdapter instanceof BaseAdapter == false) {
				return;
			}
			
			((BaseAdapter)mAdapter).notifyDataSetChanged();
		}
		
	};

}

@@ -604,20 +604,56 @@
 Listener
+, AdapterView.OnItemLongClickListener
  %7B%0A
-%09
 %0A%09public
@@ -768,16 +768,124 @@
 %0A    %7D%0A%0A
+%09public interface OnListItemHoldListener %7B%0A%0A        public boolean onListItemHold(Object itemData);%0A%0A    %7D%0A%0A
 %09private
@@ -1024,16 +1024,76 @@
 istener;
+%0A    private OnListItemHoldListener mOnListItemHoldListener;
 %0A%0A%09@Over
@@ -1568,16 +1568,380 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@Override%0A%09public boolean onItemLongClick(AdapterView%3C?%3E parent, View view, int position, long id) %7B%0A%09%09if (mOnListItemHoldListener != null) %7B%0A%09%09%09final BaseAdapter adapter = getAdapter();%0A%09%09%09if (adapter == null) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%0A%09%09%09Object data = adapter.getItem(position);%0A%0A%09%09%09return mOnListItemHoldListener.onListItemHold(data);%0A%09%09%7D%0A%0A%09%09return false;%0A%09%7D%0A%0A
 %09protect
@@ -2814,16 +2814,66 @@
 (this);%0A
+%09%09%09mAdapterView.setOnItemLongClickListener(this);%0A
 %09%09%09mAdap
@@ -3564,16 +3564,16 @@
 s: %25s%22,%0A
-
         
@@ -3635,16 +3635,286 @@
        %7D
+%0A%0A        if (activity instanceof OnListItemHoldListener) %7B%0A        %09mOnListItemHoldListener = (OnListItemHoldListener) activity;%0A        %7D else %7B%0A        %09Logger.warn(%22host activity does not implements: %25s%22,%0A%09%09%09%09%09OnListItemHoldListener.class.getSimpleName());%0A        %7D
 %0A    %7D%0A%0A

add long click listener tof AbsAdapterDialogFragment;