package wind07.gpacalculator;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

// Define all crap
public class MainActivity extends AppCompatActivity {
    public static int modCount = 0;
    public static List<Module> modList = new ArrayList<>();
    private EditText txtModName;
    private EditText txtCredUnits;
    private TextView lblModCount;
    private TextView lblAddMod;
    private Spinner ddlGrade;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialise();
    }

    @Override
    public void onResume(){
        super.onResume();
        lblModCount.setText(String.format(Locale.ENGLISH,"%d",modCount));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about: {
                Intent intent = new Intent(this, About.class);
                startActivity(intent);
                break;
            }
            case R.id.action_settings: {
                Intent intent = new Intent(this, Settings.class);
                startActivity(intent);
                break;
            }
            // case blocks for other MenuItems (if any)
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder exitDialog = new AlertDialog.Builder(this);
        exitDialog.setMessage(getString(R.string.exit_confirmation));
        exitDialog.setCancelable(true);
        exitDialog.setPositiveButton(
                getString(R.string.yes),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        exitApp();
                    }
                });
        exitDialog.setNegativeButton(
                getString(R.string.no),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog exitAlert = exitDialog.create();
        exitAlert.show();
    }

    public void onAddModuleClick(View view){
        String modName = txtModName.getText().toString().trim();
        double credUnits = 0;
        int validation = 1;
        if (modName.length() == 0 ){
            txtModName.setError(getString(R.string.empty_module_name));
            validation = 0;
        }
        try {
            credUnits = Double.parseDouble(txtCredUnits.getText().toString());
        }
        catch(NumberFormatException nfe){
            txtCredUnits.setError(getString(R.string.empty_module_credits));
            validation = 0;
        }
        if(credUnits < 1 || credUnits>35){
            txtCredUnits.setError(getString(R.string.invalid_module_credits));
            validation = 0;
        }
        if(validation == 0){
            return;
        }
        else {
            String grade = ddlGrade.getSelectedItem().toString();
            Module newModule = new Module(modName, credUnits, grade);
            modList.add(newModule);
            modCount += 1;
            lblModCount.setText(String.format(Locale.ENGLISH,"%d",modCount));
            lblAddMod.setText(getString(R.string.module_added, modName));
            lblAddMod.setTextColor(Color.rgb(50,255,50));
            txtModName.setText("");
            txtCredUnits.setText("");
        }
    }
    public void viewModules(View view){
        Intent intent = new Intent(this, ViewModules.class);
        startActivity(intent);
    }

    public void computeGPA(View view){
        if (modCount == 0){
            Context context = getApplicationContext();
            CharSequence text = getString(R.string.no_modules_added);
            int duration = Toast.LENGTH_LONG;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
        else {
            Intent intent = new Intent(this, GPA.class);
            startActivity(intent);
        }
    }

    public void initialise(){
        setContentView(R.layout.activity_main);
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        String darkTheme = settings.getString("dark_theme", "");
        if (darkTheme.equals("true")){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        else if (darkTheme.equals("false")){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }

        txtModName = (EditText)findViewById(R.id.txtModName);
        txtCredUnits = (EditText)findViewById(R.id.txtCredUnits);
        lblModCount = (TextView)findViewById(R.id.lblModCount);
        lblAddMod = (TextView)findViewById((R.id.lblAddMod));
        ddlGrade = (Spinner) findViewById(R.id.ddlGrade);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.grade_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ddlGrade.setAdapter(adapter);
        if(modList.isEmpty()) {
            modCount = 0;
        }
        lblModCount.setText(String.format(Locale.ENGLISH,"%d", modCount));
    }

    public void exitApp(){
        //Clean up code
        modList.clear();
        modCount=0;
        finish();
    }
}

@@ -3427,16 +3427,17 @@
    catch
+ 
 (NumberF
@@ -3457,16 +3457,16 @@
 n nfe)%7B%0A
-
         
@@ -3582,16 +3582,17 @@
       if
+ 
 (credUni
@@ -3743,16 +3743,17 @@
       if
+ 
 (validat
@@ -3755,25 +3755,25 @@
 lidation == 
-0
+1
 )%7B%0A         
@@ -3774,526 +3774,41 @@
     
-     
-return;%0A        %7D%0A        else %7B%0A            String grade = ddlGrade.getSelectedItem().toString();%0A            Module newModule = new Module(modName, credUnits, grade);%0A            modList.add(newModule);%0A            modCount += 1;%0A            lblModCount.setText(String.format(Locale.ENGLISH,%22%25d%22,modCount));%0A            lblAddMod.setText(getString(R.string.module_added, modName));%0A            lblAddMod.setTextColor(Color.rgb(50,255,50));%0A            txtModName.setText(%22%22);%0A            txtCredUnits.setText(%22%22
+     addModule(modName, credUnits
 );%0A 
@@ -5657,32 +5657,547 @@
 Count));%0A    %7D%0A%0A
+    public void addModule(String modName, double credUnits)%7B%0A        String grade = ddlGrade.getSelectedItem().toString();%0A        Module newModule = new Module(modName, credUnits, grade);%0A        modList.add(newModule);%0A        modCount += 1;%0A        lblModCount.setText(String.format(Locale.ENGLISH,%22%25d%22,modCount));%0A        lblAddMod.setText(getString(R.string.module_added, modName));%0A        lblAddMod.setTextColor(Color.rgb(50,255,50));%0A        txtModName.setText(%22%22);%0A        txtCredUnits.setText(%22%22);%0A    %7D%0A%0A
     public void 

Move addModule to its own function