package com.example.scoutingapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.metrics.Event;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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 OfflineActivity extends AppCompatActivity {
    public static MatchData matchData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_offline);
        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;
        });
        SharedPreferences sharedPreferences = getSharedPreferences("MySettings", MODE_PRIVATE);
        String EventText = sharedPreferences.getString("MatchText", "");
        String NameText = sharedPreferences.getString("NameText", "");
            Toast.makeText(this, "Offline Mode", Toast.LENGTH_SHORT).show();
        matchData = new MatchData();
        Button nextButton = (Button) findViewById(R.id.ButtonNext);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText matchNumber = (EditText) findViewById(R.id.Matchedit);
                String matchNumStr = matchNumber.getText().toString();
                int matchNum = 0;
                if (!matchNumStr.isEmpty()) {
                    matchNum = Integer.parseInt(matchNumStr);
                }

                MainActivity.matchData.setMatchNumber(matchNum);
                EditText teamNumber = (EditText) findViewById(R.id.TeamNumber);
                MainActivity.matchData.setTeamNumber(teamNumber.getText().toString());
                MainActivity.matchData.setEventName(EventText);
                MainActivity.matchData.setScoutName(NameText);

                if (matchData == null) {
                    matchData = new MatchData();
                }
                if (matchData.getMatchNumber() == 0) {
                    matchData.setMatchNumber(1);
                }

                matchNumber.setText(String.valueOf(matchData.getMatchNumber()));

                if (MainActivity.matchData.getEventName().isEmpty() ||
                        MainActivity.matchData.getMatchNumber() == 0 ||
                        MainActivity.matchData.getTeamNumber().equals("0000") ||
                        MainActivity.matchData.getScoutName().isEmpty() ||
                        MainActivity.matchData.getScoutName().equals("NO NAME PROVIDED")) {
                    Toast.makeText(getApplicationContext(), "Fill in EVERYTHING", Toast.LENGTH_SHORT).show();
                } else {
                    makeIntent();
                }
                }

        });

        Button SettingsButton = (Button) findViewById(R.id.ButtonSettings);
        SettingsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                makeSettings();
            }
        });
        Button Practice = (Button) findViewById(R.id.Practice);
        Practice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.matchData.setScoutName(NameText);
                MainActivity.matchData.setTeamNumber("2648");
                MainActivity.matchData.setPractice(true);
                makeIntent();
            }
        });
        Button redButton = (Button) findViewById(R.id.Red);
        Button blueButton = (Button) findViewById(R.id.Blue);
        redButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.matchData.setBlueAlliance(false);
            }
        });
        blueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.matchData.setBlueAlliance(true);
            }
        });
        Button backButton = (Button) findViewById(R.id.ButtonBack);
        if (!isNetworkConnected()){
            backButton.setText("refresh");
        } else {
            SettingsButton.setVisibility(View.GONE);
            Practice.setVisibility(View.GONE);
        }

        backButton.setOnClickListener((v) -> {
            Intent backIntent = new Intent(OfflineActivity.this, MainActivity.class);
            startActivity(backIntent);
        });
    }
    private void makeIntent() {
        Intent intent = new Intent(this, startingActivity.class);
        startActivity(intent);
    }
    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        }
        return false;
    }


    private void makeSettings() {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }
}