import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'profile_screen.dart';
import 'shared_preferences_provider.dart';
import 'status_page.dart';

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (_) => SharedPreferencesProvider(),
      child: const MyApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Pogdark',
      theme: _buildTheme(),
      home: const HomeScreen(),
    );
  }

  ThemeData _buildTheme() {
    return ThemeData(
      primarySwatch: Colors.blue,
      visualDensity: VisualDensity.adaptivePlatformDensity,
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
        ),
      ),
    );
  }
}

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

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

class HomeScreenState extends State<HomeScreen> {
  late Future<void> _prefsReady;
  bool isProfileActive = false;
  bool showProfileInitially = false;

  @override
  void initState() {
    super.initState();

    final prefsProvider =
        Provider.of<SharedPreferencesProvider>(context, listen: false);
    _prefsReady = prefsProvider.ready.then((_) {
      if (mounted) {
        setState(() {
          showProfileInitially = prefsProvider.getUserName().isEmpty;
        });
      }
    });
  }

  void toggleProfileScreen() {
    setState(() => isProfileActive = !isProfileActive);
  }

  void closeInitialProfileScreen() {
    setState(() => showProfileInitially = false);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _prefsReady,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return Stack(
            children: [
              StatusPage(toggleProfile: toggleProfileScreen),
              if (showProfileInitially)
                ProfileOverlay(
                  isEditing: false,
                  onClose: closeInitialProfileScreen,
                ),
              if (isProfileActive && !showProfileInitially)
                ProfileOverlay(
                  isEditing: true,
                  onClose: toggleProfileScreen,
                ),
            ],
          );
        } else {
          return const Center(child: CircularProgressIndicator());
        }
      },
    );
  }
}

class ProfileOverlay extends StatelessWidget {
  final bool isEditing;
  final VoidCallback onClose;

  const ProfileOverlay(
      {super.key, required this.isEditing, required this.onClose});

  @override
  Widget build(BuildContext context) {
    return BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
      child: Container(
        color: Colors.black.withOpacity(0.3),
        child: Center(
          child: ProfileScreen(
            isEditing: isEditing,
            onClose: onClose,
          ),
        ),
      ),
    );
  }
}
