package com.ae.apps.tripmeter.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ae.apps.tripmeter.R;

/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

    public static final String FORMAT = "%.02f";
    private EditText txtTripDistance;
    private EditText txtFuelPrice;
    private EditText txtMileage;
    private TextView lblTotalCost;
    private TextView lblFuelNeeded;

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.fragment_main, container, false);

        // Find text inputs and labels to show result
        txtTripDistance = (EditText) inflatedView.findViewById(R.id.txtDistance);
        txtFuelPrice = (EditText) inflatedView.findViewById(R.id.txtFuelPrice);
        txtMileage = (EditText) inflatedView.findViewById(R.id.txtMileage);
        lblFuelNeeded = (TextView) inflatedView.findViewById(R.id.lblFuelNeeded);
        lblTotalCost = (TextView) inflatedView.findViewById(R.id.lblTotalCost);

        // Show result text without placeholders on load
        lblFuelNeeded.setText( getResources().getString(R.string.str_total_fuel_needed, ""));
        lblTotalCost.setText(getResources().getString(R.string.str_total_fuel_price, ""));

        // Find and handle Calculate button clicks
        Button calculate = (Button) inflatedView.findViewById(R.id.btnCalculate);
        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                validateAndCalculateTripCosts();
            }
        });

        // TODO Read last saved mileage and fuel price

        return inflatedView;
    }

    private void validateAndCalculateTripCosts() {
        if(!TextUtils.isEmpty(txtTripDistance.getText().toString())
                && !TextUtils.isEmpty(txtFuelPrice.getText().toString())
                && !TextUtils.isEmpty(txtMileage.getText().toString())){

            // Process data and make the calculations
            float tripDistance = Float.parseFloat(txtTripDistance.getText().toString());
            float fuelPrice = Float.parseFloat(txtFuelPrice.getText().toString());
            float mileage = Float.parseFloat(txtMileage.getText().toString());

            // Check for mileage greater than 0
            if(mileage > 0){
                float fuelNeeded =  tripDistance / mileage;
                float totalFuelPrice = fuelPrice * fuelNeeded;

                // Update labels in result card view
                lblFuelNeeded.setText( getResources().getString(
                        R.string.str_total_fuel_needed, String.format(FORMAT, fuelNeeded)));
                lblTotalCost.setText(getResources().getString(
                        R.string.str_total_fuel_price, String.format(FORMAT, totalFuelPrice)));

                // TODO Store Mileage and Current Fuel Price
            } else {
                Toast.makeText(getActivity().getBaseContext(),
                        R.string.str_error_no_mileage, Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getActivity().getBaseContext(),
                    R.string.str_error_empty_inputs, Toast.LENGTH_LONG).show();
        }
    }
}

@@ -54,17 +54,136 @@
 oid.
-os.Bundle
+content.Context;%0Aimport android.content.SharedPreferences;%0Aimport android.os.Bundle;%0Aimport android.preference.PreferenceManager
 ;%0Aim
@@ -838,24 +838,192 @@
 uelNeeded;%0A%0A
+    private Context mContext;%0A%0A    private static String PREF_KEY_FUEL_PRICE = %22pref_key_fuel_price%22;%0A    private static String PREF_KEY_MILEAGE = %22pref_key_mileage%22;%0A%0A
     public M
@@ -1196,24 +1196,76 @@
 nceState) %7B%0A
+        mContext = getActivity().getBaseContext();%0A%0A
         View
@@ -2391,21 +2391,16 @@
       //
- TODO
  Read la
@@ -2430,16 +2430,454 @@
 el price
+ if exists%0A        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);%0A        String strFuelPrice = sharedPreferences.getString(PREF_KEY_FUEL_PRICE, null);%0A        String strMileage = sharedPreferences.getString(PREF_KEY_MILEAGE, null);%0A        if(null != strFuelPrice && null != strMileage)%7B%0A            txtFuelPrice.setText(strFuelPrice);%0A            txtMileage.setText(strMileage);%0A        %7D
 %0A%0A      
@@ -3732,16 +3732,90 @@
 rd view%0A
+                String strFuelNeeded = String.format(FORMAT, fuelNeeded);%0A
         
@@ -3874,33 +3874,8 @@
 ing(
-%0A                        
 R.st
@@ -3901,16 +3901,69 @@
 _needed,
+ strFuelNeeded));%0A                String strMileage =
  String.
@@ -3981,20 +3981,22 @@
 AT, 
-fuelNeeded))
+totalFuelPrice
 );%0A 
@@ -4060,33 +4060,8 @@
 ing(
-%0A                        
 R.st
@@ -4091,45 +4091,18 @@
 ce, 
-String.format(FORMAT, totalFuelPrice)
+strMileage
 ));%0A
@@ -4124,13 +4124,8 @@
   //
- TODO
  Sto
@@ -4174,79 +4174,277 @@
     
-%7D else %7B%0A                Toast.makeText(getActiv
+    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);%0A                preferences.edit().putString(PREF_KEY_FUEL_PRICE, strFuelNeeded).commit();%0A                preferences.ed
 it
-y
 ().
-getBaseContext(),
+putString(PREF_KEY_MILEAGE, strMileage).commit();%0A
 %0A   
@@ -4442,32 +4442,43 @@
 t();%0A%0A          
+  %7D else %7B%0A
               R.
@@ -4466,32 +4466,59 @@
  %7B%0A             
+   Toast.makeText(mContext,
  R.string.str_er
@@ -4622,59 +4622,17 @@
 ext(
-getActivity().getBaseContext(),%0A                   
+mContext,
  R.s

store and retain last fuel price and mileage