package com.example.apidemo;

import com.example.apidemo.MainActivity.LongOperation;

import in.shoppingwish.scandid.net.JSONConstants;
import in.shoppingwish.scandid.net.Product;
import in.shoppingwish.scandid.net.ScandidRequestHandler;
import in.shoppingwish.scandid.net.SearchResult;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class ResultListActivity extends ListActivity{
	 ScandidRequestHandler scandidRequestHandler;
	 String prodID;
	 SearchResult searchResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		scandidRequestHandler =  new ScandidRequestHandler(CConstants.API_KEY);
		searchResult = (SearchResult)getIntent().getSerializableExtra("SearchResult");
		if(!searchResult.get_searchResponseMessage().equals(JSONConstants.RESPONSE_ERROR))
		{
			String str[] = new String[searchResult.get_searchItems().length];
			for(int i=0;i<searchResult.get_searchItems().length;i++) {
				str[i] = searchResult.get_searchItems()[i].get_itemTitle();
				
			}
			
			ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_s,str);
			
			setListAdapter(arrayAdapter);
			getListView().setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					// TODO Auto-generated method stub
					prodID = searchResult.get_searchItems()[arg2].get_itemID();
					new LongOperation().execute("");
					
				}
			});
		}
		
	}
	 class LongOperation extends AsyncTask<String, Void, Product> {
	    	ProgressDialog progress;

	        @Override
	        protected Product doInBackground(String... params) {
	        	Product product = new Product();
	        	
				try {
					
					product = scandidRequestHandler.getProductDetailsFromProductID(prodID);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				return product;
	        }

	        @Override
	        protected void onPostExecute(Product product) {
	        	progress.dismiss();
	        	if(product != null && (!product.get_productResponseMessage().equals(JSONConstants.RESPONSE_ERROR)))
	        	{
	        		Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
	        		intent.putExtra("Product", product); 
	        		startActivity(intent);
	        	}
	        	else
					Toast.makeText(
							getApplicationContext(),
							"Either you are not contected to internet or server is down try again later.",
							Toast.LENGTH_LONG).show();
	        	
	        	
	        }

	        @Override
			protected void onPreExecute() {
				progress = ProgressDialog.show(ResultListActivity.this, "Hang on",
						"Searching barcodes...", true,true,new DialogInterface.OnCancelListener(){
	                @Override
	                public void onCancel(DialogInterface dialog) {
	                	LongOperation.this.cancel(true);
	                }
	            });
			}

	        @Override
	        protected void onProgressUpdate(Void... values) {}

			

			
	    }
	
}
