import 'package:hmg_patient_app/core/model/labs/lab_result.dart';
import 'package:hmg_patient_app/core/model/labs/patient_lab_orders.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:readmore/readmore.dart';

import '../FlowChartPage.dart';
import 'labWidgets.dart';

class LabItem extends StatefulWidget {
  final LabResultList item;
  PatientLabOrders? patientLabOrders;

  LabItem({super.key, required this.item, this.patientLabOrders});

  @override
  State<LabItem> createState() => _LabItemState();
}

class _LabItemState extends State<LabItem> {
  bool _isShowMoreGeneral = true;
  bool showBottomSheet = false;

  @override
  void initState() {
    super.initState();
    if (showBottomSheet) {
      print('the bottom sheet is showing');
      openFlowChart(context, '');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        GestureDetector(
          onTap: () {
            setState(() {
              _isShowMoreGeneral = !_isShowMoreGeneral;
            });
          },
          behavior: HitTestBehavior.opaque,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: Text(
                  widget.item.filterName ?? '',
                  style: TextStyle(fontSize: 21, fontWeight: FontWeight.bold, color: Color(0xff2B353E), letterSpacing: -0.64, height: 25 / 16),
                ),
              ),
              Icon(
                _isShowMoreGeneral ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                color: Color(0xff2B353E),
                size: 26,
              )
            ],
          ),
        ),
        SizedBox(
          height: 12,
        ),
        ReadMoreText(
          widget.item.description ?? '',
          trimMode: TrimMode.Line,
          trimLines: 3,
          style: const TextStyle(color: Color(0xff575757), fontSize: 11),
          colorClickableText: CustomColors.accentColor,
          trimCollapsedText: '${TranslationBase.of(context).readMore}',
          trimExpandedText: ' show less',
        ),
        SizedBox(
          height: 8,
        ),
        if (_isShowMoreGeneral)
          ...List.generate(widget.item.patientLabResultList?.length ?? 0, (index) {
            var data = widget.item.patientLabResultList?[index];
            return Column(
              children: [
                ItemResultCardWidgetWithParams(
                  title: data?.description ?? '',
                  subTitle: data?.resultValue ?? '',
                  referenceRange: data?.referanceRange ?? '',
                  percentage: data?.percentage ?? 0.0,
                  note: data?.notes ?? '',
                  source: "",
                  buttonText: "",
                  type: data?.calculatedResultFlag?.getType() ?? ResultTypes.unknown,
                  shouldShowResultBarAndGraph: data?.shouldShowResultBarAndGraph() ?? true,
                  onButtonPressed: () {
                    openFlowChart(context, data?.description ?? '');
                  },
                ),
                SizedBox(
                  height: 13,
                ),
              ],
            );
          })
        // Expanded(
        //     child: ListView.builder(
        //         itemCount: widget.item.patientLabResultList?.length ?? 0,
        //         itemBuilder: (context, index) {
        //           var data = widget.item.patientLabResultList?[index];
        //           Column(
        //             children: [
        //               EllipsisTextWithMore(
        //                 text: data?.packageShortDescription ?? '',
        //               ),
        //               SizedBox(
        //                 height: 8,
        //               ),
        //               ItemResultCardWidgetWithParams(
        //                   title: data?.description ?? '',
        //                   subTitle: data?.resultValue ?? '',
        //                   referenceRange: data?.referanceRange ?? '',
        //                   percentage: 20,
        //                   note: data?.testShortDescription ?? '',
        //                   source: "",
        //                   buttonText: "",
        //                   type: data?.calculatedResultFlag?.getType() ??
        //                       ResultTypes.unknown),
        //             ],
        //           );
        //         }))
      ],
    );
  }

  openFlowChart(BuildContext context, String procedure) {
    print('openFlowChart: the bottom sheet is showing');

    showModalBottomSheet(
        backgroundColor: Colors.white,
        isScrollControlled: true,
        context: context,
        scrollControlDisabledMaxHeightRatio: .75,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20))),
        builder: (context) {
          return FlowChartPage(
            filterName: procedure,
            patientLabOrder: widget.patientLabOrders,
          );
        }).then((value) {
      setState(() {
        showBottomSheet = false;
      });
    });
  }
}
