package com.example.scoutingapp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

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 TeleActivity extends AppCompatActivity {
    private MatchData matchData;

    private Button hub;
    private Button hub15;
    private Button hub25;
    private Button hub50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_tele);
        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;
        });

        Button nextButton = (Button) findViewById(R.id.button_Next);
        Button backButton = (Button) findViewById(R.id.BackButton);
        matchData = MainActivity.matchData;
        EditText notes = findViewById(R.id.notes4);
        notes.setText(matchData.geteNote());


        TextView textViewTeam = findViewById(R.id.teamnumber);
        textViewTeam.setText("Team " + matchData.getTeamNumber());
        TextView textViewMatch = findViewById(R.id.matchNumber);
        textViewMatch.setText("Match " + matchData.getMatchNumber());
        //sets team number and color
        if (matchData.isPractice()) {
            textViewTeam.setBackgroundColor(Color.parseColor("#0D9204")); //red
            textViewMatch.setBackgroundColor(Color.parseColor("#FF5722"));
            textViewMatch.setText("Practice");
        } else {
            if (!matchData.isBlueAlliance()) {
                textViewTeam.setBackgroundColor(Color.parseColor("#F71000")); //red
                textViewMatch.setBackgroundColor(Color.parseColor("#F71000"));
            } else {
                textViewTeam.setBackgroundColor(Color.parseColor("#0084ff"));
                textViewMatch.setBackgroundColor(Color.parseColor("#0084ff"));//blue
            }
        }
        hub = (Button) findViewById(R.id.Hub);
        hub15 = (Button) findViewById(R.id.Hub15);
        hub25 = (Button) findViewById(R.id.Hub25);
        hub50 = (Button) findViewById(R.id.Hub50);
        hub15.setText(getString(R.string.hub_15, matchData.gettHub()));
        hub25.setText(getString(R.string.hub_25, matchData.gettHub()));
        hub50.setText(getString(R.string.hub_50, matchData.gettHub()));
        CheckBox canLeaveBox = (CheckBox) findViewById(R.id.cb_CanLeave);


        hub15.setOnClickListener((v) -> {
            matchData.settHub(matchData.gettHub() + 15);
            hubText();
        });



        hub25.setOnClickListener((v) -> {
            matchData.settHub(matchData.gettHub() + 25);
            hubText();
        });
        hub50.setOnClickListener((v) -> {
            matchData.settHub(matchData.gettHub() + 50);
            hubText();
        });

        hub.setOnLongClickListener((v) -> {
            matchData.settHub(0);
            hubText();
            return true;
        });

        hub15.setOnLongClickListener((v) -> {
            matchData.settHub(matchData.gettHub() - 15);
            hubText();
            return true;
        });

        hub25.setOnLongClickListener((v) -> {
            matchData.settHub(matchData.gettHub() - 25);
            hubText();
            return true;
        });

        hub50.setOnLongClickListener((v) -> {
            matchData.settHub(matchData.gettHub() - 50);
            hubText();
            return true;
        });


        nextButton.setOnLongClickListener((v) -> {
            Intent intent = new Intent(this, EndActivity.class);
            startActivity(intent);
            notes.setText(String.valueOf(notes.getText()));
            matchData.seteNote(String.valueOf(notes.getText()));
            return true;
        });


        backButton.setOnLongClickListener((v) -> {
            Intent intent = new Intent(this, AutoActivity.class);
            startActivity(intent);
            return true;
        });

        canLeaveBox.setOnClickListener((v) -> matchData.settDied(canLeaveBox.isChecked()));

    }
    private void hubText() {
        hub15.setText(getString(R.string.hub_15, matchData.gettHub()));
        hub25.setText(getString(R.string.hub_25, matchData.gettHub()));
        hub50.setText(getString(R.string.hub_50, matchData.gettHub()));
    }


}
