package com.wasu.cs.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.wasu.main.R;

import static cn.com.wasu.main.R.style.dialog;

/**
 * 统一错误对话框
 * 
 * @author yuanjr@wasu.com
 * 
 */
public class ErrorDialog extends Dialog {

	private Context mContext;
	/**
	 * 错误对话框视图
	 */
	protected View view;
	/**
	 * 报错图片
	 * */
	protected ImageView imgView;
	protected int feedBackPos = -1;
	/**
	 * 错误对话框的消息内容的控件，永远获焦的自定义的textview
	 */
	protected AlwaysMarqueeTextView tvContent;
	/**
	 * 错误对话框的消息头的控件
	 */
	protected TextView tvTitle;
	/**
	 * 焦点位置，最右边的位置为1，从右 往左依次递增
	 */
	protected int pos = 1;
	/**
	 * 用户添加的按钮数量，最多不能超过3个
	 */
	protected int btnNum;
	/**
	 * 用户添加消息头和消息内容
	 */
	protected String title, message;
	/**
	 * 用户添加按钮控件
	 */
	protected TextView btn1, btn2, btn3;

	/**
	 * 用户添加按钮的名称
	 */
	protected String btnName1, btnName2, btnName3;
	/**
	 * 按钮点击的业务实现接口
	 */
	protected DialogInterface.OnClickListener OnBtn1ClickListener,
			OnBtn2ClickListener, OnBtn3ClickListener;

	/**
	 * 统一错误对话框
	 * 
	 * @param 上下文环境
	 *            （context）
	 *            ,按钮个数（btnNum）,第一个按钮的名称（btnName1）,第二个按钮的名称（btnName2）,第三个按钮的名称
	 *            （btnName3），消息头(title)，消息内容(content) （按钮是从右往左依次递增）
	 * 
	 * @return null
	 */
	protected ErrorDialog(Context context, int btnNum, String btnName1,
			String btnName2, String btnName3, String title, String message) {
		super(context, R.style.error_dialog);
		this.mContext = context;
		this.btnNum = btnNum;
		this.title = title;
		this.message = message;
		this.btnName1 = btnName1;
		this.btnName2 = btnName2;
		this.btnName3 = btnName3;
		initView();
		setTitle(title);
		setMessage(message);
		setBtnView();

	}

	protected ErrorDialog(Context context) {
		super(context, R.style.error_dialog);
		this.mContext = context;
		initView();

	}

	public void feedBack(int pos){
		feedBackPos = pos;
		imgView.setImageResource(R.drawable.err_dialog_content_rec);
	}

	public static class mBuilder {
		private ErrorDialog errorDialog;

		public mBuilder(Context context) {

			errorDialog = new ErrorDialog(context);
		}

		public mBuilder setTitle(String title) {
			errorDialog.setTitle(title);
			return this;
		}

		public mBuilder setMessage(String message) {
			errorDialog.setMessage(message);
			return this;
		}

		public mBuilder addBtn1(String name, OnClickListener OnClickListener) {
			errorDialog.btnNum = 1;
			errorDialog.setFocus(errorDialog.pos);
			errorDialog.btn1.setVisibility(View.VISIBLE);
			errorDialog.btn2.setVisibility(View.GONE);
			errorDialog.btn3.setVisibility(View.GONE);
			errorDialog.btn1.setText(name);
			errorDialog.setOnBtn1ClickListener(OnClickListener);
			return this;
		}

		public mBuilder addBtn2(String name, OnClickListener OnClickListener) {
			errorDialog.btnNum = 2;
			errorDialog.btn1.setVisibility(View.VISIBLE);
			errorDialog.btn2.setVisibility(View.VISIBLE);
			errorDialog.btn3.setVisibility(View.GONE);
			errorDialog.btn2.setText(name);
			errorDialog.setOnBtn2ClickListener(OnClickListener);
			return this;
		}

		public mBuilder addBtn3(String name, OnClickListener OnClickListener) {
			errorDialog.btnNum = 3;
			errorDialog.btn1.setVisibility(View.VISIBLE);
			errorDialog.btn2.setVisibility(View.VISIBLE);
			errorDialog.btn3.setVisibility(View.VISIBLE);
			errorDialog.btn3.setText(name);
			errorDialog.setOnBtn3ClickListener(OnClickListener);
			return this;
		}

		public ErrorDialog create() {

			return errorDialog;
		}

	}

