package com.jarulla.katnax;


import java.util.List;

import com.sabirjan.android.R;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainAcrivity extends Activity {
	private int intTallighanIzdeshShakli=-1;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_katnax);
        KorunmeYuzniTengshesh();
    }
    
    private void KorunmeYuzniTengshesh()
    {
    	MySqLiteOpenHelper myHelper=new MySqLiteOpenHelper(this);
    	myHelper.HatireQoshush();
    	final EditText txtHazirqiBiket=(EditText)findViewById(R.id.txtHazirqiBiket);
    	final EditText txtNishanBiket=(EditText)findViewById(R.id.txtNishanBiket);
    	final TextView labNatije=(TextView)findViewById(R.id.labNatije);
    	Button btnIzdesh=(Button)findViewById(R.id.btnIzdesh);
        Spinner spinner = (Spinner)findViewById(R.id.spinner1);

		int intHetQongluqi=15;
		final Typeface tfHetNushisi=Typeface.createFromAsset(this.getAssets(), "UKIJTuzTom.ttf");
		
		txtHazirqiBiket.setTypeface(tfHetNushisi);
		txtNishanBiket.setTypeface(tfHetNushisi);
		labNatije.setTypeface(tfHetNushisi);
		btnIzdesh.setTypeface(tfHetNushisi);
		
		txtHazirqiBiket.setTextSize(intHetQongluqi);
		txtNishanBiket.setTextSize(intHetQongluqi);
		labNatije.setTextSize(intHetQongluqi);
		btnIzdesh.setTextSize(intHetQongluqi);
		
		labNatije.setVisibility(View.INVISIBLE);
		btnIzdesh.setText("ﺋﯩﺰﺩﻩﺵ");
		
        ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this,R.array.arryIzdeshShekli,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
            @Override

            public void onItemSelected(AdapterView<?> parent, View view,int position, long id){
            	TextView txt=(TextView)view;
            	txt.setTypeface(tfHetNushisi);
            	intTallighanIzdeshShakli=position;
            	switch(intTallighanIzdeshShakli)
            	{
            	case 0:
            		//AptibusLiniyesiniIzdesh();
            		txtNishanBiket.setEnabled(false);
            		labNatije.setVisibility(View.INVISIBLE);
            		txtHazirqiBiket.setText("");
            		txtHazirqiBiket.setHint("101 ﺋﺎﭘﺘﻮﺑﯘﺱ ﻟﯩﻨﯩﻴﻪ ﻧﻮﻣﯘﺭﯨﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ ");
            		break;
            	case 1:
            		//BiketIzdesh();
            		txtNishanBiket.setEnabled(false);
            		labNatije.setVisibility(View.INVISIBLE);
            		txtHazirqiBiket.setText("");
            		txtHazirqiBiket.setHint("ﺋﯩﺰﺩﻩﻳﺪﯨﻐﺎﻥ ﺑﯧﻜﻪﺕ ﻧﺎﻣﯩﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ: ﻧﻪﻧﻤﯩﻦ");
            		break;
            	case 2:
            		//AptibusAlmishish();
            		txtNishanBiket.setEnabled(true);
            		labNatije.setVisibility(View.INVISIBLE);
            		txtHazirqiBiket.setText("");
            		txtNishanBiket.setText("");
            		txtHazirqiBiket.setHint("ﺳﯩﺰﮔﻪ ﺋﻪﯓ ﻳﯧﻘﯩﻦ ﺑﻮﻟﻐﺎﻥ ﺑﯩﻜﻪﺕ ﻧﺎﻣﯩﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ: ﺑﯧﻴﻤﯩﻦ");
            		txtNishanBiket.setHint("ﺑﺎﺭﯨﺪﯨﻐﺎﻥ ﺑﯩﻜﻪﺗﻨﯩﯔ ﻧﺎﻣﯩﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ: ﻧﻪﻧﻤﯩﻦ");
            		break;
            	}
             //Toast.makeText(MainAcrivity.this,"ﺗﺎﻟﻠﯩﻐﺎﻥ ﺋﯩﺰﺩﻩﺵ ﺷﻪﻛﻠﻰ" +parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
            }

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

        });
		
        //izdesh konupkisini chekkende yuzbiridighan ix towende
        btnIzdesh.setOnClickListener(new Button.OnClickListener()
        {
			@Override
			public void onClick(View v) 
			{
				switch(intTallighanIzdeshShakli)
				{
					case 0: AptibusLiniyesiniIzdesh();break;
					case 1: BiketIzdesh();break;
					case 2: 
//						new Thread (new Runnable()
//					{
//						public void run()
//						{
							AptibusAlmishish();
//						}
//					}).start();
					break;
				}//switch
			}//onClick
        }//OnClickListener
        );//end OnClickListener
    }//KorunmeYuzniTengshesh
    
    private void AptibusLiniyesiniIzdesh()
    {
    	EditText txtHazirqiBiket=(EditText)findViewById(R.id.txtHazirqiBiket);
    	String strHazirqiBiket=txtHazirqiBiket.getText().toString().trim();
    	if(strHazirqiBiket.length()==0)
    	{
    		txtHazirqiBiket.setText("");
    		return;
    	}
    	MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(this);
		String strBiket=myDb.BirHatireIzdesh("select biketler from urumchiQatnashLiniyesi where yolNami like ?",new String[]{strHazirqiBiket},"biketler");
		TextView labNatije=(TextView)findViewById(R.id.labNatije);
		labNatije.setText(strBiket);
		labNatije.setVisibility(View.VISIBLE);
    }//AptibusLiniyesiniIzdesh
    
    private void BiketIzdesh()
    {
    	EditText txtHazirqiBiket=(EditText)findViewById(R.id.txtHazirqiBiket);
    	MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(this);
    	String strHazirqiBiket=txtHazirqiBiket.getText().toString().trim();
    	if(strHazirqiBiket.length()==0)
    	{
    		//txtHazirqiBiket.setHint("ﺋﯩﺰﺩﻩﻳﺪﯨﻐﺎﻥ ﺑﯧﻜﻪﺕ ﻧﺎﻣﯩﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ: ﻧﻪﻧﻤﯩﻦ");
    		txtHazirqiBiket.setText("");
    		return;
    	}
    	String strYolNami=myDb.KopHatireIzdesh("select yolNami from urumchiQatnashLiniyesi where biketler like ?",new String[]{"%"+strHazirqiBiket+"%"},"yolNami");
    	TextView labNatije=(TextView)findViewById(R.id.labNatije);
    	labNatije.setText(strYolNami);
    	labNatije.setVisibility(View.VISIBLE);

    }//BiketIzdesh
    
    private void AptibusAlmishish()
    {
    	EditText txtHazirqiBiket=(EditText)findViewById(R.id.txtHazirqiBiket);
    	EditText txtNishanBiket=(EditText)findViewById(R.id.txtNishanBiket);
    	String strHazirqiBiket=txtHazirqiBiket.getText().toString().trim();
    	String strNishanBiket=txtNishanBiket.getText().toString().trim();
    	TextView labNatije=(TextView)findViewById(R.id.labNatije);
    	if(strHazirqiBiket.length()==0 || strNishanBiket.length()==0)
    	{
    		//txtHazirqiBiket.setHint("ﺳﯩﺰﮔﻪ ﺋﻪﯓ ﻳﯧﻘﯩﻦ ﺑﻮﻟﻐﺎﻥ ﺑﯩﻜﻪﺕ ﻧﺎﻣﯩﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ: ﺑﯧﻴﻤﯩﻦ");
    		//txtNishanBiket.setHint("ﺑﺎﺭﯨﺪﯨﻐﺎﻥ ﺑﯩﻜﻪﺗﻨﯩﯔ ﻧﺎﻣﯩﻨﻰ ﻛﯩﺮﮔﯜﺯﯛﯓ، ﻣﻪﺳﯩﻠﻪﻥ: ﻧﻪﻧﻤﯩﻦ");
    		txtHazirqiBiket.setText("");
    		txtNishanBiket.setText("");
    		return;
    	}
    	MySqLiteOpenHelper myDb=new MySqLiteOpenHelper(this);
    	String strYolNami=myDb.KopHatireIzdesh("select yolNami from urumchiQatnashLiniyesi where biketler like ?",new String[]{"%"+strHazirqiBiket+"%"+strNishanBiket+"%"},"yolNami");
		if(strYolNami.length()==0)
		{
	    	try
	    	{
			List<String> listIzdeshNatijisi=myDb.WastilikBaridighanLiniyeIzdesh(strHazirqiBiket, strNishanBiket);
			for(int i=0;i<listIzdeshNatijisi.size();i++)
			{
				strYolNami+=listIzdeshNatijisi.get(i);
			}
	    	}
	    	catch(Exception ex)
	    	{
	    		labNatije.setText("chatak");
	    		labNatije.setVisibility(View.VISIBLE);
	    		ex.printStackTrace();
	    	}
		}
		labNatije.setText(strYolNami);
		labNatije.setVisibility(View.VISIBLE);

    }//AptibusAlmishish
}