import "package:flutter/material.dart";
import "package:google_fonts/google_fonts.dart";
import "../demo_identity.dart";
import "../theme/ensup_colors.dart";
import "../models/emprunt_item.dart";
import "../models/anomalie_retour.dart";
import "../services/emprunt_service.dart";
import "../widgets/ensup_top_bar.dart";
import "../widgets/brand_corners.dart";
import "../widgets/etat_checklist.dart";
import "resultat_retour_screen.dart";

/// Checklist de retour (RG16) : l'étudiant indique l'état de chaque accessoire
/// au retour, en vue de la comparaison avec la checklist de départ.
class ChecklistRetourScreen extends StatefulWidget {
  const ChecklistRetourScreen({super.key, required this.emprunt});

  final EmpruntItem emprunt;

  @override
  State<ChecklistRetourScreen> createState() => _ChecklistRetourScreenState();
}

class _ChecklistRetourScreenState extends State<ChecklistRetourScreen> {
  late final List<String> _elements = widget.emprunt.materiel.accessoires.isEmpty
      ? <String>[widget.emprunt.materiel.nom]
      : widget.emprunt.materiel.accessoires;
  late final List<EtatChecklist> _etats = List<EtatChecklist>.filled(
    _elements.length,
    EtatChecklist.present,
  );
  final TextEditingController _commentaireController = TextEditingController();
  bool _envoiEnCours = false;

  @override
  void dispose() {
    _commentaireController.dispose();
    super.dispose();
  }

  Future<void> _valider() async {
    setState(() => _envoiEnCours = true);

    final anomalies = <AnomalieRetour>[];
    for (var i = 0; i < _etats.length; i++) {
      if (_etats[i] == EtatChecklist.absent) {
        anomalies.add(AnomalieRetour(element: _elements[i], etatRetour: "Absent au retour"));
      } else if (_etats[i] == EtatChecklist.deteriore) {
        anomalies.add(AnomalieRetour(element: _elements[i], etatRetour: "Détérioré au retour"));
      }
    }

    try {
      final resultat = await EmpruntService.restituerEmprunt(
        empruntId: widget.emprunt.id,
        commentaireRetour: _commentaireController.text,
        elements: _elements.asMap().entries.map((entry) {
          return {
            "nomElement": entry.value,
            "etat": _etatApi(_etats[entry.key]),
            "quantiteConstatee": _etats[entry.key] == EtatChecklist.absent ? 0 : 1,
          };
        }).toList(),
      );

      if (!mounted) {
        return;
      }

      final anomaliesAffichees = resultat.statut == "CLOTURE" ? <AnomalieRetour>[] : anomalies;
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (_) => ResultatRetourScreen(
            emprunt: widget.emprunt.copyWith(statut: resultat.statut),
            anomalies: anomaliesAffichees,
          ),
        ),
      );
    } catch (error) {
      if (!mounted) {
        return;
      }
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(error.toString())),
      );
    } finally {
      if (mounted) {
        setState(() => _envoiEnCours = false);
      }
    }
  }

  String _etatApi(EtatChecklist etat) {
    switch (etat) {
      case EtatChecklist.present:
        return "PRESENT";
      case EtatChecklist.absent:
        return "ABSENT";
      case EtatChecklist.deteriore:
        return "DETERIORE";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: EnsupColors.soft,
      body: Stack(
        children: [
          Center(
            child: ConstrainedBox(
              constraints: const BoxConstraints(maxWidth: 1280, maxHeight: 820),
              child: Container(
                margin: const EdgeInsets.all(24),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(16),
                  boxShadow: [
                    BoxShadow(
                      color: EnsupColors.text.withValues(alpha: 0.14),
                      blurRadius: 60,
                      offset: const Offset(0, 18),
                    ),
                  ],
                ),
                clipBehavior: Clip.antiAlias,
                child: Column(
                  children: [
                    EnsupTopBar(
                      brandText: "Checklist de retour",
                      campusTag: DemoIdentity.campusTag,
                      userName: DemoIdentity.userName,
                      userInitials: DemoIdentity.userInitials,
                      onBack: () => Navigator.of(context).pop(),
                    ),
                    Expanded(
                      child: SingleChildScrollView(
                        padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 28),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Expanded(flex: 3, child: _colonneGauche()),
                            const SizedBox(width: 20),
                            Expanded(flex: 2, child: _colonneDroite()),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          const Positioned.fill(child: BrandCorners()),
        ],
      ),
    );
  }

  Widget _colonneGauche() {
    final materiel = widget.emprunt.materiel;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
          decoration: BoxDecoration(
            color: EnsupColors.soft,
            borderRadius: BorderRadius.circular(12),
            border: Border.all(color: EnsupColors.teal.withValues(alpha: 0.15), width: 1.5),
          ),
          child: Row(
            children: [
              Container(
                width: 38,
                height: 38,
                decoration: BoxDecoration(
                  color: EnsupColors.teal.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(9),
                ),
                child: Icon(materiel.icon, color: EnsupColors.teal, size: 18),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      materiel.nom,
                      style: GoogleFonts.darkerGrotesque(fontWeight: FontWeight.w800, fontSize: 16),
                    ),
                    Text(
                      "Emprunté le ${widget.emprunt.dateEmprunt}",
                      style: GoogleFonts.titilliumWeb(fontSize: 12, color: EnsupColors.muted),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        const SizedBox(height: 16),
        ChecklistTableau(
          accessoires: _elements,
          etats: _etats,
          onChanged: (index, etat) => setState(() => _etats[index] = etat),
        ),
      ],
    );
  }

  Widget _colonneDroite() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const ChecklistInstructions(
          titre: "Rappel checklist de départ",
          texte:
              "Au départ, tous les accessoires du kit étaient présents. "
              "Indiquez leur état au retour ; toute différence sera signalée.",
        ),
        const SizedBox(height: 14),
        ChecklistCommentaire(
          controller: _commentaireController,
          hint: "Commentaire optionnel...",
        ),
        const SizedBox(height: 14),
        SizedBox(
          width: double.infinity,
          child: FilledButton.icon(
            onPressed: _envoiEnCours ? null : _valider,
            icon: _envoiEnCours
                ? const SizedBox(
                    width: 18,
                    height: 18,
                    child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
                  )
                : const Icon(Icons.check, size: 18),
            label: Text(
              _envoiEnCours ? "Enregistrement..." : "Valider le retour",
              style: GoogleFonts.darkerGrotesque(fontWeight: FontWeight.w700, fontSize: 16),
            ),
            style: FilledButton.styleFrom(
              backgroundColor: EnsupColors.blue3,
              padding: const EdgeInsets.symmetric(vertical: 16),
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            ),
          ),
        ),
      ],
    );
  }
}
