import 'dart:io';

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

import '../../../../common/widgets/scaffold_with_statusbar.dart';
import '../../business/wifi_controller.dart';
import 'widgets/ap_tile.dart';
import 'widgets/ap_tile_demo.dart';

// dialog api:
// https://api.flutter.dev/flutter/material/Dialog-class.html

// inspired from:
// https://github.com/canonical/nm.dart/blob/main/example/wifi.dart

class WifiSettingsScreen extends ConsumerWidget {
  const WifiSettingsScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final wifiRepository = (Platform.isLinux)
        ? ref.watch(wifiControllerProvider.notifier).wifiStream()
        : const Stream.empty();

    return ScaffoldWithStatusbar(
      appBar: AppBar(
        title: const Text('Wifi Settings'),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            StreamBuilder(
              stream: wifiRepository,
              builder: (BuildContext context, snapshot) {
                if (!Platform.isLinux) {
                  return ListView.builder(
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: 5,
                      itemBuilder: (BuildContext context, int index) {
                        return const AccessPointTileDemo();
                      });
                }
                if (snapshot.hasData) {
                  return Center(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(24.0),
                          child: Switch(
                            value: snapshot.data!.$2.wirelessEnabled,
                            onChanged: (_) {
                              snapshot.data!.$2.setWirelessEnabled(
                                !snapshot.data!.$2.wirelessEnabled,
                              );
                            },
                          ),
                        ),
                        Container(
                          constraints: const BoxConstraints(maxWidth: 500),
                          child: ListView.builder(
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            itemCount: snapshot
                                    .data!.$1.wireless?.accessPoints.length ??
                                0,
                            itemBuilder: (BuildContext context, int index) {
                              return AccessPointTile(
                                accessPoint: snapshot
                                    .data!.$1.wireless!.accessPoints
                                    .elementAt(index),
                                client: snapshot.data!.$2,
                                device: snapshot.data!.$1,
                              );
                            },
                          ),
                        ),
                      ],
                    ),
                  );
                }
                if (snapshot.hasError) return Text(snapshot.error.toString());
                return const Center(
                    child: Padding(
                  padding: EdgeInsets.only(top: 16.0),
                  child: CircularProgressIndicator(),
                ));
              },
            ),
          ],
        ),
      ),
    );
  }
}
