package com.cwbuyer.lib;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cwbuyer.format.IDialog;
import com.cwbuyer.format.IReqCallback;
import com.pwbuyer.main.R;

@SuppressLint("InflateParams")
public class DialogUtilis {
	// 顯示 SpinerDialog
    public static void showDialog(Context cnt, String title, int select, final String[] array, final IDialog callback){
    	new AlertDialog.Builder(cnt)
    	.setTitle(title)
        .setSingleChoiceItems(array, select, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				callback.onDialogFinish(which, array[which]);
				dialog.dismiss();
			}
		}).create().show();
    }
    
 // 一般的確認 Dialog
    public static void showDialogChk(Context cnt, String title, String msg, final IReqCallback callback){
    	new AlertDialog.Builder(cnt)
    	.setTitle(title)
    	.setMessage(msg)
    	.setPositiveButton("確定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				callback.onAction();
				dialog.dismiss();
			}
		})
    	.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
//				callback.onAction();
				dialog.dismiss();
			}
		}).create().show();
    }
    
    // 計算機
    @SuppressLint("InflateParams")
	@SuppressWarnings("unused")
	public static void showComputer(final Activity activity, final int vid){
		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		final View root = activity.getLayoutInflater().inflate(R.layout.dlg_computer, null);
		Calculator calu = new Calculator(activity, root);
		final TextView print = (TextView)root.findViewById(R.id.text_print);
		dialog.setView(root);
		dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
//	        	  EditText edit = (EditText)activity.findViewById(vid);
	        	  try{
	        		  String result = print.getText().toString();
	        		  if(result == null || result.length() <=0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("+") > 0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("-") > 0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("×") > 0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("÷") > 0){
	        			  print.setText("0");
	        		  }
	        		  View view = null;
	        		  try{
	        			  view = activity.findViewById(vid);
	        			  if(view instanceof EditText){
	        				  ((EditText)view).setText(print.getText().toString());
	        			  }
	        			  if(view instanceof TextView){
	        				  ((TextView)view).setText(print.getText().toString());
	        			  }
	        		  }catch(Exception e){
	        			  e.printStackTrace();
	        		  }
//	        		  callback.onAction(print.getText().toString());
//	        		  edit.setText(print.getText().toString());
	        	  }catch(Exception e){
	        		  
	        	  }
	          }
		});
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
	          }
			});
		dialog.show();
	}
    
    
 // 來源編號
    public static void showSourceItem(final Activity activity, final int vid, String sourceNumber){
		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		final View root = activity.getLayoutInflater().inflate(R.layout.dlg_source_item, null);
		
		//限制最多只能三個字
		InputFilter[] FilterArray = new InputFilter[1];
		FilterArray[0] = new InputFilter.LengthFilter(3);
		((EditText)root.findViewById(R.id.edit_source_2)).setFilters(FilterArray);
		
		if(sourceNumber != null && sourceNumber.length() > 3){
			try{
				int spliteIndex = sourceNumber.length() - 3;
				String source1 = sourceNumber.substring(0, spliteIndex);
				String source2 = sourceNumber.substring(spliteIndex, sourceNumber.length());
				((EditText)root.findViewById(R.id.edit_source_1)).setText(source1);
				((EditText)root.findViewById(R.id.edit_source_2)).setText(source2);
			}catch(Exception e){
				e.printStackTrace();
			}
		}else if(sourceNumber != null && sourceNumber.length() == 3){
			((EditText)root.findViewById(R.id.edit_source_1)).setText("0");
			((EditText)root.findViewById(R.id.edit_source_2)).setText(sourceNumber);
		}
		
		dialog.setView(root);
		dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
	        	  try{
	        		  String source1 = ((EditText)root.findViewById(R.id.edit_source_1)).getText().toString();
	        		  String source2 = ((EditText)root.findViewById(R.id.edit_source_2)).getText().toString();
	        		  if(source1 == null || source1.length() <= 0){
	        			  Toast.makeText(activity, "來源編號不可為空", Toast.LENGTH_SHORT).show();
	        			  return;
	        		  }
	        		  
	        		  if(source2 == null || source2.length() <= 0){
	        			  Toast.makeText(activity, "流水號不可為空", Toast.LENGTH_SHORT).show();
	        			  return;
	        		  }
	        		  
	        		  // source2 需要format
	        		  source2 = AppUtil.formatSource(source2);
	        		  View view = null;
	        		  try{
	        			  view = activity.findViewById(vid);
	        			  if(view instanceof EditText){
	        				  ((EditText)view).setText(source1 + source2);
	        			  }
	        			  if(view instanceof TextView){
	        				  ((TextView)view).setText(source1 + source2);
	        			  }
	        		  }catch(Exception e){
	        			  e.printStackTrace();
	        		  }
	        	  }catch(Exception e){
	        		  
	        	  }
	          }
		});
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
	          }
			});
		dialog.show();
	}
    
    
    
 // 計算機
    @SuppressWarnings("unused")
	public static void showComputer(final Activity activity, final IDialog callback){
		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		final View root = activity.getLayoutInflater().inflate(R.layout.dlg_computer, null);
		Calculator calu = new Calculator(activity, root);
		final TextView print = (TextView)root.findViewById(R.id.text_print);
		dialog.setView(root);
		dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
//	        	  EditText edit = (EditText)activity.findViewById(vid);
	        	  try{
	        		  String result = print.getText().toString();
	        		  if(result == null || result.length() <=0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("+") > 0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("-") > 0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("×") > 0){
	        			  print.setText("0");
	        		  }else if(result.indexOf("÷") > 0){
	        			  print.setText("0");
	        		  }
	        		  callback.onDialogFinish(0, print.getText().toString());
	        	  }catch(Exception e){
	        		  
	        	  }
	          }
		});
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
	          }
			});
		dialog.show();
	}
}
