package com.sabirjan.android;

import com.sabirjan.Dialog.Custom_WaitDialog;
import com.sabirjan.DrawUyghurWord.ConvertUL;
import com.sabirjan.DrawUyghurWord.SetUyghurTypeface;
import com.sabirjan.wangluo.fangwenService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Ch_Uy_Activity extends Activity{

	
	EditText edit_chuy_1;
	TextView txt_jieguo;
	Custom_WaitDialog cwait;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch_uy);
		
		TextView txt1=(TextView)this.findViewById(R.id.txt_chuy_1);
		txt1=SetUyghurTypeface.GetTextView(this, txt1);
		edit_chuy_1=(EditText)this.findViewById(R.id.edit_chuy_1);
		Button btn1=(Button)this.findViewById(R.id.btn_chuy_1);
		btn1=SetUyghurTypeface.GetButton(this, btn1);
		txt_jieguo=(TextView)this.findViewById(R.id.txt_jieguo);
		txt_jieguo=SetUyghurTypeface.GetTextView(this, txt_jieguo);
		cwait=new Custom_WaitDialog(this);
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(edit_chuy_1.getWindowToken(), 0);
		
				if(edit_chuy_1.getText().toString().trim().length()==0)
				{
					showErrorGameAlert();
					return;
				}
				else
				{	
					cwait.show();
					new Thread( new Runnable() {     
					    public void run() {     
					    	
					    	mHandler.sendEmptyMessage(2);
					    }
					}).start();
					//mHandler.sendEmptyMessage(1);
			//		new myTrhead().start();
				}
			}
		
		}
		);
		
	}
	  private Handler mHandler = new Handler(){  
          
	        public void handleMessage(Message msg) {  
	        	try
				{
	        	
				fangwenService cls=new fangwenService();
				String result=cls.callChaXunHanWei(edit_chuy_1.getText().toString().trim());
				String []str=result.split("；");
				ConvertUL cls2=new ConvertUL();
				result="";
				for(String strOne:str)
				{
					result+=cls2.getShape(strOne.trim())+"\n";
				}
		
			//	result=cls2.GetULUyghurWord(result);
				txt_jieguo.setText(result);
				}
				catch(Exception ex)
				{
					
					Toast toast;
					toast = Toast.makeText(getApplicationContext(),
						     ex.getMessage(), Toast.LENGTH_LONG);
						   toast.setGravity(Gravity.CENTER, 0, 0);
						   toast.show();
					
					/*
				
					LayoutInflater inflater = getLayoutInflater();
					   View layout = inflater.inflate(R.layout.custom_toast,
					     (ViewGroup) findViewById(R.id.llToast));
					   ImageView image = (ImageView) layout
					     .findViewById(R.id.tvImageToast);
					   image.setImageResource(R.drawable.icon);
					   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
					   title.setText("Attention");
					   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
					   text.setText();
					   toast = new Toast(getApplicationContext());
					   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
					   toast.setDuration(Toast.LENGTH_LONG);
					   toast.setView(layout);
					   toast.show();
					   */
					
				}
				cwait.dismiss();
	        }
	        };  
	private void showErrorGameAlert() {
		
		final AlertDialog dlg = new AlertDialog.Builder(this).create();
		dlg.show();
		Window window = dlg.getWindow();
	        // *** 主要就是在这里实现这种效果的.
	        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
		window.setContentView(R.layout.oyun1_hata_dialog);
	        // 为确认按钮添加事件,执行退出应用操作

	 
	        // 关闭alert对话框架
	        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
	        cancel.setOnClickListener(new View.OnClickListener() {
		  public void onClick(View v) {
			  dlg.cancel();
			 
			}
		  });
	}

}
