import 'dart:async';

import 'package:flutter/material.dart';
import 'package:marianum_mobile/data/chatList/chatListProps.dart';
import 'package:marianum_mobile/screen/pages/timetable/timetable.dart';
import 'package:provider/provider.dart';
import 'screen/pages/files/files.dart';
import 'screen/pages/more/overhang.dart';
import 'screen/pages/talk/chatList.dart';
import 'screen/settings/settings.dart';

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  int currentPage = 0;

  @override
  void initState() {
    Timer.periodic(const Duration(seconds: 30), (Timer t) => {
      setState((){}),
    });

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      Provider.of<ChatListProps>(context, listen: false).run();
    });
    Timer.periodic(const Duration(minutes: 1), (timer) {
      Provider.of<ChatListProps>(context, listen: false).run();
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final PageController pageController = PageController();
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: const Text("Marianum Fulda"),
        actions: <Widget>[
          IconButton(
            padding: const EdgeInsets.only(right: 15),
            icon: const Icon(Icons.settings),
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) => const Settings()));
            },
          )
        ],
      ),
      body: Column(
        children: [
          Visibility(
            visible: false,
            child: LinearProgressIndicator(
              backgroundColor: Colors.transparent,
              valueColor: AlwaysStoppedAnimation(Theme.of(context).primaryColor),
              minHeight: 5,
            ),
          ),
          Flexible(
            child: PageView(
              controller: pageController,
              children: const [
                Timetable(),
                ChatList(),
                Files(),
                Overhang(),
              ],
              onPageChanged: (page) {
                setState(() {
                  currentPage = page;
                });
              },
            ),
          )
        ],
      ),

      bottomNavigationBar: BottomNavigationBar(
        items: [
          const BottomNavigationBarItem(icon: Icon(Icons.calendar_month), label: "Vertretung"),
          BottomNavigationBarItem(icon: Stack(
            children: [
              const Icon(Icons.chat),
              Consumer<ChatListProps>(
                builder: (context, value, child) {
                  if(value.primaryLoading()) return const SizedBox.shrink();
                  int messages = value.getRoomsResponse.data.map((e) => e.unreadMessages).reduce((a, b) => a+b);
                  return Visibility(
                    visible: messages > 0,
                    child: Positioned(
                      right: 0,
                      top: 0,
                      child: Container(
                        padding: const EdgeInsets.all(1),
                        decoration: BoxDecoration(
                          color: Theme.of(context).primaryColor,
                          borderRadius: BorderRadius.circular(6),
                        ),
                        constraints: const BoxConstraints(
                          minWidth: 13,
                          minHeight: 13,
                        ),
                        child: Text(
                          "$messages",
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 10,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                  );
                },
              )
            ],
          ), label: "Talk"),
          const BottomNavigationBarItem(icon: Icon(Icons.folder), label: "Dateien"),
          const BottomNavigationBarItem(icon: Icon(Icons.more_horiz), label: "Mehr"),
        ],
        selectedItemColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.grey,
        showUnselectedLabels: true,
        showSelectedLabels: true,

        currentIndex: currentPage,
        onTap: (item) {
          setState(() {
            currentPage = item;
            pageController.jumpToPage(item);
          });
        },
      ),
    );
  }
}