package com.wasu.cs.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import cn.com.wasu.main.R;

/**
 * 通用dialog
 * @author zhangrm@wasu.com
 */
public class WAlertDialog extends Dialog {
	private static final String TAG = WAlertDialog.class.getSimpleName();
	//标题和内容
	private TextView title, message;
	//确定和取消按钮
	private Button btn1, btn2;

	public WAlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	public WAlertDialog(Context context, int theme) {
		super(context, theme);
		init(context);
	}

	public WAlertDialog(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.alert_dialog_normal);
		title = (TextView) findViewById(R.id.alertTitle);
		message = (TextView) findViewById(R.id.message);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
	}

	@Override
	public void setTitle(CharSequence title) {
		setTitleMsg(title);
	}

	private void setTitleMsg(CharSequence msg) {
		title.setText(msg);
	}

	public void setMessage(CharSequence msg) {
		if (msg == null) {
			return;
		}
		message.setMovementMethod(ScrollingMovementMethod.getInstance());
		message.setText(Html.fromHtml(msg.toString()));
	}

	public void setPositiveButton(String text, final View.OnClickListener listener) {
		btn1.setVisibility(View.VISIBLE);
		btn1.setText(text);
		btn1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onClick(v);
				}
				dismiss();
			}
		});
	}

	public void setNegativeButton(String text, final View.OnClickListener listener) {
		btn2.setVisibility(View.VISIBLE);
		btn2.setText(text);
		btn2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onClick(v);
				}
				dismiss();
			}
		});
	}

	@Override
	public void show() {
		super.show();
		btn1.requestFocus();
	}

}
