import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/alert_dialog.dart';

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

  @override
  State<StatefulWidget> createState() => _HelpDialogState();

  static void show() {
    Get.dialog(
      const VAlertDialog(
        width: 520,
        title: "帮助中心",
        contentPadding: EdgeInsets.symmetric(horizontal: 24),
        content: SizedBox(
          height: 280,
          child: HelpDialog(),
        ),
        // showCancel: false,
      ),
      barrierDismissible: true,
    );
  }
}

class _HelpDialogState extends State<HelpDialog> {
  List<Map<String, dynamic>>? _items;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadItems();
    });
  }

  Future<void> _loadItems() async {
    try {
      final jsonText = await rootBundle.loadString("assets/docs/help.json");
      final jsonMap = jsonDecode(jsonText);

      setState(() {
        _items =
            (jsonMap as List).map((e) => e as Map<String, dynamic>).toList();
      });
    } catch (e) {
      logger.e("HelpDialog - _loadItems error.", e);
    }
  }

  @override
  Widget build(BuildContext context) {
    if (_items == null) {
      return const Center(
        child: SizedBox(
          width: 60,
          height: 60,
          child: CircularProgressIndicator(),
        ),
      );
    }
    final children = <Widget>[];
    final count = _items!.length;
    for (int i = 0; i < count; i++) {
      if (i > 0) {
        children.add(const SizedBox(height: 12));
      }

      final e = _items![i];
      children.add(HelpCenterItem(e));
    }
    return ListView(
      shrinkWrap: false,
      padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 12),
      children: children,
    );
  }
}

class HelpCenterItem extends StatelessWidget {
  final Map<String, dynamic> itemMap;

  const HelpCenterItem(this.itemMap, {super.key});

  @override
  Widget build(BuildContext context) {
    final title = itemMap['title'];
    final content = itemMap['content'];
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HelpDialogItemTitle(title: title),
        const SizedBox(height: 4),
        HelpDialogItemContnt(content: content),
      ],
    );
  }
}

class HelpDialogItemTitle extends StatelessWidget {
  final String title;

  const HelpDialogItemTitle({super.key, required this.title});
  @override
  Widget build(BuildContext context) {
    return Text(
      title,
      style: const TextStyle(fontSize: 20),
    );
  }
}

class HelpDialogItemContnt extends StatelessWidget {
  final String content;

  const HelpDialogItemContnt({super.key, required this.content});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 12.0),
      child: Text(
        content,
        style: const TextStyle(fontSize: 16),
      ),
    );
  }
}
