package cn.net.caoyao.cpzshandset.ui.dialog;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;

public class TipsDialog extends Dialog implements android.view.View.OnClickListener{
	private Button btn__tips_i_know, btn_dialog_tips_cancle, btn_dialog_tips_sure;
	private TextView tv_dialog_tips_title, tv_dialog_tips_content;
	private DSure dSure;
	private IKnow iKnow;

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

	public TipsDialog(Context context, int theme) {
		super(context, R.style.MyNoBgDialog);
	}

	public TipsDialog(Context context) {
		super(context,R.style.MyNoBgDialog);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initDialogStyle();
		setContentView(R.layout.dialog_tips_layout);
		initView();
		setListener();
	}
	
	private void setListener() {
		btn__tips_i_know.setOnClickListener(this);
		btn_dialog_tips_cancle.setOnClickListener(this);
		btn_dialog_tips_sure.setOnClickListener(this);
	}

	public void setTipsView(String title, String content,int position, boolean isIKonw, IKnow iKnow){
		this.iKnow = iKnow;
		if (isIKonw) {
			btn__tips_i_know.setVisibility(View.VISIBLE);
			btn_dialog_tips_cancle.setVisibility(View.GONE);
			btn_dialog_tips_sure.setVisibility(View.GONE);
		} else {
			btn__tips_i_know.setVisibility(View.GONE);
			btn_dialog_tips_cancle.setVisibility(View.VISIBLE);
			btn_dialog_tips_sure.setVisibility(View.VISIBLE);
		}
		
		if (tv_dialog_tips_title != null && title != null) {
			tv_dialog_tips_title.setText(title);
		}
		
		if (tv_dialog_tips_content != null) {
			tv_dialog_tips_content.setText(content);
		}
	}
	public void setTipsView(String title, String content,int position, boolean isIKonw, DSure dSure){
		this.dSure = dSure;
		if (isIKonw) {
			btn__tips_i_know.setVisibility(View.VISIBLE);
			btn_dialog_tips_cancle.setVisibility(View.GONE);
			btn_dialog_tips_sure.setVisibility(View.GONE);
		} else {
			btn__tips_i_know.setVisibility(View.GONE);
			btn_dialog_tips_cancle.setVisibility(View.VISIBLE);
			btn_dialog_tips_sure.setVisibility(View.VISIBLE);
		}
		
		if (tv_dialog_tips_title != null && title != null) {
			tv_dialog_tips_title.setText(title);
		}
		
		if (tv_dialog_tips_content != null) {
			tv_dialog_tips_content.setText(content);
		}
	}
	
	public void setDelWarnView(String title, String content,int position, boolean isIKonw, DSure dSure){
		this.dSure = dSure;
		if (isIKonw) {
			btn__tips_i_know.setVisibility(View.VISIBLE);
			btn_dialog_tips_cancle.setVisibility(View.GONE);
			btn_dialog_tips_sure.setVisibility(View.GONE);
		} else {
			btn__tips_i_know.setVisibility(View.GONE);
			btn_dialog_tips_cancle.setVisibility(View.VISIBLE);
			btn_dialog_tips_sure.setVisibility(View.VISIBLE);
		}
		
		if (tv_dialog_tips_title != null && title != null) {
			tv_dialog_tips_title.setText(title);
		}
		
		if (tv_dialog_tips_content != null) {
			tv_dialog_tips_content.setText(content);
		}
		if (tv_dialog_tips_title != null && title != null) {
			tv_dialog_tips_title.setTextColor(Color.RED);
			tv_dialog_tips_title.setText(title);
		}
	}
	
	public void setWarnView(String title, String content, boolean isIKonw, DSure dSure){
		this.dSure = dSure;
		if (isIKonw) {
			btn__tips_i_know.setVisibility(View.VISIBLE);
			btn_dialog_tips_cancle.setVisibility(View.GONE);
			btn_dialog_tips_sure.setVisibility(View.GONE);
		} else {
			btn__tips_i_know.setVisibility(View.GONE);
			btn_dialog_tips_cancle.setVisibility(View.VISIBLE);
			btn_dialog_tips_sure.setVisibility(View.VISIBLE);
		}
		
		if (tv_dialog_tips_title != null && title != null) {
			tv_dialog_tips_title.setTextColor(Color.RED);
			tv_dialog_tips_title.setText(title);
		}
		
		if (tv_dialog_tips_content != null) {
			tv_dialog_tips_content.setText(content);
		}
	}
	
	private void initView() {
		btn__tips_i_know = (Button) this.findViewById(R.id.btn__tips_i_know);
		btn_dialog_tips_cancle = (Button) this.findViewById(R.id.btn_dialog_tips_cancle);
		btn_dialog_tips_sure = (Button) this.findViewById(R.id.btn_dialog_tips_sure);
		tv_dialog_tips_title = (TextView) this.findViewById(R.id.tv_dialog_tips_title);
		tv_dialog_tips_content = (TextView) this.findViewById(R.id.tv_dialog_tips_content);		
	}

	private void initDialogStyle() {
		Window win = this.getWindow();
		win.setBackgroundDrawableResource(R.color.transparent);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
//		win.setWindowAnimations(R.style.dialogAnimation);
		// win.setWindowAnimations(R.style.CustomDialog);
		win.setGravity(Gravity.CENTER);
//		setFinishOnTouchOutside(false);//设置点击区域外不消失
		this.setCanceledOnTouchOutside(false);
	}
	
	@Override
	public void dismiss() {
		try {
			if (this != null && this.isShowing()) {
				super.dismiss();
			}
		} catch (Exception e) {
		}
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn__tips_i_know:
			this.dismiss();
			if (iKnow != null) {
				iKnow.iKnow();
			}
			break;
		case R.id.btn_dialog_tips_cancle:
			this.dismiss();
			break;
		case R.id.btn_dialog_tips_sure:
			if (dSure != null) {
				dSure.sureClick();
			}
			this.dismiss();
			break;
		}
	}
	public interface IKnow{
		void iKnow();
	}
	public interface DSure{
		void sureClick();
	}

}
