package com.example.scoutingapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
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;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class AutoActivity extends AppCompatActivity {
    private MatchData matchData;
    private Button hub;
    private Button hub10;
    private Button hub5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_auto);
        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;
        Button Climb = (Button) findViewById(R.id.Climb);


        TextView textViewTeam = findViewById(R.id.teamnumber);
        textViewTeam.setText("Team " + matchData.getTeamNumber());
        TextView textViewMatch = findViewById(R.id.matchNumber);
        textViewMatch.setText("Match " + matchData.getMatchNumber());
        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);
        hub5 = (Button) findViewById(R.id.Hub5);
        hub10 = (Button) findViewById(R.id.Hub10);

        hub.setText(String.format(getResources().getString(R.string.coralScored), "hub +1 : ", matchData.getaHub()));
        hub5.setText(String.format(getResources().getString(R.string.coralScored), "hub +5: ", matchData.getaHub()));
        hub10.setText(String.format(getResources().getString(R.string.coralScored), "hub +10: ", matchData.getaHub()));

        CheckBox canLeaveBox = (CheckBox) findViewById(R.id.cb_CanLeave);
        hub.setOnClickListener((v) -> {
            matchData.setaHub(matchData.getaHub() + 1);
            hubText();
        });


        hub5.setOnClickListener((v) -> {
            matchData.setaHub(matchData.getaHub() + 5);
            hubText();
        });



        hub10.setOnClickListener((v) -> {
            matchData.setaHub(matchData.getaHub() + 10);
            hubText();
        });

        hub.setOnLongClickListener((v) -> {
            matchData.setaHub(matchData.getaHub() - 1);
            hubText();
            return true;
        });

        hub5.setOnLongClickListener((v) -> {
            matchData.setaHub(matchData.getaHub() - 5);
            hubText();
            return true;
        });

        hub10.setOnLongClickListener((v) -> {
            matchData.setaHub(matchData.getaHub() - 10);
            hubText();
            return true;
        });


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


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

        Climb.setOnClickListener((v) -> {
            matchData.setaClimb(1);
        });

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



    }


    private void hubText() {
        hub.setText(String.format(getResources().getString(R.string.coralScored), "hub +1 : ", matchData.getaHub()));
        hub5.setText(String.format(getResources().getString(R.string.coralScored), "hub +5: ", matchData.getaHub()));
        hub10.setText(String.format(getResources().getString(R.string.coralScored), "hub +10: ", matchData.getaHub()));
        }
}
