import 'dart:math';

import 'package:flutter/material.dart';
import 'package:gymlink_module_web/main_mobile.dart';
import 'package:gymlink_module_web/providers/main.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(const MyExampleApp());
}

class MyExampleApp extends StatelessWidget {
  const MyExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'GymLink Example App',
      debugShowCheckedModeBanner: false,
      home: const ExampleMainPage(),
      theme: ThemeData.dark(useMaterial3: true),
    );
  }
}

class ExamplePage extends StatefulWidget {
  const ExamplePage({super.key});

  @override
  State<ExamplePage> createState() => _ExamplePageState();
}

Widget getDrawer(BuildContext context) => Drawer(
      child: Column(
        children: [
          const DrawerHeader(child: Text('Drawer Header')),
          ListTile(
            leading: const Icon(Icons.home),
            title: const Text('Home'),
            onTap: () => Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => const ExampleMainPage(),
              ),
            ),
          ),
          ListTile(
            leading: const Icon(Icons.search),
            title: const Text('Example page'),
            onTap: () => Navigator.of(context).push(MaterialPageRoute(
              builder: (context) => const ExampleSecondPage(),
            )),
          ),
        ],
      ),
    );

class ExampleMainPage extends StatelessWidget {
  const ExampleMainPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (_) => GymLinkProvider(),
        child: Consumer<GymLinkProvider>(
          builder: (_, value, __) => const ExamplePage(),
        ));
  }
}

class _ExamplePageState extends State<ExamplePage> {
  @override
  void initState() {
    super.initState();
    Future.microtask(
        () => context.read<GymLinkProvider>().onTokenReceived('token123'));
    Future.microtask(() => context
        .read<GymLinkProvider>()
        .setTheme(ThemeData.dark(useMaterial3: true)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GymLink Example App'),
      ),
      drawer: getDrawer(context),
      body: Column(
        children: [
          const Text('test'),
          IconButton(
            icon: const Icon(Icons.abc),
            onPressed: () {
              context.read<GymLinkProvider>().onTokenReceived('token123');
            },
          ),
          const Expanded(
            child: MyApp(),
          ),
          const SizedBox(
            height: 20,
          ),
          const Text('Bottom text')
        ],
      ),
      floatingActionButton: IconButton(
        icon: const Icon(Icons.search),
        onPressed: () {
          context
              .read<GymLinkProvider>()
              .changeTheme(Random().nextInt(0xffffff + 1));
        },
      ),
    );
  }
}

class ExampleSecondPage extends StatelessWidget {
  const ExampleSecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GymLink Example App'),
      ),
      drawer: getDrawer(context),
      body: const Center(
        child: Text('Example page'),
      ),
    );
  }
}
