import 'package:ernaehrung/pages/nav_pages/subpages/statistics_page/statistics_page.dart';
import 'package:ernaehrung/pages/nav_pages/subpages/today_page/today_page.dart';
import 'package:flutter/material.dart';
import '../../services/statistics.dart';
import 'subpages/progress_page/sub_components/charts/sub_components/tabs/timespan_tabs.dart';
import '../user_data_form/user_data_form.dart';
import 'subpages/progress_page/progress_page.dart';

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

  @override
  MainPageState createState() => MainPageState();
}

class MainPageState extends State<MainPage> {
  List pages = [
    const TodayPage(title: 'Essensplan'),
    const ProgressPage(title: 'Gesamtübersicht'),
    const StatisticsPage(title: 'Statistiken')
  ];

  int currentIndex = 0;

  void onTap(int index) async {
    setState(() {
      currentIndex = index;
      if (currentIndex == 1) {
        DataService.instance
            .updateStatisticsTodayBoxByTimespan(TimeSpan.day);
      } else if (currentIndex == 2) {
        DataService.instance.updateProgressBoxValues();
      }
      pages[currentIndex];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: false,
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
            pages[currentIndex].title,
            style: const TextStyle(
            color: Colors.black
          ),
        ),
            ElevatedButton(
              onPressed: () async {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => const SettingsPage()),
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.green, // Set the background color to green
              ),
              child: Row(
                children: const [
                  Icon(
                    Icons.account_circle,
                    size: 30,
                    color: Colors.white,
                  ),
                ],
              ),
            )

          ],
        ),
        backgroundColor: Colors.transparent,
        foregroundColor: Colors.grey.shade400,
        elevation: 0,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8),
        child: pages[currentIndex],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        selectedItemColor: Colors.black54,
        unselectedItemColor: Colors.grey.withOpacity(0.5),
        showSelectedLabels: false,
        showUnselectedLabels: false,
        elevation: 0,
        onTap: onTap,
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.today), label: 'Today'),
          BottomNavigationBarItem(
              icon: Icon(Icons.area_chart), label: 'Progress'),
          BottomNavigationBarItem(icon: Icon(Icons.apple), label: 'Meal Plan'),
        ],
      ),
    );
  }
}
