import 'package:diplomaticquarterapp/core/model/labs/lab_result.dart';
import 'package:diplomaticquarterapp/core/model/labs/patient_lab_orders.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/labs_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_send_email_dialog.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/network_base_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

import '../../text.dart';
import 'LabResultWidget.dart';

class LaboratoryResultWidget extends StatefulWidget {
  final GestureTapCallback onTap;
  final String billNo;
  final String details;
  final String orderNo;
  final PatientLabOrders patientLabOrder;

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

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

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

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return BaseView<LabsViewModel>(
      onModelReady: (model) =>
          model.getPatientLabResult(patientLabOrder: widget.patientLabOrder),
      builder: (_, model, w) => NetworkBaseView(
        baseViewModel: model,
        child: Container(
          margin: EdgeInsets.all(15),
          child: Column(
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[

                  Container(
                    margin: EdgeInsets.all(8),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Expanded(
                          child: Container(
                            decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                color: Colors.white,
                                borderRadius: BorderRadius.all(
                                  Radius.circular(5.0),
                                )),
                            padding: EdgeInsets.all(10.0),
                            margin: EdgeInsets.only(left: 5, right: 5),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Texts(TranslationBase.of(context).invoiceNo),
                                Texts(widget.billNo),
                              ],
                            ),
                          ),
                        ),
                        InkWell(
                          onTap: (){
                            showConfirmMessage(context,widget.onTap,model.user.emailAddress);
                          },
                          child: Container(
                            margin: EdgeInsets.only(left: 5, right: 5),
                            decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                color: Theme.of(context).primaryColor,
                                borderRadius: BorderRadius.all(
                                  Radius.circular(5.0),
                                )),
                            child: Container(
                              padding: EdgeInsets.all(10.0),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  Icon(
                                    Icons.email,
                                    color: Colors.white,
                                  ),
                                  Texts(
                                    TranslationBase.of(context).sendEmail,
                                    color: Colors.white,
                                  )
                                ],
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  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: Texts(TranslationBase.of(context).generalResult)),
                                  Container(
                                    width: 25,
                                    height: 25,
                                    decoration: BoxDecoration(
                                        shape: BoxShape.circle,
                                        color: Theme.of(context).primaryColor),
                                    child: Icon(
                                      _isShowMoreGeneral
                                          ? Icons.keyboard_arrow_up
                                          : Icons.keyboard_arrow_down,
                                      color: Colors.white,
                                      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>[
                                    ...List.generate(
                                      model.labResultLists.length,
                                          (index) => LabResultWidget(
                                        patientLabOrder: widget.patientLabOrder,
                                        filterName: model
                                            .labResultLists[index].filterName,
                                        patientLabResultList: model
                                            .labResultLists[index]
                                            .patientLabResultList,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                        ],
                      ),
                    ),
                  SizedBox(
                    height: 10,
                  ),
                  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: Texts(TranslationBase.of(context).specialResult)),
                          Container(
                            width: 25,
                            height: 25,
                            decoration: BoxDecoration(
                                shape: BoxShape.circle, color: Theme.of(context).primaryColor),
                            child: Icon(
                              _isShowMore
                                  ? Icons.keyboard_arrow_up
                                  : Icons.keyboard_arrow_down,
                              color: Colors.white,
                              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: Html(
                            data: widget.details ?? TranslationBase.of(context).noDataAvailable,
                          )),
                    ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
  void showConfirmMessage(BuildContext context, GestureTapCallback onTap,String email) {
    showDialog(
      context: context,
      child: ConfirmSendEmailDialog(
        email: email,
        onTapSendEmail: () {
         onTap();
        },
      ),
    );
  }
}
