package com.qhclh.ytzh.utils.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qhclh.ytzh.R;

public class MixedDialog extends Dialog implements View.OnClickListener
{
	public static final int SINGLE = 1;
	public static final int MULTI = 2;
	public static final int ALERT = 3;
	public static final int SHARE = 4;

	private TextView mTitle;
	public LinearLayout text1_ll;
	private TextView nameTextView;
	public EditText importEditText;
	
	private TextView name2TextView;
	public EditText import2EditText;
	
	private TextView name3TextView;
	public EditText import3EditText;

	public ImageButton buttonCancel;
	public Button buttonOK;
	public Button wechat_btn ;
	public Button cancel_btn;
	
	private Context mContext;

	public MixedDialog(Context context, int mode, int style)
	{
		super(context, style);
		
		mContext = context;
		switch (mode)
		{

		case SINGLE:
			setContentView(R.layout.dialog_basic_data_single);
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

			initSingleImportDialogView();
			break;

		case MULTI:
			setContentView(R.layout.dialog_basic_data_multi);
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
			initMultiImportDialogView();
			break;

		case ALERT:
			setContentView(R.layout.dialog_basic_data_alert);
			initAlertImportDialogView();
			break;	
			
		case SHARE:
			setContentView(R.layout.dialog_share_message);
			initShareImportDialogView();	
			
		default:
			break;
		}

	}

	private void initShareImportDialogView() {
		mTitle = (TextView) findViewById(R.id.tv_dlg_share_title);
		importEditText = (EditText) findViewById(R.id.et_dlg_share_import);
		
		buttonCancel = (ImageButton) findViewById(R.id.imgbtn_dlg_share_cancel);
		buttonCancel.setOnClickListener(this);

		buttonCancel = (ImageButton) findViewById(R.id.imgbtn_dlg_share_cancel);
		buttonCancel.setOnClickListener(this);
		
		Button msg_btn = (Button) findViewById(R.id.btn_dlg_msg);
		msg_btn.setOnClickListener(this);
		
		wechat_btn = (Button) findViewById(R.id.btn_dlg_wechat);
		wechat_btn.setOnClickListener(this);
	}

	private void initSingleImportDialogView()
	{
		mTitle = (TextView) findViewById(R.id.tv_dlg_single_title);
		nameTextView = (TextView) findViewById(R.id.tv_dlg_single_name);
		importEditText = (EditText) findViewById(R.id.et_dlg_single_import);

		buttonCancel = (ImageButton) findViewById(R.id.imgbtn_dlg_single_cancel);
		buttonCancel.setOnClickListener(this);

		buttonOK = (Button) findViewById(R.id.btn_dlg_single_ok);
		buttonOK.setOnClickListener(this);
	}

	private void initMultiImportDialogView()
	{
		text1_ll = (LinearLayout) findViewById(R.id.text1_ll);
		mTitle = (TextView) findViewById(R.id.tv_dlg_multi_title);
		nameTextView = (TextView) findViewById(R.id.tv_dlg_multi_name1);
		importEditText = (EditText) findViewById(R.id.et_dlg_multi_import1);
		
		name2TextView = (TextView) findViewById(R.id.tv_dlg_multi_name2);
		import2EditText = (EditText) findViewById(R.id.et_dlg_multi_import2);
		
		name3TextView = (TextView) findViewById(R.id.tv_dlg_multi_name3);
		import3EditText = (EditText) findViewById(R.id.et_dlg_multi_import3);

		buttonCancel = (ImageButton) findViewById(R.id.imgbtn_dlg_multi_cancel);
		buttonCancel.setOnClickListener(this);

		buttonOK = (Button) findViewById(R.id.btn_dlg_multi_ok);
		buttonOK.setOnClickListener(this);
	}
	
	private void initAlertImportDialogView() {
		mTitle = (TextView) findViewById(R.id.tv_dlg_multi_title);
		nameTextView = (TextView) findViewById(R.id.tv_dlg_multi_name1);
		
		buttonOK = (Button) findViewById(R.id.btn_dlg_multi_ok);
		buttonOK.setOnClickListener(this);
		
		cancel_btn = (Button) findViewById(R.id.btn_dlg_alert_cancel);
		cancel_btn.setOnClickListener(this);
	}
	
	public void setTitleText(String titleString)
	{
		mTitle.setText(titleString);

	}
	
	public void setNameText(String nameString)
	{
		nameTextView.setText(nameString);

	}
	
	public void setName2Text(String nameString)
	{
		name2TextView.setText(nameString);

	}
	
	public void setName3Text(String nameString)
	{
		name3TextView.setText(nameString);

	}
	
	@Override
	public void onClick(View v)
	{
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);  
		switch (v.getId())
		{
		case R.id.imgbtn_dlg_single_cancel:
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); //强制隐藏键盘  
			cancel();
			break;
			
		case R.id.imgbtn_dlg_multi_cancel:
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); //强制隐藏键盘  
			cancel();
			break;
			
		case R.id.btn_dlg_alert_cancel:
			cancel();
			break;
		
		case R.id.imgbtn_dlg_share_cancel:
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); //强制隐藏键盘  
			cancel();
			break;

		default:
			break;
		}	
		
	}


	/**
	 * 系统对话框
	 *
	 */
	public static void showDialog(Activity context, String[] items, String title,
								  DialogInterface.OnClickListener onClickListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		if (!android.text.TextUtils.isEmpty(title)) {
			builder.setTitle(title);
		}
		builder.setItems(items, onClickListener).create().show();
	}
	public static void showDialog(Activity context, String[] items,
								  DialogInterface.OnClickListener onClickListener) {
		showDialog(context, items, null, onClickListener);
	}
	public interface OnClickListener {
		void onPositiveButtonClick();
	}


	public static void showAlertDialog(Activity context, @Nullable CharSequence message, final OnClickListener onClickListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message)
				.setTitle("提示")
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (onClickListener != null) {
							onClickListener.onPositiveButtonClick();
						}
						dialog.dismiss();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				})
				.show();
	}


}
