package athary.audio.ency.Helpers.data;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import athary.audio.ency.IntroActivity;
import athary.audio.ency.R;

/**
 * @author nguyendt
 * 
 */
public class IconContextMenu implements DialogInterface.OnCancelListener, 
										DialogInterface.OnDismissListener{

	private static final int LIST_PREFERED_HEIGHT = 65;
	
	private IconMenuAdapter menuAdapter = null;
	private Activity parentActivity = null;
	private int dialogId = 0;
	
	private IconContextMenuOnClickListener clickHandler = null;
	
	/**
	 * constructor
	 * @param parent
	 * @param id
	 */
	public IconContextMenu(Activity parent, int id) {
		this.parentActivity = parent;
		this.dialogId = id;
		
		menuAdapter = new IconMenuAdapter(parentActivity);
	}
	
	/**
	 * Add menu item
	 * @param menuItem
	 */
	public void addItem(Resources res, CharSequence title,
			 int actionTag) {
		menuAdapter.addItem(new IconContextMenuItem(res, title, actionTag));
	}
	
	public void addItem(Resources res, int textResourceId,
			int imageResourceId, int actionTag) {
		menuAdapter.addItem(new IconContextMenuItem(res, textResourceId, actionTag));
	}
	
	/**
	 * Set menu onclick listener
	 * @param listener
	 */
	public void setOnClickListener(IconContextMenuOnClickListener listener) {
		clickHandler = listener;
	}
	
	/**
	 * Create menu
	 * @return
	 */

	private class StyleAlertDialog extends AlertDialog {
	    protected StyleAlertDialog(Context context, int theme) {
	        super(context, theme);
	    }
	}

	public Dialog createMenu(String menuItitle) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
        StyleAlertDialog adb = new StyleAlertDialog(parentActivity, R.style.Theme_Dialog_Translucent);
        adb.setView(parentActivity.getLayoutInflater().inflate(R.layout.alert_title, null));

        builder.setCustomTitle(adb.getCurrentFocus());
        builder.setAdapter(menuAdapter, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialoginterface, int i) {
				IconContextMenuItem item = (IconContextMenuItem) menuAdapter.getItem(i);
				
				if (clickHandler != null) {
					clickHandler.onClick(item.actionTag);
				}
			}
		});

        builder.setInverseBackgroundForced(true);

        AlertDialog dialog = builder.create();
        dialog.setOnCancelListener(this);
        dialog.setOnDismissListener(this);
        return dialog;
	}
	
	public void onCancel(DialogInterface dialog) {
        cleanup();
    }

    public void onDismiss(DialogInterface dialog) {
    }
   
    private void cleanup() {
    	parentActivity.dismissDialog(dialogId);
    }
	
    /**
     * IconContextMenu On Click Listener interface
     */
    public interface IconContextMenuOnClickListener {
		public abstract void onClick(int menuId);
    }
    
	/**
	 * Menu-like list adapter with icon
	 */
    protected class IconMenuAdapter extends BaseAdapter {
		private Context context = null;
		
	    private ArrayList<IconContextMenuItem> mItems = new ArrayList<IconContextMenuItem>();
		
	    public IconMenuAdapter(Context context) {
	    	this.context = context;
	    }
	    
	    /**
	     * add item to adapter
	     * @param menuItem
	     */
	    public void addItem(IconContextMenuItem menuItem) {
	    	mItems.add(menuItem);
	    }
	    
		@Override
		public int getCount() {
			return mItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			IconContextMenuItem item = (IconContextMenuItem) getItem(position);
			return item.actionTag;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			IconContextMenuItem item = (IconContextMenuItem) getItem(position);
			
			Resources res = parentActivity.getResources();
			
			if (convertView == null) {
	        	TextView temp = new TextView(context);
	        	AbsListView.LayoutParams param = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, 
	        																  AbsListView.LayoutParams.WRAP_CONTENT);
	        	temp.setLayoutParams(param);
	        	temp.setBackgroundResource(R.drawable.custom_style_nocorners);
	        	temp.setPadding((int)toPixel(res, 15), 0, (int)toPixel(res, 15), 0);
	        	temp.setGravity(android.view.Gravity.CENTER_VERTICAL);
	        	temp.setTypeface(IntroActivity.tf, Typeface.NORMAL);
	        	temp.setTextSize(TypedValue.COMPLEX_UNIT_SP, IntroActivity.fontsize);
	        	
	        	temp.setMinHeight(LIST_PREFERED_HEIGHT);
	        	temp.setCompoundDrawablePadding((int)toPixel(res, 14));
	        	convertView = temp;
			}
			
			TextView textView = (TextView) convertView;
			textView.setTag(item);
			textView.setBackgroundResource(R.drawable.custom_style_nocorners);
			textView.setText(item.text);
			textView.setTextColor(Color.argb(255, 134, 121, 81));
			textView.setGravity(Gravity.CENTER);
			textView.setTypeface(IntroActivity.tf, Typeface.NORMAL);
			textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, IntroActivity.fontsize);

        	
	        return textView;
		}
		
		private float toPixel(Resources res, int dip) {
			float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, res.getDisplayMetrics());
			return px;
		}
	}

	/**
	 * menu-like list item with icon
	 */
    protected class IconContextMenuItem {
		public final CharSequence text;
		public final int actionTag;

		/**
		 * public constructor
		 * 
		 * @param res
		 *            resource handler
		 * @param textResourceId
		 *            id of title in resource
		 * @param imageResourceId
		 *            id of icon in resource
		 * @param actionTag
		 *            indicate action of menu item
		 */
		public IconContextMenuItem(Resources res, int textResourceId,
				int actionTag) {
			text = res.getString(textResourceId);
			this.actionTag = actionTag;
		}

		/**
		 * public constructor
		 * @param res
		 * 			resource handler
		 * @param title
		 * 			menu item title
		 * @param imageResourceId
		 * 			id of icon in resource
		 * @param actionTag
		 * 			indicate action of menu item
		 */
		public IconContextMenuItem(Resources res, CharSequence title,
				int actionTag) {
			text = title;
			this.actionTag = actionTag;
		}
	}
}
