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/widgets/others/network_base_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

import '../text.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 = false;
  bool _isShowMoreGeneral = false;
  ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return Container(
      margin: EdgeInsets.all(15),
      child: Column(
        children: <Widget>[
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.all(15),
                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('Invoice No'),
                            Texts(widget.billNo),
                          ],
                        ),
                      ),
                    ),
                    InkWell(
                      onTap: widget.onTap,
                      child: Container(
                        margin: EdgeInsets.only(left: 5, right: 5),
                        decoration: BoxDecoration(
                            shape: BoxShape.rectangle,
                            color: Colors.red[800],
                            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(
                                'Send a copy',
                                color: Colors.white,
                              )
                            ],
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
              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('Special Result')),
                      Container(
                        width: 25,
                        height: 25,
                        decoration: BoxDecoration(
                            shape: BoxShape.circle, color: Colors.red[900]),
                        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: Text(widget.details ?? 'No Data')),
                ),
              SizedBox(height: 12,),
              BaseView<LabsViewModel>(
                onModelReady: (model) => model.getPatientLabResult(
                    patientLabOrder: widget.patientLabOrder),
                builder: (_, model, w) => NetworkBaseView(
                  baseViewModel: model,
                  child: 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('General Result')),
                                Container(
                                  width: 25,
                                  height: 25,
                                  decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      color: Colors.red[900]),
                                  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>[
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Texts('U/A'),
                                      InkWell(
                                        onTap: () {
                                          model.getPatientLabResult(
                                              patientLabOrder:
                                                  widget.patientLabOrder);
                                        },
                                        child: Texts(
                                          'Flow Chart',
                                          decoration: TextDecoration.underline,
                                          color: Colors.blue,
                                        ),
                                      ),
                                    ],
                                  ),
                                  Table(
                                    border: TableBorder.symmetric(
                                      inside: BorderSide(
                                          width: 2.0, color: Colors.grey[300]),
                                    ),
                                    children: fullData(model.labResultList),
                                  ),
                                ],
                              ),
                            ),
                          ),
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ],
      ),
    );
  }

  List<TableRow> fullData(List<LabResult> labResultList) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Container(
            child: Container(
              decoration: BoxDecoration(
                color: Hexcolor('#515B5D'),
                borderRadius: BorderRadius.only(
                  topLeft: projectViewModel.isArabic ? Radius.circular(0.0): Radius.circular(10.0),
                  topRight:  projectViewModel.isArabic ?  Radius.circular(10.0) : Radius.circular(0.0),
                ),
              ),
              child: Center(
                child: Texts(
                  'Description',
                  color: Colors.white,
                ),
              ),
              height: 60,
            ),
          ),
          Container(
            child: Container(
                decoration: BoxDecoration(
                  color: Hexcolor('#515B5D'),

                ),
                child: Center(
                  child: Texts('Value', color: Colors.white),
                ),
                height: 60),
          ),
          Container(
            child: Container(
                decoration: BoxDecoration(
                  color: Hexcolor('#515B5D'),
                  borderRadius: BorderRadius.only(
                    topLeft: projectViewModel.isArabic ? Radius.circular(10.0):Radius.circular(0.0),
                    topRight:  projectViewModel.isArabic ?  Radius.circular(0.0) : Radius.circular(10.0),
                  ),
                ),
                child: Center(
                  child: Texts('Range', color: Colors.white),
                ),
                height: 60),
          ),
        ],
      ),
    );
    labResultList.forEach((lab) {
      tableRow.add(
        TableRow(
          children: [
            Container(
              child: Container(
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Center(
                  child: Texts(
                    lab.description,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
            Container(
              child: Container(
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Center(
                  child: Texts(
                    lab.resultValue,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
            Container(
              child: Container(
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Center(
                  child: Texts(
                    lab.referanceRange,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    });
    return tableRow;
  }
}
