import 'package:flutter/material.dart';

import 'composition/demo_scheduler_composition.dart';
import 'widgets/backlog_pane.dart';
import 'widgets/today_pane.dart';

class FocusFlowApp extends StatelessWidget {
  const FocusFlowApp({required this.composition, super.key});

  final DemoSchedulerComposition composition;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FocusFlow',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color(0xFF2F6F73),
          brightness: Brightness.dark,
        ),
        scaffoldBackgroundColor: const Color(0xFF101414),
        useMaterial3: true,
      ),
      home: SchedulerHome(composition: composition),
    );
  }
}

class SchedulerHome extends StatefulWidget {
  const SchedulerHome({required this.composition, super.key});

  final DemoSchedulerComposition composition;

  @override
  State<SchedulerHome> createState() => _SchedulerHomeState();
}

class _SchedulerHomeState extends State<SchedulerHome> {
  late final todayController = widget.composition.createTodayController();
  late final backlogController = widget.composition.createBacklogController();
  late final commandController = widget.composition.createCommandController(
    refreshReads: () async {
      await Future.wait([todayController.load(), backlogController.load()]);
    },
  );
  int selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    todayController.load();
    backlogController.load();
  }

  @override
  void dispose() {
    todayController.dispose();
    backlogController.dispose();
    commandController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('FocusFlow')),
      body: IndexedStack(
        index: selectedIndex,
        children: [
          TodayPane(
            controller: todayController,
            commandController: commandController,
          ),
          BacklogPane(
            controller: backlogController,
            commandController: commandController,
          ),
        ],
      ),
      bottomNavigationBar: NavigationBar(
        selectedIndex: selectedIndex,
        onDestinationSelected: (index) {
          setState(() {
            selectedIndex = index;
          });
        },
        destinations: const [
          NavigationDestination(
            icon: Icon(Icons.today_outlined),
            selectedIcon: Icon(Icons.today),
            label: 'Today',
          ),
          NavigationDestination(
            icon: Icon(Icons.inbox_outlined),
            selectedIcon: Icon(Icons.inbox),
            label: 'Backlog',
          ),
        ],
      ),
    );
  }
}
