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

import '../../i18n/strings.g.dart';
import '../../mixins/mounted_set_state_mixin.dart';
import '../../providers/companion_remote_provider.dart';
import '../../services/companion_remote/companion_remote_host_controller.dart';
import '../../services/settings_service.dart';
import '../../focus/focusable_button.dart';
import '../../focus/key_event_utils.dart';

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

  @override
  State<RemoteSessionDialog> createState() => _RemoteSessionDialogState();

  static Future<void> show(BuildContext context) {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (context) => const RemoteSessionDialog(),
    );
  }
}

class _RemoteSessionDialogState extends State<RemoteSessionDialog> with MountedSetStateMixin {
  bool _isStarting = false;
  String? _errorMessage;
  final _closeFocusNode = FocusNode(debugLabel: 'RemoteSessionDialog.close');
  final _toggleFocusNode = FocusNode(debugLabel: 'RemoteSessionDialog.toggle');
  final _minimizeFocusNode = FocusNode(debugLabel: 'RemoteSessionDialog.minimize');
  final _errorCloseFocusNode = FocusNode(debugLabel: 'RemoteSessionDialog.errorClose');
  final _errorRetryFocusNode = FocusNode(debugLabel: 'RemoteSessionDialog.errorRetry');

  @override
  void dispose() {
    _closeFocusNode.dispose();
    _toggleFocusNode.dispose();
    _minimizeFocusNode.dispose();
    _errorCloseFocusNode.dispose();
    _errorRetryFocusNode.dispose();
    super.dispose();
  }

  Future<void> _startServer() async {
    setState(() {
      _isStarting = true;
      _errorMessage = null;
    });

    try {
      final settings = await SettingsService.getInstance();
      await settings.write(SettingsService.enableCompanionRemoteServer, true);
      if (!mounted) return;
      final started = await startCompanionRemoteHost(context);
      if (!mounted) return;
      final provider = context.read<CompanionRemoteProvider>();
      setState(() {
        _isStarting = false;
        _errorMessage = started ? null : provider.session?.errorMessage ?? t.companionRemote.pairing.cryptoInitFailed;
      });
    } catch (e) {
      setStateIfMounted(() {
        _isStarting = false;
        _errorMessage = e.toString();
      });
    }
  }

  Future<void> _stopServer() async {
    final settings = await SettingsService.getInstance();
    await settings.write(SettingsService.enableCompanionRemoteServer, false);
    if (!mounted) return;
    await context.read<CompanionRemoteProvider>().stopHostServer();
  }

  Future<void> _toggleServer() async {
    final provider = context.read<CompanionRemoteProvider>();
    if (provider.isHostServerRunning) {
      await _stopServer();
    } else {
      await _startServer();
    }
  }

  void _close() => Navigator.of(context).pop();

