import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'help.dart';
import 'hints.dart';
import 'main.dart';
import 'semesterticket.dart';
import 'studierendenausweis.dart';

class StartScreen extends StatelessWidget {
  final now = DateTime.now();

  @override
  Widget build(BuildContext context) {
    initializeDateFormatting('de_DE');

    return Theme(
        data: Theme.of(context).copyWith(
            textTheme: TextTheme(
                bodyMedium:
                    TextStyle(color: Theme.of(context).colorScheme.secondary))),
        child: Scaffold(
          appBar: AppBar(title: const Text('Ausweise'), actions: [
            IconButton(
              icon: const Icon(Icons.autorenew),
              tooltip: 'Tickets aktualisieren',
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                    content: Text('Die Tickets werden aktualisiert')));
              },
            ),
            IconButton(
                icon: const Icon(Icons.help_outline),
                tooltip: 'Hilfe öffnen',
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => ShowHelp()));
                })
          ]),
          body: ListView.builder(
              itemCount: (now.month == 3 || now.month == 9) ? 6 : 4,
              padding:
                  const EdgeInsets.only(top: 4, bottom: 8, left: 4, right: 4),
              itemBuilder: (BuildContext context, int index) {
                switch (index) {
                  case 0:
                    return Text(
                        'Angemeldet als ${Ticket().nachname}, ${Ticket().vorname}');
                  case 1:
                    return GestureDetector(
                      onTap: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => ShowHints(index)));
                      },
                      child: Card(
                        semanticContainer: true,
                        margin: const EdgeInsets.only(top: 4, bottom: 4),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: const [
                            Icon(Icons.library_books_outlined),
                            Padding(
                              padding: EdgeInsets.all(16.0),
                              child: Text(
                                "Hinweise anzeigen",
                                textAlign: TextAlign.center,
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  default:
                    return createTicket(context, index);
                }
              }),
        ));
  }

  Widget createTicket(BuildContext context, final int index) {
    final bool isSemester = index % 2 == 0;
    final bool isWinter = index < 4
        ? now.month >= 10 || now.month <= 3
        : now.month >= 4 && now.month <= 9;

    return GestureDetector(
        onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => isSemester
                      ? ShowSemester(isWinter, now)
                      : ShowAusweis(isWinter, now)));
        },
        child: Card(
            semanticContainer: true,
            margin: const EdgeInsets.only(top: 8, bottom: 8, left: 4, right: 4),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                            isSemester
                                ? 'Semesterticket'
                                : 'Studierendenausweis',
                            style: const TextStyle(
                                fontWeight: FontWeight.w700, fontSize: 18)),
                        const Icon(
                          Icons.arrow_forward_ios_outlined,
                          weight: 100,
                        )
                      ],
                    ),
                    const SizedBox(height: 16),
                    Text((isWinter
                            ? 'Wintersemester ${now.month > 6 ? now.year : now.year - 1}'
                            : 'Sommersemester ${now.year}') +
                        (isSemester
                            ? ''
                            : ', Matrikelnummer ${Ticket().matrikel}')),
                    const SizedBox(height: 24),
                    Row(children: [
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: const [
                            Text('Zuletzt aktualisiert:',
                                style: TextStyle(fontSize: 10)),
                            Text('Offline verfügbar bis:',
                                style: TextStyle(fontSize: 10)),
                          ]),
                      const SizedBox(
                        width: 24,
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                              DateFormat("EEEE, dd. MMMM yyyy HH:mm", 'de_DE')
                                  .format(now),
                              style: const TextStyle(fontSize: 10)),
                          Text(
                              DateFormat("EEEE, dd. MMMM yyyy 23:59", 'de_DE')
                                  .format(now
                                      .add(Duration(days: isSemester ? 2 : 7))),
                              style: const TextStyle(fontSize: 10)),
                        ],
                      )
                    ]),
                  ]),
            )));
  }
}