import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:nm/nm.dart';

import '../../../../../common/widgets/text_input_dialog.dart';
import '../../../business/wifi_controller.dart';

class AccessPointPasswordDialog extends ConsumerWidget {
  const AccessPointPasswordDialog({this.accessPoint, super.key});

  final NetworkManagerAccessPoint? accessPoint;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final TextEditingController pskController = TextEditingController();

    return Dialog(
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              'You are trying to connect to a protected network.',
              // TODO: constant font sizes
              style: TextStyle(fontSize: 20),
              textAlign: TextAlign.start,
            ),
            const SizedBox(
              height: 24,
            ),
            Container(
              constraints: const BoxConstraints(
                maxWidth: 300,
              ),
              child: TextInputDialog(
                title: 'Enter the WiFi password',
                controller: pskController,
                onReturnPress: () => context.pop(),
                hintText: 'Enter the WiFi password',
              ),
            ),
            const SizedBox(
              height: 24,
            ),
            ElevatedButton(
              onPressed: () {
                if (accessPoint != null) {
                  ref
                      .read(wifiControllerProvider.notifier)
                      .connectToWifiNetwork(
                        accessPoint: accessPoint!,
                        psk: pskController.text,
                      );
                }
                // close Connect AlertDialog
                context.pop();
                // close AccessPoint FullscreenDialog
                context.pop();
              },
              child: const Text('Connect'),
            ),
          ],
        ),
      ),
    );
  }
}
