import 'package:flutter/material.dart';
import 'package:garage/car/page/add_car_page.dart';
import 'package:garage/car/page/my_insurance_page.dart';
import 'package:garage/user/model/user_provider.dart';
import 'package:garage/user/page/user_page.dart';
import 'package:garage/utils/date_time_utils.dart';
import 'package:provider/provider.dart';

import 'car/page/my_garage_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int currentPageIndex = 0;

  @override
  void initState() {
    super.initState();

    final exp = context.read<UserProvider>().currentUser!.licenceExpiration;
    print("Expiruje");
    if (exp != null && exp.isBefore(DateTime.now().add(Duration(days: 30)))) {
      String warning = exp.isBefore(DateTime.now())
          ? "Řidičský průkaz expiroval dne:"
          : "Řidičský průkaz expiruje dne:";
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        await showDialog(
          context: context,
          builder: (BuildContext context) => Center(
            child: AlertDialog(
              insetPadding: EdgeInsets.all(10),
              contentPadding: EdgeInsets.all(0),
              content: SizedBox(
                height: 120,
                width: MediaQuery.of(context).size.width,
                child:
                    Column(mainAxisAlignment: MainAxisAlignment.end, children: [
                  Text(
                    "POZOR",
                    style: TextStyle(fontSize: 30),
                  ),
                  Text(
                    warning,
                    style: TextStyle(fontSize: 20),
                  ),
                  Text(
                    getPrettyDate(exp),
                    style: TextStyle(fontSize: 20),
                  ),
                ]),
              ),
              actionsAlignment: MainAxisAlignment.spaceBetween,
              actions: <Widget>[
                TextButton(
                    onPressed: () {
                      setState(() => currentPageIndex = 3);
                      Navigator.pop(context);
                    },
                    child: Text("Změnit datum platnosti")),
                TextButton(
                    onPressed: () => Navigator.pop(context), child: Text("Ok"))
              ],
            ),
          ),
        );
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: NavigationBar(
        onDestinationSelected: (int index) {
          setState(() {
            currentPageIndex = index;
          });
        },
        indicatorColor: Colors.lightBlueAccent,
        selectedIndex: currentPageIndex,
        destinations: const <Widget>[
          NavigationDestination(
            selectedIcon: Icon(Icons.garage),
            icon: Icon(Icons.garage_outlined),
            label: 'Moje STK',
          ),
          NavigationDestination(
            selectedIcon: Icon(Icons.receipt_long),
            icon: Icon(Icons.receipt_long_outlined),
            label: 'Moje pojištění',
          ),
          NavigationDestination(
            selectedIcon: Icon(Icons.add),
            icon: Icon(Icons.add_outlined),
            label: 'Přidat vozidlo',
          ),
          NavigationDestination(
            selectedIcon: Icon(Icons.person),
            icon: Icon(Icons.person_2_outlined),
            label: 'Uživatel',
          ),
        ],
      ),
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.primary,
        centerTitle: true,
        title: Text("Moje Garáž",
            style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)),
        actions: <Widget>[
          IconButton(
            icon: const Icon(
              Icons.notifications,
              color: Colors.white,
            ),
            onPressed: () {
              // Navigator.push(
              //     context,
              //     MaterialPageRoute(
              //       builder: (context) => NotificationPage(),
              //     ));
            },
          )
        ],
      ),
      body: [
        MyGaragePage(),
        MyInsurancePage(),
        AddCarPage(callback: () {
          setState(() {
            currentPageIndex = 0;
          });
        }),
        UserPage(),
      ][currentPageIndex],
    );
  }
}
