import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:wyatt_ui_kit_example/bars/bars.dart';
import 'package:wyatt_ui_kit_example/buttons/buttons.dart';
import 'package:wyatt_ui_kit_example/cards/cards.dart';
import 'package:wyatt_ui_kit_example/demo_page.dart';
import 'package:wyatt_ui_kit_example/loaders/loaders.dart';
import 'package:wyatt_ui_kit_example/rich_text_builders/rich_text_builders.dart';
import 'package:wyatt_ui_kit_example/text_input/text_inputs.dart';
import 'package:wyatt_ui_kit_example/theme/themes.dart';

const String title = 'Wyatt UIKit Example';

class Home extends StatefulWidget {
  const Home({super.key, this.forceIndex = 0});

  final int forceIndex;

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  // Simply add your demo page here.
  final List<DemoPage> pages = const [
    Cards(),
    Buttons(),
    Loaders(),
    RichTextBuilders(),
    TextInputs(),
    Bars(),
  ];

  int currentIndex = 0;

  @override
  void initState() {
    currentIndex = widget.forceIndex;
    super.initState();
  }

  List<Widget> _drawerTiles(BuildContext context) {
    final tiles = <Widget>[];
    for (var i = 0; i < pages.length; i++) {
      final page = pages[i];
      tiles.add(
        ListTile(
          title: Text(page.title),
          onTap: () {
            if (currentIndex != i) {
              setState(() {
                currentIndex = i;
              });
              Navigator.pop(context);
            }
          },
        ),
      );
    }

    return tiles;
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        drawer: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: _drawerTiles(context),
          ),
        ),
        appBar: AppBar(
          title: const Text(title),
          actions: [
            Row(
              children: [
                const Text('Mode'),
                Switch.adaptive(
                  value:
                      AdaptiveTheme.of(context).brightness == Brightness.dark,
                  onChanged: (_) {
                    AdaptiveTheme.of(context).brightness == Brightness.light
                        ? AdaptiveTheme.of(context).setDark()
                        : AdaptiveTheme.of(context).setLight();
                  },
                ),
              ],
            ),
            const Gap(30),
            Row(
              children: [
                const Text('Studio'),
                Switch.adaptive(
                  value: Themes.currentThemeIndex == 1,
                  onChanged: (_) {
                    setState(() {
                      Themes.currentThemeIndex =
                          (Themes.currentThemeIndex == 1) ? 0 : 1;
                    });
                    Themes.auto(context);
                  },
                ),
              ],
            )
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 8),
          child: pages[currentIndex],
        ),
      );
}
