import 'package:doctor_app_flutter/models/patient/vital_sign/vital_sign_req_model.dart';
import 'package:doctor_app_flutter/widgets/shared/dr_app_circular_progress_Indeicator.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/dr_app_embedded_error.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../config/shared_pref_kay.dart';
import '../../../../config/size_config.dart';
import '../../../../lookups/patient_lookup.dart';
import '../../../../models/patient/patiant_info_model.dart';
import '../../../../models/patient/vital_sign/vital_sign_res_model.dart';
import '../../../../providers/patients_provider.dart';
import '../../../../routes.dart';
import '../../../../screens/patients/profile/vital_sign/vital_sign_item.dart';
import '../../../../util/dr_app_shared_pref.dart';
import '../../../../widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';

DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

class VitalSignDetailsScreen extends StatefulWidget {
  // VitalSignDetailsScreen({Key key, this.vitalSing}) : super(key: key);
  @override
  _VitalSignDetailsScreenState createState() => _VitalSignDetailsScreenState();
}

class _VitalSignDetailsScreenState extends State<VitalSignDetailsScreen> {
  VitalSignResModel vitalSing;
  String url = "assets/images/";

  PatientsProvider patientsProv;
  var _isInit = true;

  /*
 *@author: Elham Rababah
 *@Date:28/4/2020
 *@param: context
 *@return:
 *@desc:   getVitalSignList Function
 */
  getVitalSignList(context) async {
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    String token = await sharedPref.getString(TOKEN);
    String type = await sharedPref.getString(SLECTED_PATIENT_TYPE);
    int inOutpatientType = 1;
    if (type == '0') {
      inOutpatientType = 2;
    }
    print(type);
    VitalSignReqModel vitalSignReqModel = VitalSignReqModel(
        patientID: patient.patientId,
        projectID: patient.projectId,
        tokenID: token,
        patientTypeID: patient.patientType,
        inOutpatientType: inOutpatientType,
        languageID: 2,
        transNo:
            patient.admissionNo != null ? int.parse(patient.admissionNo) : 0);
    patientsProv.getPatientVitalSign(vitalSignReqModel.toJson());
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (_isInit) {
      patientsProv = Provider.of<PatientsProvider>(context);
      getVitalSignList(context);
    }
    _isInit = false;
  }

  final double contWidth = SizeConfig.realScreenWidth * 0.70;

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    vitalSing = routeArgs['vitalSing'];
    return AppScaffold(
        appBarTitle: TranslationBase.of(context).vitalSign,
        body: patientsProv.isLoading
            ? DrAppCircularProgressIndeicator()
            : patientsProv.isError
                ? DrAppEmbeddedError(error: patientsProv.error)
                : patientsProv.patientVitalSignOrderdSubList.length == 0
                    ? DrAppEmbeddedError(
                        error: 'You don\'t have any vital Sings')
                    : Container(
                        child: Column(
                          children: <Widget>[
                            Row(
                              children: <Widget>[
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title': TranslationBase.of(context)
                                              .bodyMeasurements,
                                          'key':
                                              vitalSignDetails.bodyMeasurements
                                        });
                                  },
                                  child: Container(
                                    child: VitalSignItem(
                                      des: TranslationBase.of(context)
                                          .bodyMeasurements,
                                      url: url + 'heartbeat.png',
                                      lastVal: patientsProv
                                          .patientVitalSignOrderdSubList[0]
                                          .heightCm
                                          .toString(),
                                      unit: 'Cm',
                                    ),
                                  ),
                                ),
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title': TranslationBase.of(context)
                                              .temperature,
                                          'key': vitalSignDetails.temperature,
                                        });
                                  },
                                  child: Container(
                                    child: VitalSignItem(
                                      des: TranslationBase.of(context)
                                          .temperature,
                                      url: url + 'heartbeat.png',
                                      lastVal: patientsProv
                                          .patientVitalSignOrderdSubList[0]
                                          .temperatureCelcius
                                          .toString(),
                                      unit: 'C',
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: <Widget>[
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title':
                                              TranslationBase.of(context).pulse,
                                          'key': vitalSignDetails.pulse
                                        });
                                  },
                                  child: VitalSignItem(
                                    des: TranslationBase.of(context).pulse,
                                    url: url + 'heartbeat.png',
                                    lastVal: patientsProv
                                        .patientVitalSignOrderdSubList[0]
                                        .pulseBeatPerMinute
                                        .toString(),
                                    unit: 'M',
                                  ),
                                ),
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title': TranslationBase.of(context)
                                              .respiration,
                                          'key': vitalSignDetails.pespiration
                                        });
                                  },
                                  child: VitalSignItem(
                                    des:
                                        TranslationBase.of(context).respiration,
                                    url: url + 'heartbeat.png',
                                    lastVal: patientsProv
                                        .patientVitalSignOrderdSubList[0]
                                        .respirationBeatPerMinute
                                        .toString(),
                                    unit: 'M',
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: <Widget>[
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title': TranslationBase.of(context)
                                              .bloodPressure,
                                          'key': vitalSignDetails.bloodPressure
                                        });
                                  },
                                  child: VitalSignItem(
                                    des: TranslationBase.of(context)
                                        .bloodPressure,
                                    url: url + 'heartbeat.png',
                                    lastVal: patientsProv
                                        .patientVitalSignOrderdSubList[0]
                                        .bloodPressure
                                        .toString(),
                                    unit: '',
                                  ),
                                ),
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title': TranslationBase.of(context)
                                              .oxygenation,
                                          'key': vitalSignDetails.oxygenation
                                        });
                                  },
                                  child: VitalSignItem(
                                    des:
                                        TranslationBase.of(context).oxygenation,
                                    url: url + 'heartbeat.png',
                                    lastVal: patientsProv
                                        .patientVitalSignOrderdSubList[0].fIO2
                                        .toString(),
                                    unit: '',
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed(
                                        BODY_MEASUREMENTS,
                                        arguments: {
                                          'title': TranslationBase.of(context)
                                              .painScale,
                                          'key': vitalSignDetails.painScale
                                        });
                                  },
                                  child: VitalSignItem(
                                    des: TranslationBase.of(context).painScale,
                                    url: url + 'heartbeat.png',
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ));
  }
}
