import 'package:flutter/material.dart';
import 'package:sheetless/core/services/sync_service.dart';

/// Callback for shuffle state changes.
typedef ShuffleCallback = void Function(bool enabled);

/// Navigation drawer for the home page.
///
/// Provides access to app-level actions like shuffle mode and logout.
class AppDrawer extends StatelessWidget {
  final bool isShuffling;
  final ShuffleCallback onShuffleChanged;
  final VoidCallback onLogout;
  final String? appName;
  final String? appVersion;
  final Future<SyncResult> syncFuture;

  const AppDrawer({
    super.key,
    required this.isShuffling,
    required this.onShuffleChanged,
    required this.onLogout,
    required this.syncFuture,
    this.appName,
    this.appVersion,
  });

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: SafeArea(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 30),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildActions(),
              Column(children: [_buildSyncStatus(), _buildAppInfo()]),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildActions() {
    return Column(
      children: [
        ListTile(
          leading: Icon(Icons.shuffle, color: isShuffling ? Colors.blue : null),
          title: const Text('Shuffle'),
          onTap: () => onShuffleChanged(!isShuffling),
        ),
        ListTile(
          leading: const Icon(Icons.logout),
          title: const Text('Logout'),
          onTap: onLogout,
        ),
      ],
    );
  }

  Widget _buildSyncStatus() {
    return Center(
      // padding: const EdgeInsets.all(5.0),
      child: FutureBuilder<SyncResult>(
        future: syncFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState != ConnectionState.done) {
            return const Center(child: CircularProgressIndicator());
          }

          if (snapshot.hasError) {
            return Text(
              "Error: ${snapshot.error.toString()}",
              style: const TextStyle(color: Colors.red),
              textAlign: TextAlign.center,
            );
          }

          if (snapshot.hasData) {
            final changes = snapshot.data!.changesUnsynced;
            final annotations = snapshot.data!.annotationsUnsynced;
            if (changes == 0 && annotations == 0) {
              return Text(
                "All synced!",
                style: const TextStyle(color: Colors.black),
                textAlign: TextAlign.center,
              );
            }
            return Text(
              "$changes changes and $annotations annotations unsynchronized!",
              style: const TextStyle(color: Colors.red),
              textAlign: TextAlign.center,
            );
          }

          return const Center(child: CircularProgressIndicator());
        },
      ),
    );
  }

  Widget _buildAppInfo() {
    final versionText = appName != null && appVersion != null
        ? '$appName v$appVersion'
        : 'Loading...';

    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Text(versionText, style: const TextStyle(color: Colors.grey)),
    );
  }
}
