package com.example.pap_teste;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.InputType;

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 com.example.pap_teste.models.Staff;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class AddStaffActivity extends AppCompatActivity {

    private Button addButton;
    private EditText nameEditText;
    private Spinner zonaSpinner;
    private Button btnAddZone;
    private Spinner mesaSpinner;
    private ArrayList<String> zones;
    private ArrayList<String> mesas;
    private ArrayAdapter<String> adapter;
    private ArrayAdapter<String> mesaAdapter;

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

        addButton = findViewById(R.id.addButton);
        nameEditText = findViewById(R.id.nammeEditText);
        zonaSpinner = findViewById(R.id.zonaSpinner);
        btnAddZone = findViewById(R.id.btnAddZone);
        mesaSpinner = findViewById(R.id.mesaSpinner);
        
        Button btnVoltarAddStaff = findViewById(R.id.btnVoltarAddStaff);
        if (btnVoltarAddStaff != null) {
            btnVoltarAddStaff.setOnClickListener(v -> finish());
        }

        zones = new ArrayList<>();
        zones.add("Sala");
        zones.add("Esplanada");
        zones.add("Balcão");

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, zones);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        zonaSpinner.setAdapter(adapter);

        mesas = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            mesas.add("Mesa " + i);
        }
        mesaAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mesas);
        mesaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mesaSpinner.setAdapter(mesaAdapter);

        btnAddZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AddStaffActivity.this);
                builder.setTitle("Adicionar Zona");

                final EditText input = new EditText(AddStaffActivity.this);
                input.setInputType(InputType.TYPE_CLASS_TEXT);
                builder.setView(input);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String newZone = input.getText().toString();
                        if (!newZone.isEmpty()) {
                            zones.add(newZone);
                            adapter.notifyDataSetChanged();
                            zonaSpinner.setSelection(zones.size() - 1);
                        }
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.show();
            }
        });

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEditText.getText().toString();
                String zona = "";
                if (zonaSpinner.getSelectedItem() != null) {
                    zona = zonaSpinner.getSelectedItem().toString();
                }
                String mesa = "";
                if (mesaSpinner.getSelectedItem() != null) {
                    mesa = mesaSpinner.getSelectedItem().toString();
                }

                if (!name.isEmpty()) {
                    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Staff");
                    String uuid = java.util.UUID.randomUUID().toString();
                    Staff staff = new Staff(name, zona, mesa, uuid);
                    databaseReference.child(uuid).setValue(staff);

                    Toast.makeText(AddStaffActivity.this, "Staff adicionado com sucesso!", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(AddStaffActivity.this, "Erro: Preencha todos os campos", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}