import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/components/error_display/error_display.dart';
import 'package:simplecloudnotifier/models/channel.dart';
import 'package:simplecloudnotifier/models/scan_result.dart';
import 'package:simplecloudnotifier/models/subscription.dart';
import 'package:simplecloudnotifier/models/user.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/utils/toaster.dart';

class ChannelScannerResultChannelView extends StatefulWidget {
  final ScanResultChannel value;

  const ChannelScannerResultChannelView({required this.value}) : super();

  @override
  State<ChannelScannerResultChannelView> createState() => _ChannelScannerResultChannelViewState();
}

class _ChannelScannerResultChannelViewState extends State<ChannelScannerResultChannelView> {
  Future<(ChannelPreview, UserPreview)?> _fetchDataFuture;

  _ChannelScannerResultChannelViewState() : _fetchDataFuture = Future.value(null); // Initial dummy future

  @override
  void initState() {
    super.initState();

    final auth = Provider.of<AppAuth>(context, listen: false);
    setState(() {
      _fetchDataFuture = _fetchData(auth);
    });
  }

  Future<(ChannelPreview, UserPreview)?> _fetchData(AppAuth auth) async {
    ChannelPreview? channel = null;
    try {
      channel = await APIClient.getChannelPreview(auth, widget.value.channelID);
    } catch (e, stackTrace) {
      Toaster.error("Error", 'Failed to fetch channel preview: ${e.toString()}');
      ApplicationLog.error('Failed to fetch channel (preview) for ${widget.value.channelID}', trace: stackTrace);
      return null;
    }

    UserPreview? user = null;
    try {
      user = await APIClient.getUserPreview(auth, widget.value.ownerUserID);
    } catch (e, stackTrace) {
      Toaster.error("Error", 'Failed to fetch user preview: ${e.toString()}');
      ApplicationLog.error('Failed to fetch user (preview) for ${widget.value.ownerUserID}', trace: stackTrace);
      return null;
    }

    return (channel, user);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<(ChannelPreview, UserPreview)?>(
      future: _fetchDataFuture,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        }

        if (snapshot.hasError) {
          return ErrorDisplay(errorMessage: '${snapshot.error}');
        }

        if (snapshot.data == null) {
          return Column(
            spacing: 32,
            children: [
              Icon(FontAwesomeIcons.solidTriangleExclamation, size: 64, color: Colors.red[900]),
              Text("Failed to parse QR", textAlign: TextAlign.center),
            ],
          );
        }

        final (channel, user) = snapshot.data!;

        return Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text("SCN Channel", style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 20), textAlign: TextAlign.center),
            const SizedBox(height: 16),
            Row(
              children: [
                ConstrainedBox(child: Text("Name: ", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(channel.displayName), scrollDirection: Axis.horizontal)),
              ],
            ),
            Row(
              children: [
                ConstrainedBox(child: Text("InternalName: ", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(channel.internalName, style: const TextStyle(fontStyle: FontStyle.italic)), scrollDirection: Axis.horizontal)),
              ],
            ),
            Row(
              children: [
                ConstrainedBox(child: Text("ChannelID: ", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(channel.channelID, style: const TextStyle(fontStyle: FontStyle.italic)), scrollDirection: Axis.horizontal)),
              ],
            ),
            Row(
              children: [
                ConstrainedBox(child: Text("Messages: ", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(channel.messagesSent.toString()), scrollDirection: Axis.horizontal)),
              ],
            ),
            if (channel.descriptionName != null && channel.descriptionName!.isNotEmpty)
              Row(
                children: [
                  ConstrainedBox(child: Text("Description:", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                  Expanded(child: SingleChildScrollView(child: Text(channel.descriptionName!), scrollDirection: Axis.horizontal)),
                ],
              ),
            const SizedBox(height: 16),
            Row(
              children: [
                ConstrainedBox(child: Text("Owner:", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(user.username ?? user.userID), scrollDirection: Axis.horizontal)),
              ],
            ),
            Row(
              children: [
                ConstrainedBox(child: Text("UserID:", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(user.userID, style: const TextStyle(fontStyle: FontStyle.italic)), scrollDirection: Axis.horizontal)),
              ],
            ),
            const SizedBox(height: 16),
            Row(
              children: [
                ConstrainedBox(child: Text("Status:", style: const TextStyle(fontWeight: FontWeight.bold)), constraints: const BoxConstraints(minWidth: 100)),
                Expanded(child: SingleChildScrollView(child: Text(_formatSubscriptionStatus(channel.subscription)), scrollDirection: Axis.horizontal)),
              ],
            ),
            const SizedBox(height: 48),
            Text('QR Code contains no subscription-key\nCannot subscribe to channel', textAlign: TextAlign.center, style: const TextStyle(fontStyle: FontStyle.italic)),
          ],
        );
      },
    );
  }

  String _formatSubscriptionStatus(Subscription? sub) {
    if (sub == null) {
      return "Not Subscribed";
    } else if (sub.confirmed) {
      if (sub.active) {
        return "Already Subscribed";
      } else {
        return "Already Subscribed (inactive)";
      }
    } else {
      return "Unconfirmed Subscription";
    }
  }
}