  @override
  Widget build(BuildContext context) {
    return Focus(
      canRequestFocus: false,
      onKeyEvent: (node, event) => handleBackKeyNavigation(context, event),
      child: Consumer<CompanionRemoteProvider>(
        builder: (context, provider, child) {
          if (_isStarting) {
            return Dialog(
              child: Padding(
                padding: const EdgeInsets.all(32.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    const CircularProgressIndicator(),
                    const SizedBox(height: 16),
                    Text(t.companionRemote.session.startingServer, style: Theme.of(context).textTheme.titleMedium),
                  ],
                ),
              ),
            );
          }

          if (_errorMessage != null) {
            return AlertDialog(
              title: Text(t.common.error),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(t.companionRemote.session.failedToCreate),
                  const SizedBox(height: 8),
                  Text(_errorMessage!, style: const TextStyle(fontFamily: 'monospace')),
                ],
              ),
              actions: [
                FocusableButton(
                  autofocus: true,
                  focusNode: _errorCloseFocusNode,
                  onPressed: _close,
                  onBack: _close,
                  onNavigateRight: () => _errorRetryFocusNode.requestFocus(),
                  useBackgroundFocus: true,
                  child: TextButton(onPressed: _close, child: Text(t.common.close)),
                ),
                FocusableButton(
                  focusNode: _errorRetryFocusNode,
                  onPressed: _startServer,
                  onBack: _close,
                  onNavigateLeft: () => _errorCloseFocusNode.requestFocus(),
                  useBackgroundFocus: true,
                  child: TextButton(onPressed: _startServer, child: Text(t.common.retry)),
                ),
              ],
            );
          }

          return Dialog(
            child: ConstrainedBox(
              constraints: const BoxConstraints(maxWidth: 500),
              child: SingleChildScrollView(
                padding: const EdgeInsets.all(24.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Row(
                      children: [
                        const Icon(Icons.phone_android, size: 32),
                        const SizedBox(width: 16),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(t.companionRemote.title, style: Theme.of(context).textTheme.headlineSmall),
                              const SizedBox(height: 4),
                              _buildStatusLine(context, provider),
                            ],
                          ),
                        ),
                        FocusableButton(
                          focusNode: _closeFocusNode,
                          onPressed: _close,
                          onBack: _close,
                          onNavigateDown: () => _toggleFocusNode.requestFocus(),
                          useBackgroundFocus: true,
                          child: IconButton(icon: const Icon(Icons.close), onPressed: _close),
                        ),
                      ],
                    ),
                    const SizedBox(height: 24),

                    _buildServerStatus(context, provider),

                    if (provider.connectedDevice != null) ...[
                      const SizedBox(height: 16),
                      _buildConnectedDevice(context, provider),
                    ],

                    const SizedBox(height: 24),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        FocusableButton(
                          autofocus: true,
                          focusNode: _toggleFocusNode,
                          onPressed: _toggleServer,
                          onBack: _close,
                          onNavigateUp: () => _closeFocusNode.requestFocus(),
                          onNavigateRight: () => _minimizeFocusNode.requestFocus(),
                          useBackgroundFocus: true,
                          child: TextButton.icon(
                            onPressed: _toggleServer,
                            icon: Icon(provider.isHostServerRunning ? Icons.stop : Icons.play_arrow),
                            label: Text(
                              provider.isHostServerRunning
                                  ? t.companionRemote.session.stopServer
                                  : t.companionRemote.session.startServer,
                            ),
                          ),
                        ),
                        const SizedBox(width: 8),
                        FocusableButton(
                          focusNode: _minimizeFocusNode,
                          onPressed: _close,
                          onBack: _close,
                          onNavigateUp: () => _closeFocusNode.requestFocus(),
                          onNavigateLeft: () => _toggleFocusNode.requestFocus(),
                          useBackgroundFocus: true,
                          child: FilledButton(onPressed: _close, child: Text(t.companionRemote.session.minimize)),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }

  Widget _buildStatusLine(BuildContext context, CompanionRemoteProvider provider) {
    if (provider.connectedDevice != null) {
      return Text(
        t.companionRemote.connectedTo(name: provider.connectedDevice!.name),
        style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: Colors.green),
      );
    }
    if (provider.isHostServerRunning) {
      return Text(
        t.companionRemote.session.serverRunning,
        style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: Theme.of(context).textTheme.bodySmall?.color),
      );
    }
    return Text(
      t.companionRemote.session.serverStopped,
      style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: Colors.grey),
    );
  }

  Widget _buildServerStatus(BuildContext context, CompanionRemoteProvider provider) {
    final isRunning = provider.isHostServerRunning;

    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Row(
          children: [
            Container(
              width: 12,
              height: 12,
              decoration: BoxDecoration(shape: BoxShape.circle, color: isRunning ? Colors.green : Colors.grey),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    isRunning ? t.companionRemote.session.serverRunning : t.companionRemote.session.serverStopped,
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                  const SizedBox(height: 4),
                  Text(
                    isRunning
                        ? t.companionRemote.session.serverRunningDescription
                        : t.companionRemote.session.serverStoppedDescription,
                    style: Theme.of(context).textTheme.bodySmall,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildConnectedDevice(BuildContext context, CompanionRemoteProvider provider) {
    final device = provider.connectedDevice!;
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            const Icon(Icons.check_circle, color: Colors.green, size: 48),
            const SizedBox(height: 8),
            Text(t.companionRemote.session.connected, style: Theme.of(context).textTheme.titleLarge),
            const SizedBox(height: 8),
            Text(device.name, style: Theme.of(context).textTheme.bodyLarge),
            Text(device.platform, style: Theme.of(context).textTheme.bodySmall),
            const SizedBox(height: 12),
            Text(
              t.companionRemote.session.usePhoneToControl,
              style: Theme.of(context).textTheme.bodyMedium,
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
    );
  }
}
