package com.movashah.talee;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.RadioGroup.*;
import android.content.*;
import android.text.*;
import android.system.*;
import android.widget.AdapterView.*;

public class greetingActivity extends Activity 
{
		@Override
		protected void onCreate(Bundle savedInstanceState)
		{
				super.onCreate(savedInstanceState);
				setContentView(R.layout.main);

				Spinner ts=(Spinner) findViewById(R.id.mainSpinner1);
				ts.setOnItemSelectedListener(new OnItemSelectedListener(){
						@Override
						public void onItemSelected(AdapterView parentView, View SelectedItemView, int postion, long id)
						{
								TextView tl=(TextView) findViewById(R.id.mainTextView2);
								
								switch((int) id){
										
										case 1:
												tl.setText("طبع: ناری، روز: یکشنبه، ماه: جمادی‌الآخر");
												return;
										case 2:
												tl.setText("طبع: ناری، روز: پنجشنبه، ماه: رمضان");
												return;
										case 3:
												tl.setText("طبع: آبی، روز: پنجشنبه، ماه: ذی‌الحجه");
												return;
										case 4:
												tl.setText("طبع: آبی، روز: دوشنبه، ماه: ربیع‌الآخر");
												return;
										case 5:
												tl.setText("طبع: خاکی، روز: جمعه، ماه: صفر");
												return;
										case 6:
												tl.setText("طبع: بادی، روز: جمعه، ماه: رجب");
												return;
										
								}
								tl.setText(" ");
								
						}
						@Override
						public void onNothingSelected(AdapterView parentView){}
				});
				
				EditText mNam=(EditText) findViewById(R.id.mainEditText1);
				mNam.addTextChangedListener(new TextWatcher()
						{
								public void afterTextChanged(Editable s)
								{

										TextView mAbj=(TextView) findViewById(R.id.abjadmadar);
										EditText mNaam=(EditText) findViewById(R.id.mainEditText1);
										//ShowD("tr:"+String.valueOf(GetAbjad(mNaam.getText().toString(), true)));
										mAbj.setText(String.valueOf(GetAbjad(mNaam.getText().toString())));
								}
								public void beforeTextChanged(CharSequence s, int start, int count, int after)
								{}
								public void onTextChanged(CharSequence s, int start, int before, int count)
								{}
						});
						
				EditText pNam=(EditText) findViewById(R.id.mainEditText2);
				pNam.addTextChangedListener(new TextWatcher()
						{
								public void afterTextChanged(Editable s)
								{

										TextView mAbj=(TextView) findViewById(R.id.abjadpishvand);
										EditText mNaam=(EditText) findViewById(R.id.mainEditText2);
										//ShowD("tr:"+String.valueOf(GetAbjad(mNaam.getText().toString(), true)));
										mAbj.setText(String.valueOf(GetAbjad(mNaam.getText().toString())));
								}
								public void beforeTextChanged(CharSequence s, int start, int count, int after)
								{}
								public void onTextChanged(CharSequence s, int start, int before, int count)
								{}
						});
						
		}
		@Override
		public View findViewById(int id)
		{
				// TODO: Implement this method
				return super.findViewById(id);
		}

		@Override
		public boolean onCreateOptionsMenu(Menu menu)
		{
				// TODO: Implement this method
				getMenuInflater().inflate(R.menu.next_menu, menu);
				return true;
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item)
		{
				// TODO: Implement this method
				switch (item.getItemId())
				{
						case(R.id.main_menuitem2):

								finish();
				}

				return true;
		}

		public void setPishvand(View view)
		{
				EditText ed = (EditText) findViewById(R.id.mainEditText2);
				if (((CheckBox) view).isChecked())
				{
						ed.setEnabled(true);
						ed.requestFocus();
						//ed.setText("سید");

				}
				else
				{
						EditText md=(EditText) findViewById(R.id.mainEditText1);
						md.requestFocus();
						//ed.setText("");
						ed.setEnabled(false);
				}



		}

