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


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

public class OperationDialog extends Dialog implements android.view.View.OnClickListener{
	private View btn_opreate_dialog_del, btn_opreate_dialog_modify, btn_opreate_dialog_cancle,btn_opreate_dialog_search_label,view_place_blank;
	private DClick dClick;
	private Context context;

	public OperationDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		this.context = context;
	}

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

	public OperationDialog(Context context) {
		super(context,R.style.MyNoBgDialog);
		this.context = context;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_operation_layout);
		initDialogStyle();
		initView();
		setListener();
	}
	/**
	 * 显示搜索按钮
	 */
	public void setSearchViewVisiable(int visibility){
		btn_opreate_dialog_search_label.setVisibility(visibility);
		view_place_blank.setVisibility(visibility);
	}
	
	public void setModifyViewVisiable(int visibility){
		btn_opreate_dialog_modify.setVisibility(visibility);
	}
	public void setDelViewVisiable(int visibility){
		btn_opreate_dialog_del.setVisibility(visibility);
	}
	
	
	
	private void setListener() {
		btn_opreate_dialog_del.setOnClickListener(this);
		btn_opreate_dialog_modify.setOnClickListener(this);
		btn_opreate_dialog_cancle.setOnClickListener(this);
		btn_opreate_dialog_search_label.setOnClickListener(this);
	}

	public void setDClick(int position, DClick dClick){
		this.dClick = dClick;
	}
	
	private void initView() {
		btn_opreate_dialog_del = findViewById(R.id.btn_opreate_dialog_del);
		btn_opreate_dialog_modify = findViewById(R.id.btn_opreate_dialog_modify);
		btn_opreate_dialog_cancle = findViewById(R.id.btn_opreate_dialog_cancle);
		btn_opreate_dialog_search_label = findViewById(R.id.btn_opreate_dialog_search_label);
		view_place_blank = findViewById(R.id.view_place_blank);
	}

	private void initDialogStyle() {
		Window win = this.getWindow();
		win.setBackgroundDrawableResource(R.color.transparent);
//		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//		int width = metrics.widthPixels;
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
		win.setWindowAnimations(R.style.dialog_bottom_to_top_style);
		win.setGravity(Gravity.BOTTOM);
//		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_opreate_dialog_search_label:
			if (dClick != null) {
				dClick.searckClick();
			}
			this.dismiss();
			break;
		case R.id.btn_opreate_dialog_cancle:
			this.dismiss();
			break;
		case R.id.btn_opreate_dialog_del:
			if (dClick != null) {
				dClick.delClick();
			}
			this.dismiss();
			break;
		case R.id.btn_opreate_dialog_modify:
			if (dClick != null) {
				dClick.modifyClick();
			}
			this.dismiss();
			break;
		}
	}
	
	public interface DClick{
		void delClick();
		void modifyClick();
		void searckClick();
	}

}
