// ignore_for_file: unused_element

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';
import 'package:vitalapp/architecture/values/features.dart';
import 'package:vitalapp/components/appbar.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/components/side_nav/defines.dart';
import 'package:vitalapp/components/side_nav/side_nav.dart';
import 'package:vitalapp/pages/patient/info/widgets/base.dart';
import 'package:vitalapp/store/store.dart';

import 'controller.dart';
import 'state.dart';
import 'widgets/detail.dart';
import 'widgets/health.dart';

class PatientInfoPage extends GetView<PatientInfoController> {
  const PatientInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: VAppBar(
        titleWidget: Obx(
          () => Text(
            "${controller.state.name}健康档案",
            style: const TextStyle(fontSize: 24),
          ),
        ),
      ),
      body: Stack(
        children: [
          VSideNavView(
            // navId: NavIds.CREATE,
            items: _buildItems(),
          ),
          Positioned(
            bottom: 24,
            left: 80,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                VButton(
                  label: "保存信息",
                  onTap: () async {
                    var result = await controller.updatePatientAsync();
                    if (result) {
                      PromptBox.toast("保存成功");
                      await controller.changePatient();
                    }
                    // else {
                    //   PromptBox.toast("保存失败");
                    // }
                    // Get.back();
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  List<VSideNavMenuItem> _buildItems() {
    final items = <VSideNavMenuItem>[];
    items.add(_buildBasicInformationItem());
    if (Store.user.hasFeature(FeatureKeys.HealthInformation)) {
      items.add(_buildDetailInformationItem()); //详细信息
      items.add(_buildHealthInformationItem());
    }
    return items;
  }

  VSideNavMenuItem _buildBasicInformationItem() {
    return VSideNavMenuItem(
      title: "基本信息",
      icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
      pageBuilder: (_) => const _NavWidget(anchor: NavAnchorType.base),
    );
  }

  VSideNavMenuItem _buildDetailInformationItem() {
    return VSideNavMenuItem(
      title: "详细信息",
      icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
      pageBuilder: (_) => const _NavWidget(anchor: NavAnchorType.detail),
    );
  }

  VSideNavMenuItem _buildHealthInformationItem() {
    return VSideNavMenuItem(
      title: "健康信息",
      icon: Icon(Icons.edit_document, color: Colors.grey.shade700),
      pageBuilder: (_) => const _NavWidget(anchor: NavAnchorType.health),
    );
  }
}

class _NavWidget extends GetView<PatientInfoController> {
  static final _pageBuilders = <NavAnchorType, WidgetBuilder>{
    NavAnchorType.base: (_) => const PatientInfoBaseView(),
    NavAnchorType.detail: (_) => const PatientInfoDetailView(),
    NavAnchorType.health: (_) => const PatientInfoHealthView(),
  };

  final NavAnchorType anchor;
  const _NavWidget({required this.anchor});

  @override
  Widget build(BuildContext context) {
    controller.state.anchor = anchor;
    return Stack(
      children: [
        Obx(() {
          final builder = _pageBuilders[controller.state.anchor];
          if (builder != null) return builder(context);
          return const Center(child: Text("Unknown"));
        }),
        // Positioned(
        //   bottom: 8,
        //   left: 200,
        //   right: 200,
        //   child: Row(
        //     mainAxisAlignment: MainAxisAlignment.spaceAround,
        //     children: [
        //       VButton(
        //         label: "保存信息",
        //         onTap: () async {
        //           var result = await controller.updatePatientAsync();
        //           if (result) {
        //             PromptBox.toast("保存成功");
        //             await controller.changePatient();
        //           } else {
        //             PromptBox.toast("保存失败");
        //           }

        //           Get.back();
        //         },
        //       ),
        //     ],
        //   ),
        // ),
      ],
    );
  }
}
