
package BWP.MobileSale.Util;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Callable;
 
import BWP.MobileSale.R; 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface; 
import android.os.Handler; 
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout; 
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.view.LayoutInflater;

public class DialogUtil {
	
	
	
	abstract static class ProgressDialogHelper {
		Context mContext;
		String mMessage;
		Callable<?> mRunnable;
		Handler mHandler = new Handler();
		
		Callable<?> mCallableAfterSuccess;
		
		public void setCallableAfterSuccess(Callable<?> callable){
			mCallableAfterSuccess = callable;
		}
		
		public ProgressDialogHelper(Context context,String message,Callable<?> runnable){
			this.mContext = context;
			this.mMessage = message;
			this.mRunnable = runnable;
		}
		
		ProgressDialog mDialog;
		
		public void Start(){
			mDialog = ProgressDialog.show(mContext, "请稍等", mMessage, true);
			Thread thread = new Thread(){
				@Override
				public void run(){
					try{
						mRunnable.call();
					}
					catch(Exception ex){
						ex.printStackTrace();
						Runnable runnable = new Runnable(){

							public void run() {
								DialogUtil.Alert(mContext, mMessage);
								
							}
							
							String mMessage;
							public Runnable setMessage(String message){
								mMessage = message;
								return this;
							}
							
						}.setMessage(ex.getMessage());
						mHandler.post(runnable);
					}
					finally{
						mDialog.dismiss();
					}
				}
				
			};
			thread.start();
		}
	}
	
	
	public static void Progress(Context context,String message,Callable<?> callable){
		new ProgressDialogHelper(context, message, callable){	
		}
		.Start();
	}
	
	public static void Progress(Context context,String message,Callable<?> callable,Callable<?> callableAfterSuccess){
		ProgressDialogHelper helper = new ProgressDialogHelper(context, message, callable){	
		};
		
		helper.Start();

	}
	
	
	public  static void Alert(Context context, String message) {
		new AlertDialog.Builder(context).setMessage(message)
				.setPositiveButton("确定", null).show();
	}
	
