package xyz.igorgee.mtaperfectfare;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    public static final double BONUS_VALUE = 1.11;
    public static final double FARE = 2.75;

    EditText trips;
    EditText days;
    EditText weeks;
    EditText startingBalanceEdit;
    TextView totalTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        trips = (EditText) findViewById(R.id.trips);
        days = (EditText) findViewById(R.id.days);
        weeks = (EditText) findViewById(R.id.weeks);
        startingBalanceEdit = (EditText) findViewById(R.id.starting_balance);
        totalTextView = (TextView) findViewById(R.id.total_text_view);
    }

    public void displayTotal(View view) {
        try {
            int numberOfDays = Integer.parseInt(days.getText().toString());
            int numberOfWeeks = Integer.parseInt(weeks.getText().toString());
            int numberOfTrips = Integer.parseInt(trips.getText().toString());
            double amountInCard = Double.parseDouble(startingBalanceEdit.getText().toString());

            if (numberOfDays < 1 || numberOfWeeks < 1 || numberOfWeeks > 4 || numberOfTrips < 1 ||
                    amountInCard < 0) {
                Toast.makeText(this, "Please enter valid numbers.", Toast.LENGTH_LONG).show();
            } else {
                double totalAmount = calculateTotal(numberOfDays, numberOfWeeks, numberOfTrips,
                        amountInCard);
                if (totalAmount <= 0){
                    Toast.makeText(this, "Silly, you already have money!.",
                            Toast.LENGTH_LONG).show();
                } else {
                    DecimalFormat df = new DecimalFormat("#.00");
                    totalTextView.setText("Total: $" + df.format(totalAmount));
                }
            }
        } catch (NumberFormatException n){
            Toast.makeText(this, "Please enter a value for all fields.", Toast.LENGTH_LONG).show();
        }
    }

    private double calculateTotal(int numberOfDays, int numberOfWeeks, int numberOfTrips,
                                  double amountInCard) {
        numberOfTrips *= numberOfDays * numberOfWeeks;

        double totalAmount;

        if ((FARE * numberOfTrips - amountInCard) / BONUS_VALUE >= 5.50) {
            totalAmount = ((FARE * numberOfTrips - amountInCard) / BONUS_VALUE);
        } else {
            totalAmount = ((FARE * numberOfTrips - amountInCard));
        }

        // MTA Metrocard Vending Machine only allow payments in nickel increments
        int pennies = (int) Math.round(totalAmount*100);
        int nickelDifference = pennies % 10 - 5;

        if (nickelDifference < 0 && nickelDifference != -5){
            pennies += -nickelDifference;
        } else if (nickelDifference > 0){
            pennies += 5 - nickelDifference;
        }

        return pennies/100.0;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

@@ -1469,48 +1469,38 @@
 if (
-numberOfDays %3C 1 %7C%7C numberOfWeeks %3C 1 %7C%7C
+!areValidNumbers(numberOfDays,
  num
@@ -1513,15 +1513,9 @@
 eeks
- %3E 4 %7C%7C
+,
  num
@@ -1528,35 +1528,9 @@
 rips
- %3C 1 %7C%7C%0A                   
+,
  amo
@@ -1538,20 +1538,17 @@
 ntInCard
- %3C 0
+)
 ) %7B%0A    
@@ -1831,16 +1831,17 @@
 nt %3C= 0)
+ 
 %7B%0A      
@@ -1906,17 +1906,16 @@
 e money!
-.
 %22,%0A     
@@ -1956,32 +1956,336 @@
 H_LONG).show();%0A
+                %7D else if (totalAmount %3E= 116.5 && numberOfWeeks == 4) %7B%0A                    totalTextView.setText(%22Buy a Monthly Metrocard%22);%0A                %7D else if (totalAmount/numberOfWeeks %3E= 31)%7B%0A                    totalTextView.setText(%22Buy a Weekly Metrocard%5Cn%22 + numberOfWeeks + %22 Time(s)%22);%0A
                 
@@ -2512,16 +2512,17 @@
 ption n)
+ 
 %7B%0A      
@@ -2628,24 +2628,411 @@
    %7D%0A    %7D%0A%0A
+    private boolean areValidNumbers(int numberOfDays, int numberOfWeeks, int numberOfTrips,%0A                                    double amountInCard) %7B%0A        if (numberOfDays %3C 1 %7C%7C numberOfDays %3E 7 %7C%7C numberOfWeeks %3C 1 %7C%7C%0A                numberOfWeeks %3E 4 %7C%7C numberOfTrips %3C 1 %7C%7C amountInCard %3C 0) %7B%0A            return false;%0A        %7D else %7B%0A            return true;%0A        %7D%0A    %7D%0A%0A
     private 
@@ -3634,17 +3634,19 @@
 alAmount
-*
+ * 
 100);%0A  
@@ -3752,16 +3752,17 @@
 e != -5)
+ 
 %7B%0A      
@@ -3837,16 +3837,17 @@
 nce %3E 0)
+ 
 %7B%0A      
@@ -3918,17 +3918,19 @@
  pennies
-/
+ / 
 100.0;%0A 

Add weekly/monthly logic