package com.example.depanntout;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import bdd.InterventionDAO;
import metier.Intervention;

public class ModifInter extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modif_inter);

        initialisation();
    }

    private void initialisation() {

        // Get data from Intent
        String date = getIntent().getStringExtra("date");
        String heure = getIntent().getStringExtra("heure");
        String observation = getIntent().getStringExtra("observation");
        int idCli = getIntent().getIntExtra("idCli", -1);
        int idInter = getIntent().getIntExtra("_id", -1);

        // Reference TextViews
        TextView etDate = findViewById(R.id.ptModifDate);
        TextView etHeure = findViewById(R.id.ptModifHeure);
        TextView etObservation = findViewById(R.id.ptModifObserv);
        TextView etIdCli = findViewById(R.id.ptModifIdClient);

        // Set data in TextViews
        etDate.setText(date);
        etHeure.setText(heure);
        etObservation.setText(observation);
        etIdCli.setText(String.valueOf(idCli));

        // gestion du boutton Quitter
        Button btQuitterInter = (Button) findViewById(R.id.btQuitterInter);
        // Association d'un écouteur d'événement au clic de btQuitter
        btQuitterInter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                finish();
            }
        });

        Button btnDeleteInter = findViewById(R.id.btnDeleteInter);
        Button btnModifInter = findViewById(R.id.btnModifInter);

        // Accès à la table intervention
        InterventionDAO interventionDAO = new InterventionDAO(this);

        // Gestion de l'événement onClick sur le boutn Ajouter
        btnDeleteInter.setOnClickListener(v -> {
            boolean isDeleted = interventionDAO.delete(idCli);
            if (isDeleted) {
                Toast.makeText(this, "Client deleted successfully", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "Delete failed", Toast.LENGTH_SHORT).show();
            }
        });

        btnModifInter.setOnClickListener(v -> {

            String dateModif = etDate.getText().toString();
            String heureModif = etHeure.getText().toString();
            String obsModif = etObservation.getText().toString();
            int idCliModif = Integer.parseInt(etIdCli.getText().toString());

            boolean isUpdated = interventionDAO.update(idInter, dateModif, heureModif, obsModif, idCliModif);
            if (isUpdated) {
                Toast.makeText(this, "Client updated successfully", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "Update failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
