import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/labs/patient_lab_orders.dart';
import 'package:doctor_app_flutter/core/viewModel/labs_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/lab_result/LabResultWidget.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/error_message.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';

class LaboratoryResultWidget extends StatefulWidget {
  final GestureTapCallback onTap;
  final String billNo;
  final String? details;
  final String orderNo;
  final PatientLabOrders patientLabOrder;
  final PatiantInformtion patient;
  final bool isInpatient;

  const LaboratoryResultWidget(
      {Key? key,
      required this.onTap,
      required this.billNo,
      required this.details,
      required this.orderNo,
      required this.patientLabOrder,
      required this.patient,
      required this.isInpatient})
      : super(key: key);

  @override
  _LaboratoryResultWidgetState createState() => _LaboratoryResultWidgetState();
}

class _LaboratoryResultWidgetState extends State<LaboratoryResultWidget> {
  bool _isShowMoreGeneral = true;
  bool _isShowMore = true;
  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return BaseView<LabsViewModel>(
      onModelReady: (model) => model.setLabResultDependOnFilterName(),
      // model.getPatientLabResult(
      //     patientLabOrder: widget.patientLabOrder,
      //     patient: widget.patient,
      //     isInpatient: widget.isInpatient),
      builder: (_, model, w) => NetworkBaseView(
        baseViewModel: model,
        child: Container(
          margin: EdgeInsets.all(15),
          child: Column(
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    height: 12,
                  ),
                  if (model.labResultLists.isNotEmpty)
                    Container(
                      child: Column(
                        children: [
                          InkWell(
                            onTap: () {
                              setState(
                                () {
                                  _isShowMoreGeneral = !_isShowMoreGeneral;
                                },
                              );
                            },
                            child: Container(
                              padding: EdgeInsets.all(10.0),
                              margin: EdgeInsets.only(left: 5, right: 5),
                              decoration: BoxDecoration(
                                  shape: BoxShape.rectangle,
                                  color: Colors.white,
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(5.0),
                                  ),),
                              child: Row(
                                children: <Widget>[
                                  Expanded(
                                      child: Container(
                                          margin: EdgeInsets.only(left: 0, right: 0),
                                          child: AppText(
                                            TranslationBase.of(context).generalResult,
                                            fontSize: SizeConfig.textMultiplier * 2.3,
                                            bold: false,
                                          ))),
                                  Container(
                                    width: 25,
                                    height: 25,
                                    child: Icon(
                                      _isShowMoreGeneral ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                                      color: Colors.grey[800],
                                      size: 22,
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ),
                          if (_isShowMoreGeneral)
                            AnimatedContainer(
                              padding: EdgeInsets.all(10.0),
                              margin: EdgeInsets.only(left: 5, right: 5),
                              decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                color: Colors.white,
                                borderRadius: BorderRadius.only(
                                  bottomLeft: Radius.circular(5.0),
                                  bottomRight: Radius.circular(5.0),
                                ),
                              ),
                              duration: Duration(milliseconds: 7000),
                              child: Container(
                                width: double.infinity,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    ListView.builder(
                                        itemCount: model.labResultLists.length,
                                        scrollDirection: Axis.vertical,
                                        physics: ScrollPhysics(),
                                        shrinkWrap: true,
                                        itemBuilder: (context, index) {
                                          return LabResultWidget(
                                        patientLabOrder: widget.patientLabOrder,
                                        filterName: model.labResultLists[index].filterName,
                                        patientLabResultList: model.labResultLists[index].patientLabResultList,
                                        patient: widget.patient,
                                        isInpatient: widget.isInpatient,
                                      );
                                        }),
                                  ],
                                ),
                              ),
                            ),
                        ],
                      ),
                    )
                  else if (widget.details == null)
                    Container(
                      child: ErrorMessage(
                        error: TranslationBase.of(context).noDataAvailable,
                      ),
                    ),
                  SizedBox(
                    height: 15,
                  ),
                  if (widget.details != null && widget.details!.isNotEmpty)
                    Column(
                      children: [
                        InkWell(
                          onTap: () {
                            setState(() {
                              _isShowMore = !_isShowMore;
                            });
                          },
                          child: Container(
                            padding: EdgeInsets.all(10.0),
                            margin: EdgeInsets.only(left: 5, right: 5),
                            decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                color: Colors.white,
                                borderRadius: BorderRadius.all(
                                  Radius.circular(5.0),
                                )),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                    child: Container(
                                        margin: EdgeInsets.only(left: 0, right: 0),
                                        child: AppText(
                                          TranslationBase.of(context).specialResult,
                                          fontSize: SizeConfig.textMultiplier * 2.3,
                                          bold: false,
                                        ))),
                                Container(
                                  width: 25,
                                  height: 25,
                                  child: Icon(
                                    _isShowMore ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                                    color: Colors.grey[800],
                                    size: 22,
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                        if (_isShowMore)
                          AnimatedContainer(
                            padding: EdgeInsets.all(10.0),
                            margin: EdgeInsets.only(left: 5, right: 5),
                            decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                color: Colors.white,
                                borderRadius: BorderRadius.only(
                                  bottomLeft: Radius.circular(5.0),
                                  bottomRight: Radius.circular(5.0),
                                )),
                            duration: Duration(milliseconds: 7000),
                            child: Container(
                              width: double.infinity,
                              child: !Helpers.isTextHtml(widget.details!)
                                  ? AppText(
                                      widget.details ?? TranslationBase.of(context).noDataAvailable,
                                    )
                                  : Html(
                                      data: widget.details ?? TranslationBase.of(context).noDataAvailable,
                                    ),
                            ),
                          ),
                        SizedBox(
                          height: 10,
                        ),
                      ],
                    ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