	public  static void AlertAndFinish(final Activity activity, String message) {
		new AlertDialog.Builder(activity).setMessage(message)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						activity.finish();
					}
					
				}).show();
	}
	

	public static void Alert(View view, String message) {
		Alert(view.getContext(), message);
	}

	public static void AutoHiddenAlert(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	public static void AutoHiddenAlert(View view, String message) {
		AutoHiddenAlert(view.getContext(), message);
	}
	
	public interface SetDate{
		void setDate(Date date);
	}
	
	public static void AddSelectDateDialog(TextView arg0,final SetDate onsetdate) {
		arg0.setOnClickListener(new OnClickListener(){

			public void onClick(final View view) {
				final TextView textView = (TextView) view;
				OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener(){

					public void onDateSet(DatePicker datePicker, int year, int month,
							int day) {
						Calendar calendar = Calendar.getInstance();
						calendar.set(year, month, day);
						textView.setText(DateTimeUtil.FormatDate(calendar));
						onsetdate.setDate(calendar.getTime());
					}
					
				};
				
				String dateStr = textView.getText().toString();
				Calendar calendar = DateTimeUtil.SafeParseCalendar(dateStr);
				
				new DatePickerDialog(
						view.getContext(), 
						onDateSetListener, 
						calendar.get(Calendar.YEAR),
						calendar.get(Calendar.MONTH),
						calendar.get(Calendar.DAY_OF_MONTH)).show();
			}
			
		});
	}
	
	
	public static void Prompt(Context context,String title,String message,String inputValue,int inputType, final OnPromptListener onprompt){
		final EditText input = new EditText(context);
		input.setText(inputValue);
		input.setInputType(inputType);
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title)
			.setMessage(message)
			.setView(input)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					String prompt = input.getText().toString();
					onprompt.OnPrompt(prompt);
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
				}
			}).show();
		
	}
	
	public final class BatchNoSelect implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			
			
			
		}
	}

	public static void PromptWithTwoInput(final Context context,final String title,String message,HashMap<String,Object> lastData,final boolean isOrNotShowSecond,boolean isOrNotShowPolicyPrice,boolean hasEditPriceRole, final OnPromptListener onprompt){
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		final View view;
	    
		view = inflater.inflate(R.layout.edittext, null);
		TextView goodsPolicyPrice = (TextView)view.findViewById(R.id.goodsPolicyPrice);
		if(!hasEditPriceRole){
			TableRow row = (TableRow)view.findViewById(R.id.tableRow2);
			row.setVisibility(view.GONE);
		}
		if(isOrNotShowPolicyPrice){
			BigDecimal PolicyPrice = (BigDecimal)lastData.get("PolicyPrice");
			if(PolicyPrice.compareTo(new BigDecimal(0)) == 1){
				goodsPolicyPrice.setText(PolicyPrice.toString());
			}
		}else{
			TableRow row = (TableRow)view.findViewById(R.id.tableRow3);
			row.setVisibility(view.GONE);
		}
		
		
		final EditText goodsNum = (EditText)view.findViewById(R.id.goodsNum);
		goodsNum.setText(StringUtil.ToString(lastData.get("Number")));
		final EditText goodsSecondNum = (EditText)view.findViewById(R.id.goodsSecondNum);
	
		if(title != "请输入数量"){
			EditText goodsPrice = (EditText)view.findViewById(R.id.goodsPrice);
			goodsPrice.setText(StringUtil.ToString(lastData.get("Price")));
		}
		if(isOrNotShowSecond){
			goodsSecondNum.setText(StringUtil.ToString(lastData.get("SecondNumber")));
			final Object leftRatio =  lastData.get("LeftRatio");
			final Object rightRatio =  lastData.get("RightRatio");
			final Object goods_SecondaryUnit1RelativeRatio =  lastData.get("Goods_SecondaryUnit1RelativeRatio");
			final Object goods_SecondaryUnit1Ratio =  lastData.get("Goods_SecondaryUnit1Ratio");
			if(leftRatio!=null&&rightRatio!=null&&goods_SecondaryUnit1RelativeRatio!=null&&goods_SecondaryUnit1Ratio!=null){
			final BigDecimal rightRatio2 =  (BigDecimal)rightRatio;
			final BigDecimal leftRatio2 =  (BigDecimal)leftRatio;
			final BigDecimal goods_SecondaryUnit1RelativeRatio2 =  (BigDecimal)goods_SecondaryUnit1RelativeRatio;
			final BigDecimal goods_SecondaryUnit1Ratio2 =  (BigDecimal)goods_SecondaryUnit1Ratio;
			goodsNum.addTextChangedListener(
					new TextWatcher() {
						
						@Override
						public void onTextChanged(CharSequence s, int start, int before, int count) {
							if(goodsNum.hasFocus()){
							BigDecimal number =  StringUtil.ParseMoney(s.toString());
							
							if(number!=null&&goods_SecondaryUnit1Ratio2!=null&&goods_SecondaryUnit1RelativeRatio2!=null){
								BigDecimal mainUnitNum = ((number).divide(leftRatio2,6,BigDecimal.ROUND_HALF_EVEN)).multiply(rightRatio2);
								BigDecimal secondNumber = (mainUnitNum.multiply(goods_SecondaryUnit1RelativeRatio2)).divide(goods_SecondaryUnit1Ratio2,6,BigDecimal.ROUND_HALF_EVEN);
								goodsSecondNum.setText(secondNumber.toString());
								
							}
							}
							
						}
						
						@Override
						public void beforeTextChanged(CharSequence s, int start, int count,
								int after) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void afterTextChanged(Editable s) {
							
							
						}
					}
					);
			
			goodsSecondNum.addTextChangedListener(
					new TextWatcher() {
						
						@Override
						public void onTextChanged(CharSequence s, int start, int before, int count) {
							if(goodsSecondNum.hasFocus()){
							BigDecimal secondNumber =  StringUtil.ParseMoney(s.toString());
							if(secondNumber!=null&&goods_SecondaryUnit1Ratio2!=null&&goods_SecondaryUnit1RelativeRatio!=null){
								BigDecimal mainUnitNum = ((secondNumber).multiply(goods_SecondaryUnit1Ratio2)).divide(goods_SecondaryUnit1RelativeRatio2);
								BigDecimal number = (mainUnitNum.divide(rightRatio2)).multiply(leftRatio2);
								goodsNum.setText(number.toString());
							}
							}
						}
						
						@Override
						public void beforeTextChanged(CharSequence s, int start, int count,
								int after) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void afterTextChanged(Editable s) {
							
							
						}
					}
					);
		}
		}else {
			goodsSecondNum.setVisibility(view.GONE);
			TextView textView_Second = (TextView)view.findViewById(R.id.TextView_SecondNum);
			textView_Second.setVisibility(view.GONE);
		}
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title)
			.setMessage(message)
			.setView(view)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					EditText goodsNum = (EditText)view.findViewById(R.id.goodsNum);
					EditText goodsSecondNum = (EditText)view.findViewById(R.id.goodsSecondNum);
					HashMap<String, Object> returnlist = new HashMap<String, Object>();
					if(title != "请输入数量"){
					EditText goodsPrice = (EditText)view.findViewById(R.id.goodsPrice);
					returnlist.put("goodsNum",StringUtil.ParseMoney( goodsNum.getText().toString()));
					returnlist.put("goodsPrice", StringUtil.ParseMoney(goodsPrice.getText().toString()));
					if(isOrNotShowSecond){
					returnlist.put("goodsSecondNum", StringUtil.ParseMoney( goodsSecondNum.getText().toString()));
					}
					onprompt.OnPrompt2(returnlist);
					}else{
						returnlist.put("goodsNum", StringUtil.ParseMoney(goodsNum.getText().toString()));
						if(isOrNotShowSecond){
						returnlist.put("goodsSecondNum", StringUtil.ParseMoney( goodsSecondNum.getText().toString()));
						}
						onprompt.OnPrompt2(returnlist);	
				}
			}})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
				}
			}).show();
		
	}
	
	public static void Confirm(Context context,String title,String trueString,String falseString,DialogInterface.OnClickListener onConfirm){
		new AlertDialog.Builder(context)
		.setTitle(title)
		.setPositiveButton(trueString, onConfirm)
		.setNegativeButton(falseString, null).show();
	}
	
	static String hadletype="";
	public static void PromptGoods(Context context, String title, String num,
			 String hadleType, final OnPromptGoodsListener onprompt) throws Exception {

		hadletype=hadleType;
		
		String message = "输入数字类型字符";
		int inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL;
		LinearLayout linearLayout = new LinearLayout(context);
		linearLayout.setOrientation(LinearLayout.VERTICAL);

		LinearLayout line1 = new LinearLayout(context);
		line1.setOrientation(LinearLayout.HORIZONTAL);
		final TextView view1 = new TextView(context);
		view1.setText("数量:");
		view1.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		final EditText edit1 = new EditText(context);
		edit1.setInputType(inputType);
		edit1.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

		line1.addView(view1);
		line1.addView(edit1);

//		LinearLayout line2 = new LinearLayout(context);
//		line2.setOrientation(LinearLayout.HORIZONTAL);
//		TextView view2 = new TextView(context);
//		view2.setText("价格:");
//		view2.setLayoutParams(new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.WRAP_CONTENT,
//				LinearLayout.LayoutParams.WRAP_CONTENT));
//		final EditText edit2 = new EditText(context);
//		edit1.setInputType(inputType);
//		edit2.setLayoutParams(new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.MATCH_PARENT,
//				LinearLayout.LayoutParams.WRAP_CONTENT));
//		line2.addView(view2);
//		line2.addView(edit2);

//		LinearLayout line3 = new LinearLayout(context);
//		line3.setOrientation(LinearLayout.HORIZONTAL);
//
//		TextView view3 = new TextView(context);
//		view3.setText("方式:");
//
//		view3.setLayoutParams(new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.WRAP_CONTENT,
//				LinearLayout.LayoutParams.WRAP_CONTENT));
//
//		Spinner spinner = new Spinner(context);
//		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
//				context, R.array.planets_array,
//				android.R.layout.simple_spinner_item);
//		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		spinner.setPrompt("选择退货处理方式");
//
//		spinner.setLayoutParams(new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.MATCH_PARENT,
//				LinearLayout.LayoutParams.WRAP_CONTENT));
//
//		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int position, long id) {
//				String itemValue = arg0.getItemAtPosition(position).toString();
//				hadletype = itemValue;				
//			}
//
//			public void onNothingSelected(AdapterView<?> arg0) {
//				
//			}
//
//		});
//		spinner.setAdapter(adapter);
//
//		line3.addView(view3);
		//line3.addView(spinner);

		linearLayout.addView(line1);
//		linearLayout.addView(line2);
		//linearLayout.addView(line3);

		edit1.setText(num);
//		edit2.setText(price);
//		if(hadleType=="")
//		{
//			spinner.setSelection(0);
//		}
//		else if(hadleType.equals("退货拉回"))
//		{
//			spinner.setSelection(1);
//		}
//		else if(hadleType.equals("丢弃或销毁"))
//		{
//			spinner.setSelection(2);
//		}
//		else
//		{
//			throw new Exception("不存在退货处理方式："+hadleType);
//		}
		
		
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title).setMessage(message).setView(linearLayout)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface arg0, int arg1) {

						String num = edit1.getText().toString();
//						String price = edit2.getText().toString();

						onprompt.OnPrompt(num,  hadletype);
						//System.out.println("数量："+num+"---价格："+price+"--处理方式："+hadletype);
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

					}
				}).show();

	}
}
