////////////////////////////////////////////////
//
// A ChaloBEST (http://chalobest.in/) initiative
// Author: Nikita (Macgregor Techknowlogy)
// License: GPLv3
//
//
package com.best.ui;

import android.app.Activity;
import android.app.ListActivity;

import android.os.Bundle;
import android.os.Message;
import android.os.Handler;

import android.content.Intent;
import android.content.Context;

import android.widget.Button;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.Filter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.LayoutInflater;

import com.best.data.GTStore_sqlite;
import com.best.util.TransitionEffect;

import java.math.RoundingMode;
import java.math.BigDecimal;
import java.math.MathContext;

public class Search extends Activity
{
	public static Activity me = null;
	public static Context m_context;
	public static LayoutInflater mInflater;
	public int EditTextNo;
	public static boolean _callFromStops = false;
	public static String[] stopNames;
	public static String[] stopsRoad;
	public static String[] stopsArea;
	public static String[] stopID;
	public static String searchtxt;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate( savedInstanceState );
		//overridePendingTransition( R.anim.fade_in, R.anim.fade_out );
		if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) {
				TransitionEffect.callOverridePendingTransition(this);
		}
		me = this;
		m_context = this;
		me.setContentView( com.best.ui.R.layout.search );
		setTitle(com.best.ui.R.string.txtSearchFor);
		Intent intent = me.getIntent();
		_callFromStops = intent.getBooleanExtra("callFromStops" , false);
		init();
	
	}
		
	public void init()
	{
		final Button btnSearch = ( Button ) me.findViewById( com.best.ui.R.id.search );
		final EditText txtSearch = (EditText) findViewById(com.best.ui.R.id.searchtxt); 
		btnSearch.setOnClickListener( new View.OnClickListener() {
			public void onClick(View v) {
				 searchtxt = txtSearch.getText().toString();
				if(searchtxt != null && searchtxt.length() > 0)
				{
					if(_callFromStops == true)
					{
						Best.showProcessing( m_context, "Searching","Please wait while searching.." );
						StopSearchThread thread = new StopSearchThread( searchStopsHandler, searchtxt);
						thread.start();
					}
					else
					{
						Best.showProcessing( m_context, "Searching","Please wait while searching.." );
						BusSearchThread thread = new BusSearchThread( searchBusHandler, searchtxt);
						thread.start();
					}
				}
			}
		});	
	}
	public Handler searchStopsHandler = new Handler() {
        public void handleMessage(Message msg) {
            Bundle dataBundle = msg.getData();
			
			String[][] resultSet = (String[][]) dataBundle.getSerializable( "resultSet" );
			
			Best.dissmissProcessing();
			
			if( resultSet!=null && resultSet.length > 0 )
			{
				System.out.println("SEARCH RESULT LENGTH = "+resultSet.length );
				String[] stopIDs = new String[resultSet.length];
				String[] stopNames = new String[resultSet.length];
				String[] stopRoad = new String[resultSet.length];
				String[] stopArea = new String[resultSet.length];
				for(int i = 0; i < resultSet.length;i++)
				{
					stopIDs[i] = resultSet[i][0];
					stopNames[i] = resultSet[i][1];
					stopRoad[i] = resultSet[i][2];
					stopArea[i] = resultSet[i][3];
				}
				Intent mIntent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putStringArray( "StopId" , stopIDs );
				bundle.putStringArray( "StopName" , stopNames );
				bundle.putStringArray( "StopRoad" , stopRoad );
				bundle.putStringArray( "StopArea" , stopArea );
				bundle.putString( "searchtxt" , searchtxt);
				mIntent.putExtras( bundle );
				setResult( 0, mIntent );
				finish();
			}
			else
			{
				Best.showMessage( m_context, com.best.ui.R.string.errMsgNoStopsMatch, "Message" );
			}	
        }
    };
	
	private class StopSearchThread extends Thread
	{
        Handler _handler;
        String _txtSearch = "";
		
		StopSearchThread(Handler handler, String txtSearch) 
		{
            _handler = handler;
			_txtSearch = txtSearch;
        }
       
        public void run() 
		{
			String[][] resultSet = null;
			try{
				
				resultSet = GTStore_sqlite.getAllStopsByText( _txtSearch, 0.0, 0.0 );
			}catch(Exception e){System.out.println("Excp2: " + e.toString() );}
		
			Bundle dataBundle = new Bundle(); 
			dataBundle.putSerializable( "resultSet", resultSet );
			
			Message msg = _handler.obtainMessage();
			msg.setData( dataBundle );
			_handler.sendMessage( msg );
        }
    }
	
	public Handler searchBusHandler = new Handler() {
        public void handleMessage(Message msg) {
            Bundle dataBundle = msg.getData();
			
			String[][] resultSet = (String[][]) dataBundle.getSerializable( "resultSet" );
			
			Best.dissmissProcessing();
			
			if( resultSet!=null && resultSet.length > 0 )
			{
				System.out.println("SEARCH RESULT LENGTH = "+resultSet.length );
				String[] routeID = new String[resultSet.length];
				String[] tripID = new String[resultSet.length];
				String[] routeLongName = new String[resultSet.length];
				String[] routeName = new String[resultSet.length];
				String[] tripFreq = new String[resultSet.length];
				for(int i = 0; i < resultSet.length;i++)
				{
					routeID[i] = resultSet[i][0];
					tripID[i] = resultSet[i][1];
					routeLongName[i] = resultSet[i][2];
					routeName[i] = resultSet[i][3];
					tripFreq[i] = resultSet[i][4];
				}
				Intent mIntent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putStringArray( "routeID" , routeID );
				bundle.putStringArray( "tripID" , tripID );
				bundle.putStringArray( "routeLongName" , routeLongName );
				bundle.putStringArray( "routeName" , routeName );
				bundle.putStringArray( "tripFreq" , tripFreq );
				bundle.putString( "searchtxt" , searchtxt);
				mIntent.putExtras( bundle );
				setResult( 0, mIntent );
				finish();
			}
			else
			{
				Best.showMessage( m_context, com.best.ui.R.string.errMsgNoBusMatch, "Message" );
			}	
        }
    };
	
	private class BusSearchThread extends Thread
	{
        Handler _handler;
        String _txtSearch = "";
		
		BusSearchThread(Handler handler, String txtSearch) 
		{
            _handler = handler;
			_txtSearch = txtSearch;
        }
       
        public void run() 
		{
			String[][] resultSet = null;
			try{
				
				resultSet = GTStore_sqlite.getAllTripsByText( _txtSearch);
			}catch(Exception e){System.out.println("Excp2: " + e.toString() );}
		
			Bundle dataBundle = new Bundle(); 
			dataBundle.putSerializable( "resultSet", resultSet );
			
			Message msg = _handler.obtainMessage();
			msg.setData( dataBundle );
			_handler.sendMessage( msg );
        }
    }
	
	
}