package com.example.fueltrack;

import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class FuelListActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.file_view);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      finish();
      return;
    }

    if (savedInstanceState == null) {
      FuelListFragment fragment = new FuelListFragment();
      fragment.setArguments(getIntent().getExtras());
      getFragmentManager().beginTransaction()
          .add(android.R.id.content, fragment).commit();
    }
  }

  public static class FuelListFragment extends ListFragment {
    private FuelCursorAdapter mca;
    private Cursor cursor;
    private FuelDbAdapter dba;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      dba = new FuelDbAdapter(getActivity());
      dba.open();
      cursor = dba.getMileageList();
      mca = new FuelCursorAdapter(getActivity(), cursor);
      setListAdapter(mca);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
      String msg = "Position: " + position + " ID: " + id;
      Toast toast = Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG);
      toast.show();
      Intent mileageEdit = new Intent(getActivity(), FuelTrackActivity.class);
      mileageEdit.putExtra("ID", id);
      startActivity(mileageEdit);
    }

  }
}
