package com.example.scoutingapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_settings);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        EditText NameTextSetting = findViewById(R.id.Name);
        EditText TBATextSetting = findViewById(R.id.TBA);
        EditText SheetsTextSetting = findViewById(R.id.Sheets);
        EditText MatchTextSetting = findViewById(R.id.Match);
        Button NextButton = findViewById(R.id.buttonNext);
        Button NHDURButton = findViewById(R.id.buttonNHDUR);
        Button MEFALButton = findViewById(R.id.buttonMEFAL);
        Button NECMPButton = findViewById(R.id.buttonNECMP);
        Button CMPTXButton = findViewById(R.id.buttonCMPTX);
        SharedPreferences sharedPreferences = getSharedPreferences("MySettings", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        NextButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            String NameToSave = NameTextSetting.getText().toString();
            String TBAToSave = TBATextSetting.getText().toString();
            String SheetsToSave = SheetsTextSetting.getText().toString();
            String MatchToSave = MatchTextSetting.getText().toString();
                editor.putString("NameText", NameToSave);
                editor.putString("TBAText", TBAToSave);
                editor.putString("SheetsText", SheetsToSave);
                editor.putString("MatchText", MatchToSave);
                editor.apply();
                Toast.makeText(SettingsActivity.this, "Saved!", Toast.LENGTH_SHORT).show();
                makeIntent();
            }
        });



        NHDURButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String defaultTBA = "https://www.thebluealliance.com/api/v3/match/2026nhdur_qm";
                TBATextSetting.setText(defaultTBA);
                editor.putString("TBAText", defaultTBA);
                String defaultSheets = "1ky5LBTpnEeBEEaaF7z6UWdh-E7YmOSeij4dYdR2PU4A";
                SheetsTextSetting.setText(defaultSheets);
                editor.putString("SheetsText", defaultSheets);
                String defaultMatch = "NHDUR";
                MatchTextSetting.setText(defaultMatch);
                editor.putString("MatchText", defaultMatch);
                editor.apply();


                Toast.makeText(SettingsActivity.this, "Reset to NHDUR default!", Toast.LENGTH_SHORT).show();
            }
        });

        MEFALButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String defaultTBA = "https://www.thebluealliance.com/api/v3/match/2026mefal_qm";
                TBATextSetting.setText(defaultTBA);
                editor.putString("TBAText", defaultTBA);
                String defaultSheets = "1ky5LBTpnEeBEEaaF7z6UWdh-E7YmOSeij4dYdR2PU4A";
                SheetsTextSetting.setText(defaultSheets);
                editor.putString("SheetsText", defaultSheets);
                String defaultMatch = "MEFAL";
                MatchTextSetting.setText(defaultMatch);
                editor.putString("MatchText", defaultMatch);
                editor.apply();


                Toast.makeText(SettingsActivity.this, "Reset to MEFAL default!", Toast.LENGTH_SHORT).show();
            }
        });

        NECMPButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String defaultTBA = "https://www.thebluealliance.com/api/v3/match/2026necmp2_qm";
                TBATextSetting.setText(defaultTBA);
                editor.putString("TBAText", defaultTBA);
                String defaultSheets = "1ky5LBTpnEeBEEaaF7z6UWdh-E7YmOSeij4dYdR2PU4A";
                SheetsTextSetting.setText(defaultSheets);
                editor.putString("SheetsText", defaultSheets);
                String defaultMatch = "NECMP";
                MatchTextSetting.setText(defaultMatch);
                editor.putString("MatchText", defaultMatch);
                editor.apply();


                Toast.makeText(SettingsActivity.this, "Reset to NECMP default!", Toast.LENGTH_SHORT).show();
            }
        });

        CMPTXButton.setEnabled(false);
        CMPTXButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String defaultTBA = "https://www.thebluealliance.com/api/v3/match/2026txcmp_qm";
                TBATextSetting.setText(defaultTBA);
                editor.putString("TBAText", defaultTBA);
                String defaultSheets = "1ky5LBTpnEeBEEaaF7z6UWdh-E7YmOSeij4dYdR2PU4A";
                SheetsTextSetting.setText(defaultSheets);
                editor.putString("SheetsText", defaultSheets);
                String defaultMatch = "CMPTX";
                MatchTextSetting.setText(defaultMatch);
                editor.putString("MatchText", defaultMatch);
                editor.apply();


                Toast.makeText(SettingsActivity.this, "Reset to CMPTX default!", Toast.LENGTH_SHORT).show();
            }
        });



        String NameText = sharedPreferences.getString("NameText", "");
        NameTextSetting.setText(NameText);
        String TBAText = sharedPreferences.getString("TBAText", "");
        TBATextSetting.setText(TBAText);
        String SheetsText = sharedPreferences.getString("SheetsText", "");
        SheetsTextSetting.setText(SheetsText);
        String MatchText = sharedPreferences.getString("MatchText", "");
        MatchTextSetting.setText(MatchText);




    }

    private void makeIntent() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}