		public void selectTalee(View view)
		{

		}
		public void ShowD(String p)
		{
				//نمایش پیام

				eDialogFragment f = new eDialogFragment();
				Bundle args = new Bundle();
				args.putString("Nam", p);
				f.setArguments(args);
				f.show(getFragmentManager(), "mydialog");

		}
		public void findName(View view)
		{
				TextView mAbj=(TextView) findViewById(R.id.abjadmadar);
				TextView pAbj=(TextView) findViewById(R.id.abjadpishvand);
				RadioButton pRad=(RadioButton) findViewById(R.id.mainRadioButton2);
				RadioButton dRad=(RadioButton) findViewById(R.id.mainRadioButton1);
				CheckBox pChk=(CheckBox) findViewById(R.id.mainCheckBox1);
				Spinner tSpn=(Spinner) findViewById(R.id.mainSpinner1);



				if (Integer.parseInt(mAbj.getText().toString()) > 0)
				{
						if (!pChk.isChecked() || Integer.parseInt(pAbj.getText().toString()) > 0)
						{
								if (pRad.isChecked() || dRad.isChecked())
								{
										//ShowD(String.valueOf(tSpn.getSelectedItemId()));
										if (tSpn.getSelectedItemId()!=0)
										{
												//جستجوی نام
												ProgressBar p = (ProgressBar) findViewById(R.id.mainProgressBar);
												p.setIndeterminate(true);
												final Handler h = new Handler();
												h.postDelayed(new Runnable(){
																@Override
																public void run()
																{
																		Intent res=new Intent(greetingActivity.this, resultActivity.class);
																		greetingActivity.this.startActivity(res);
																		ProgressBar p = (ProgressBar) findViewById(R.id.mainProgressBar);
																		p.setIndeterminate(false);
																}


														}, 1000);
										}
										else
										{
												ShowD("طالع مورد نظر خود را انتخاب کنید.");
										}
								}
								else
								{
										ShowD("جنسیت نوزاد را انتخاب کنید.");
								}
						}
						else
						{
								ShowD("پیشوند نام را با حروف فارسی وارد کنید.");
						}
				}
				else
				{
						ShowD("نام مادر را با حروف فارسی وارد کنید.");
				}




		}

		public void gohelp(View v)
		{
				Intent hel=new Intent(greetingActivity.this, helpActivity.class);
				greetingActivity.this.startActivity(hel);
		}

		// توایع اصلی


		public int Abjad(String a)
		{
				switch (a)
				{
						case "ا":
						case "آ":
						case "أ":
						case "إ":
						case "ء":
								return(1);
						case "ب":
						case "پ":
								return(2);
						case "ج":
						case "چ":
								return(3);
						case "د":
								return(4);
						case "ه":
						case "ة":
						case "ۀ":
								return(5);
						case "و":
						case "ؤ":
								return(6);
						case "ز":
						case "ژ":
								return(7);
						case "ح":
								return(8);
						case "ط":
								return(9);
						case "ی":
						case "ئ":
						case "ي":
								return(10);
						case "ک":
						case "ك":
						case "گ":
								return(20);
						case "ل":
								return(30);
						case "م":
								return(40);
						case "ن":
								return(50);
						case "س":
								return(60);
						case "ع":
								return(70);
						case "ف":
								return(80);
						case "ص":
								return(90);
						case "ق":
								return(100);
						case "ر":
								return(200);
						case "ش":
								return(300);
						case "ت":
								return(400);
						case "ث":
								return(500);
						case "خ":
								return(600);
						case "ذ":
								return(700);
						case "ض":
								return(800);
						case "ظ":
								return(900);
						case "غ":
								return(1000);
						default:
								return(0);
				}
		}
		public int AbjadDel(String a)
		{
				switch (a)
				{
						case "ا":
						case "آ":
						case "أ":
						case "إ":
						case "ء":
								return(1);
						case "ب":
								return(2);
						case "ج":
								return(3);
						case "د":
								return(4);
						case "ه":
						case "ة":
						case "ۀ":
								return(5);
						case "و":
						case "ؤ":
								return(6);
						case "ز":
								return(7);
						case "ح":
								return(8);
						case "ط":
								return(9);
						case "ی":
						case "ئ":
						case "ي":
								return(10);
						case "ک":
						case "ك":
								return(20);
						case "ل":
								return(30);
						case "م":
								return(40);
						case "ن":
								return(50);
						case "س":
								return(60);
						case "ع":
								return(70);
						case "ف":
								return(80);
						case "ص":
								return(90);
						case "ق":
								return(100);
						case "ر":
								return(200);
						case "ش":
								return(300);
						case "ت":
								return(400);
						case "ث":
								return(500);
						case "خ":
								return(600);
						case "ذ":
								return(700);
						case "ض":
								return(800);
						case "ظ":
								return(900);
						case "غ":
								return(1000);
						default:
								return(0);
				}
		}
		public int GetAbjad(String a)
		{
				int s = 0;
				RadioButton rd=(RadioButton) findViewById(R.id.mainRadioButton4);
				try
				{
						if (rd.isChecked())
						{

								int i;
								for (i = 0;i <= a.length();i++)
								{
										//ShowD(a+":"+String.valueOf( a.substring(i,i+1)));
										s += Abjad(a.substring(i, i + 1));
								}


						}
						else
						{
								int i;
								for (i = 0;i < a.length();i++)
								{
										s += AbjadDel(a.substring(i, i + 1));
								}
						}
				}
				catch (Exception e)
				{
						//ShowD(e.getMessage());
				}
				
				return s;
		}
		public boolean DelPaj()
		{
				RadioButton rc = (RadioButton) findViewById(R.id.mainRadioButton3);
				if (rc.isChecked())
				{
						return(true);
				}
				else
				{
						return(false);
				}
		}


}

class eDialogFragment extends DialogFragment
{
		String Naam;

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState)
		{
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				Naam = getArguments().getString("Nam");
				builder.setTitle("فرم ناقص");
				builder.setMessage(Naam);
				builder.setNegativeButton("فهمیدم", new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface p1, int p2)
								{

								}
						});
				return builder.create();
		}


}
