package com.example.fueltrack;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FuelTrackActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fuel_track);
    setContentView(R.layout.activity_fuel_track);
    getFragmentManager().beginTransaction()
        .replace(android.R.id.content, new FuelTrackFragment()).commit();

  }

  public static class FuelTrackFragment extends Fragment {
    private EditText dateText;
    private EditText distanceText;
    private EditText fuelText;
    private EditText costText;
    private Button enterButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Get references to input fields
      dateText = (EditText) getActivity().findViewById(R.id.dateText);
      distanceText = (EditText) getActivity().findViewById(R.id.distanceText);
      fuelText = (EditText) getActivity().findViewById(R.id.fuelText);
      costText = (EditText) getActivity().findViewById(R.id.costText);

      clear_form();

      // Handle Save button to add new mileage record
      enterButton = (Button) getActivity().findViewById(R.id.enterButton);
      enterButton.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
          // save data to persistent storage
          saveMileageInput();
          // calculate & display mpg
        }
      });

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      // Inflate the menu; this adds items to the action bar if it is present.
      inflater.inflate(R.menu.fuel_track, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      int itemid = item.getItemId();
      switch (itemid) {
        case 0:
          // Preferences activity
      }
      return true;
    }

    private void setDateFieldCurrentDate() {
      // Initialize date field with current date
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
      Date curDate = new Date(java.lang.System.currentTimeMillis());
      String dateString = df.format(curDate);
      dateText.setText(dateString);
    }

    private void clear_form() {
      distanceText.setText("");
      fuelText.setText("");
      costText.setText("");
      setDateFieldCurrentDate();
    }

    private void saveMileageInput() {
      String date = dateText.getText().toString();
      Float distance = Float.valueOf(distanceText.getText().toString());
      Float fuel = Float.valueOf(fuelText.getText().toString());
      Float cost = Float.valueOf(costText.getText().toString());
      String testMessage = "Date: " + date + "; Distance: " + distance
          + "; Fuel: " + fuel + "; Cost: " + cost + "; $/G: " + cost / fuel;
      Toast.makeText(getActivity(), testMessage, Toast.LENGTH_LONG).show();
      // Save mileage data here
      clear_form();
      float testMilesDiff = 300; // Delete this dummy setting
      float testGallonsDiff = 13; // Delete this dummy setting
      showCurrentMileage(testMilesDiff, testGallonsDiff); // Change this
    }

    private void showCurrentMileage(float milesDiff, float gallonsDiff) {
      float mpg = milesDiff / gallonsDiff;
      float kpl = milesToKm(milesDiff) / gallonsToLiters(gallonsDiff);
      TextView mpgView = (TextView) getActivity().findViewById(R.id.mpgView);
      mpgView.setText(mpg + " MPG (" + kpl + " KPL)");
    }

    private float kmToMiles(float distance) {
      return distance * 0.621371192f;
    }

    private float milesToKm(float distance) {
      return distance * 1.609344f;
    }

    private float litersToGallons(float volume) {
      return volume * 0.264172037f;
    }

    private float gallonsToLiters(float volume) {
      return volume * 3.785412f;
    }
  }
}