import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:garage/user/model/user_provider.dart';
import 'package:get_it/get_it.dart';
import 'package:provider/provider.dart';

import '../model/car.dart';
import '../service/car_service.dart';
import '../widget/car_row.dart';
import 'info_car_page.dart';

class MyGaragePage extends StatefulWidget {
  final bool showArchived;

  const MyGaragePage({super.key, this.showArchived = false});

  @override
  State<MyGaragePage> createState() => _MyGaragePageState();
}

class _MyGaragePageState extends State<MyGaragePage> {
  final CarService carService = GetIt.instance<CarService>();

  @override
  Widget build(BuildContext context) {
    final UserProvider userProvider = context.watch<UserProvider>();
    final futureCars = widget.showArchived
        ? userProvider.currentUser!.archivedCars
        : userProvider.currentUser!.cars;
    return Scaffold(
      appBar: widget.showArchived
          ? AppBar(
              backgroundColor: Theme.of(context).colorScheme.primary,
              centerTitle: true,
              title: Text("Archiv vozidel",
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.white)),
            )
          : null,
      body: Center(
          child: FutureBuilder<List<Car?>>(
        future: Future.wait(
            futureCars.map((carId) => carService.getItemById(carId))),
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if (snapshot.hasError) {
            return Center(child: Text("WRONG ${snapshot.error}"));
          }
          if (!snapshot.hasData) {
            return const Center(child: CircularProgressIndicator());
          }
          final List<Car?> cars = snapshot.data!;
          if (cars.isEmpty) {
            return Center(
                child: Column(
              children: [
                Image.asset("images/empty_drawing.png"),
                Text(widget.showArchived
                    ? "Žádné vozidlo nebylo archivováno"
                    : "Žádná vozidla nebyla přidána."),
              ],
            ));
          }
          mergeSort(cars);
          return ListView.separated(
              separatorBuilder: (_, __) => const Divider(),
              itemCount: cars.length,
              itemBuilder: (context, i) {
                return InkWell(
                    onTap: () {
                      Navigator.of(context)
                          .push(MaterialPageRoute(builder: (context) {
                        return InfoCarPage(
                          car: cars[i]!,
                          refresh: () {
                            setState(() {});
                          },
                          isArchived: widget.showArchived,
                        );
                      }));
                    },
                    child: CarRow(
                      car: cars[i]!,
                      refresh: () => setState(() {}),
                    ));
              });
        },
      )),
    );
  }
}