	protected void initView() {
		LayoutInflater layoutInflater = getLayoutInflater();
		view = layoutInflater.inflate(R.layout.dialog_error, null);
		imgView = (ImageView) view.findViewById(R.id.ivContent);
		tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		tvContent = (AlwaysMarqueeTextView) view.findViewById(R.id.tvContent);
		btn1 = (TextView) view.findViewById(R.id.btn1);
		btn2 = (TextView) view.findViewById(R.id.btn2);
		btn3 = (TextView) view.findViewById(R.id.btn3);

//		WindowManager.LayoutParams lp=this.getWindow().getAttributes();
//		lp.alpha=1.0f;
//		this.getWindow().setAttributes(lp);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(view);

	}

	protected void setFocus(int pos) {

		float textSizeBig = mContext.getResources().getDimensionPixelSize(
				R.dimen.s_32sp);
		float textSizeSmall = mContext.getResources().getDimensionPixelSize(
				R.dimen.s_24sp);

		switch (pos) {
		case 1:
			btn1.requestFocus();
			btn1.setSelected(true);
//			btn1.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeBig);
			btn2.setSelected(false);
//			btn2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeSmall);
			btn3.setSelected(false);
//			btn3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeSmall);
			break;
		case 2:
			btn2.requestFocus();
			btn2.setSelected(true);
//			btn2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeBig);
			btn1.setSelected(false);
//			btn1.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeSmall);
			btn3.setSelected(false);
//			btn3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeSmall);
			break;
		case 3:
			btn3.requestFocus();
			btn3.setSelected(true);
//			btn3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeBig);
			btn2.setSelected(false);
//			btn2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeSmall);
			btn1.setSelected(false);
//			btn1.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeSmall);
			break;

		default:
			break;
		}
	}

	protected void setBtnView() {
		setFocus(pos);
		switch (btnNum) {
		case 0:
			btn1.setVisibility(View.GONE);
			btn2.setVisibility(View.GONE);
			btn3.setVisibility(View.GONE);
			break;
		case 1:
			btn1.setVisibility(View.VISIBLE);
			btn2.setVisibility(View.GONE);
			btn3.setVisibility(View.GONE);
			btn1.setText(btnName1);
			break;
		case 2:
			btn1.setVisibility(View.VISIBLE);
			btn2.setVisibility(View.VISIBLE);
			btn3.setVisibility(View.GONE);
			btn1.setText(btnName1);
			btn2.setText(btnName2);
			break;
		case 3:
			btn1.setVisibility(View.VISIBLE);
			btn2.setVisibility(View.VISIBLE);
			btn3.setVisibility(View.VISIBLE);
			btn1.setText(btnName1);
			btn2.setText(btnName2);
			btn3.setText(btnName3);
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:

			if (pos < btnNum) {
				pos++;
				setFocus(pos);
			}
			return true;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			if (pos > 1) {
				pos--;
				setFocus(pos);
			}
			return true;

		case KeyEvent.KEYCODE_DPAD_CENTER:
		case KeyEvent.KEYCODE_ENTER:

			return doBtn();
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	protected void setOnBtn1ClickListener(
			DialogInterface.OnClickListener OnClickListener) {
		this.OnBtn1ClickListener = OnClickListener;
	}

	protected void setOnBtn2ClickListener(
			DialogInterface.OnClickListener OnClickListener) {
		this.OnBtn2ClickListener = OnClickListener;
	}

	protected void setOnBtn3ClickListener(
			DialogInterface.OnClickListener OnClickListener) {
		this.OnBtn3ClickListener = OnClickListener;
	}

	protected boolean doBtn() {

		if (pos == 1) {
			if (OnBtn1ClickListener != null) {
				OnBtn1ClickListener.onClick(ErrorDialog.this, 1);
			}
			if (feedBackPos != pos){
				dismiss();
			}
		} else if (pos == 2) {
			if (OnBtn2ClickListener != null) {
				OnBtn2ClickListener.onClick(ErrorDialog.this, 2);
			}
			if (feedBackPos != pos){
				dismiss();
			}
		} else if (pos == 3) {

			if (OnBtn3ClickListener != null) {
				OnBtn3ClickListener.onClick(ErrorDialog.this, 3);
			}
			if (feedBackPos != pos){
				dismiss();
			}
		}

		return true;
	}

	protected void setBtnNum(int btnNum) {
		this.btnNum = btnNum;
		setBtnView();
	}

	protected void setBtnName1(String btnName1) {
		this.btnName1 = btnName1;
		btn1.setText(btnName1);
	}

	protected void setBtnName2(String btnName2) {
		this.btnName2 = btnName2;
		btn2.setText(btnName2 + "");
	}

	protected void setBtnName3(String btnName3) {
		this.btnName3 = btnName3;
		btn3.setText(btnName3);
	}

	protected void setTitle(String title) {
		this.title = title;
		tvTitle.setText(title);
	}

	protected void setMessage(String message) {
		this.message = message;
		tvContent.setText(message);

	}
}
