import 'dart:io';

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

import '../../features/settings/business/wifi_controller.dart';
import '../../features/settings/presentation/wifi_settings_screen/widgets/wifi_bar_icon.dart';
import 'clock.dart';

const horizontalSpace = SizedBox(
  width: 8,
);

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

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

    return Container(
      width: double.infinity,
      height: 24,
      color: Colors.black,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Row(
            children: [
              horizontalSpace,
              kDebugMode
                  ? Text(
                      'DEBUG MODE',
                      style: TextStyle(color: Colors.white),
                    )
                  : SizedBox(),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              StreamBuilder(
                stream: wifiStream,
                builder: (context, snapshot) {
                  if (!Platform.isLinux) {
                    return const Row(
                      children: [
                        Text(
                          'DemoConnection',
                          style: TextStyle(color: Colors.white),
                        ),
                        horizontalSpace,
                        WifiBarIcon(
                          strength: 1,
                          size: 16,
                          color: Colors.white,
                        ),
                      ],
                    );
                  }
                  if (snapshot.hasData) {
                    final data = snapshot.data?.$1 as NetworkManagerDevice;
                    return Row(
                      children: [
                        Text(
                          data.activeConnection?.id ?? 'No connection',
                          style: const TextStyle(color: Colors.white),
                        ),
                        horizontalSpace,
                        WifiBarIcon(
                          strength:
                              data.wireless?.activeAccessPoint?.strength ?? 0,
                          size: 16,
                          color: Colors.white,
                        ),
                      ],
                    );
                  }
                  if (snapshot.hasError) {
                    // error can be caught here
                    // display error in statusbar:
                    // return Text(
                    //   snapshot.error.toString(),
                    //   style: const TextStyle(color: Colors.white),
                    // );
                    // show nothing (hide error message from user):
                    // the error that can happen here is mostly related
                    // to not finding any usable wifi device
                    return const SizedBox();
                  }
                  return const Text(
                    "No connection",
                    style: TextStyle(color: Colors.white),
                  );
                },
              ),
              horizontalSpace,
              const ClockWidget(
                textStyle: TextStyle(color: Colors.white),
              ),
              horizontalSpace,
            ],
          ),
        ],
      ),
    );
  }
}
