package fr.esgi.meteoland;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import fr.esgi.meteoland.dao.CityBdd;
import fr.esgi.meteoland.helper.DatabaseHandler;
import fr.esgi.meteoland.model.City;

public class AddCityActivity extends Activity {

	// =========================================================================
	// ATTRIBUTES
	// =========================================================================

	public static final int ADD_ACTION = 1234;
	private final ArrayList<String> cities = new ArrayList<String>();
	private ListView listView;

	// Listview Adapter
	ArrayAdapter<String> adapter;

	// Search EditText
	EditText inputSearch;

	// ArrayList for Listview
	ArrayList<HashMap<String, String>> productList;

	DatabaseHandler db = new DatabaseHandler(this);

	// =========================================================================
	// CONSTRUCTORS
	// =========================================================================

	// =========================================================================
	// METHODS
	// =========================================================================

	// =========================================================================
	// OVERRIDES
	// =========================================================================

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_city);
		final Context context = this;
		Log.v("Info", "DatabaseHandler");

		final CityBdd cityBdd = new CityBdd(db);

		// Reading all contacts
		Log.d("Reading: ", "Reading all contacts ..");
		List<City> cityList = cityBdd.getAllCityByFavorite(false);

		for (City cn : cityList) {
			cities.add(cn.getCountryCode() + ", " + cn.getNm());
			String log = "Id: " + cn.getId() + ", Nm: " + cn.getNm()
					+ ", CountryCode: " + cn.getCountryCode() + ", Favorite: "
					+ cn.getFavorite();
			Log.d("City : ", log);
		}

		listView = (ListView) this.findViewById(R.id.listAddCity);
		inputSearch = (EditText) findViewById(R.id.inputSearch);

		String[] stockArr = new String[cities.size()];
		stockArr = cities.toArray(stockArr);

		// Adding items to listview
		adapter = new ArrayAdapter<String>(this, R.layout.list_item,
				R.id.product_name, stockArr);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.d("position", parent.getItemAtPosition(position).toString());

				String str[] = parent.getItemAtPosition(position).toString()
						.split(", ");
				City city = cityBdd.getCityByNmAndCountryCode(str[0], str[1]);
				city.setFavorite(true);
				cityBdd.updateCity(city, context);
				AddCityActivity.this.finish();
			}
		});

		// Enabling Search Filter
		inputSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {
				// When user changed the Text
				AddCityActivity.this.adapter.getFilter().filter(cs);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
			}
		});
	}

	// =========================================================================
	// GETTERS & SETTERS
	// =========================================================================
}