package com.util;

import com.chaoran.entiry.SelfEditText;
import com.activity.chaoran.DjActivity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;

public class DialogUtil {
	// 错误消息对话框
	public static void builder(final Context context, String title, String message,
			int fontSize) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		SpannableStringBuilder ssBuilser = new SpannableStringBuilder(message);
		builder.setTitle(title);
		if (fontSize > 0) {
			AbsoluteSizeSpan span = new AbsoluteSizeSpan(fontSize);
			ssBuilser.setSpan(span, 0, message.length(),
					Spannable.SPAN_INCLUSIVE_INCLUSIVE);
			builder.setMessage(ssBuilser);
		} else {
			builder.setMessage(message);
		}
		builder.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (DjActivity.cr5wScanControl != null && "DjActivity".equals(context.getClass().getSimpleName())) {
					DjActivity.cr5wScanControl.setIsOpen(true);
					DjActivity.cr5wScanControl.start(context);
				} else if (DjActivity.nr510ScanControl != null && "DjActivity".equals(context.getClass().getSimpleName())) {
					DjActivity.nr510ScanControl.start(context);
				} else if (DjActivity.barcodeReader != null  && "DjActivity".equals(context.getClass().getSimpleName())) {
					try {
						DjActivity.barcodeReader.light(true);      //turn on/off backlight
						DjActivity.barcodeReader.aim(true);		//开关瞄准线
						DjActivity.barcodeReader.decode(true);	    //开关解码功能
					} catch (Exception ex) {
						 ex.printStackTrace();
					}
				}
				if (DjActivity.m_view != null) {
					if (DjActivity.m_view instanceof SelfEditText) {
						((SelfEditText)DjActivity.m_view).selectAll();
					}
					DjActivity.m_view = null;
				}
			}
		});
		builder.create();
		builder.show();

//		if (DjActivity.cr5wScanControl != null  && "DjActivity".equals(context.getClass().getSimpleName()))
//			DjActivity.cr5wScanControl.setIsOpen(false);
	}

	public static void builderCancel(Context context, String title,
			String message, OnClickListener okListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("确定", okListener);
		builder.setNegativeButton("取消", null);
		builder.create();
		builder.show();
	}

	public static void setDialog(ProgressDialog pd, String title, String message) {
		pd.setTitle(title);
		pd.setMessage(message);
		pd.show();
	}
}
