package com.example.usstates;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class StatesFlagGalleryActivity extends Activity {
  StatesDbAdapter sda;
  private Cursor cursor;
  private int abbrIndex;
  private int nameIndex;
  private int nickIndex;
  private int capitalIndex;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actvity_states_gallery);
    Gallery gallery = (Gallery)findViewById(R.id.flagGallery);
    sda = new StatesDbAdapter((Context)this);
    cursor = sda.getAllStates();
    startManagingCursor(cursor);
    abbrIndex = cursor.getColumnIndex(StatesDbAdapter.KEY_STATE_ABBR);
    nameIndex = cursor.getColumnIndex(StatesDbAdapter.KEY_STATE_NAME);
    nickIndex = cursor.getColumnIndex(StatesDbAdapter.KEY_STATE_NICK);
    capitalIndex = cursor.getColumnIndex(StatesDbAdapter.KEY_STATE_CAPITAL);
    StatesImageAdapter sia = new StatesImageAdapter(this,cursor);
    gallery.setAdapter(sia);
    gallery.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        displayStateInfo();
      }
    });
    gallery.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        displayStateInfo();
      }
    });

  }

  private void displayStateInfo() {
    String msg = cursor.getString(nameIndex)
        + "\nAbbreviation: " + cursor.getString(abbrIndex)
        + "\nCapital: " + cursor.getString(capitalIndex)
        + "\nNickname: " + cursor.getString(nickIndex);
    Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();

  }
  
}
