import 'package:doctor_app_flutter/models/patient/vital_sign/vital_sign_req_model.dart';
import 'package:doctor_app_flutter/routes.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 '../../../../models/patient/patiant_info_model.dart';
import '../../../../providers/patients_provider.dart';
import '../../../../util/dr_app_shared_pref.dart';
import '../../../../widgets/shared/app_scaffold_widget.dart';
import '../../../../widgets/shared/app_texts_widget.dart';
import '../../../../widgets/shared/card_with_bg_widget.dart';
import '../../../../widgets/shared/dr_app_circular_progress_Indeicator.dart';
import '../../../../widgets/shared/profile_image_widget.dart';

DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

/* 
 *@author: Elham Rababah 
 *@Date:26/4/2020  
 *@param: 
 *@return:VitalSignScreen
 *@desc:   VitalSignScreen class
 */

class VitalSignScreen extends StatefulWidget {
  @override
  _VitalSignScreenState createState() => _VitalSignScreenState();
}

class _VitalSignScreenState extends State<VitalSignScreen> {
  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;
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      appBarTitle: "VITAL SIGN",
      body: patientsProv.isLoading
          ? DrAppCircularProgressIndeicator()
          : patientsProv.isError
              ? DrAppEmbeddedError(error: patientsProv.error)
              : patientsProv.patientVitalSignList.length == 0
                  ? DrAppEmbeddedError(error: 'You don\'t have any Vital Sign')
                  : Container(
                      margin: EdgeInsets.fromLTRB(
                          SizeConfig.realScreenWidth * 0.05,
                          0,
                          SizeConfig.realScreenWidth * 0.05,
                          0),
                      child: ListView.builder(
                          itemCount: patientsProv.patientVitalSignList.length,
                          itemBuilder: (BuildContext ctxt, int index) {
                            return InkWell(
                              child: CardWithBgWidget(
                                widget: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Row(
                                      children: <Widget>[
                                        ProfileImageWidget(
                                            url: patientsProv
                                                .patientVitalSignList[index]
                                                .doctorImageURL),
                                        Expanded(
                                          child: Padding(
                                            padding: const EdgeInsets.fromLTRB(
                                                8, 0, 0, 0),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: <Widget>[
                                                AppText(
                                                  '${patientsProv.patientVitalSignList[index].doctorName}',
                                                  fontSize: 2.5 *
                                                      SizeConfig.textMultiplier,
                                                  fontWeight: FontWeight.bold,
                                                ),
                                                SizedBox(
                                                  height: 8,
                                                ),
                                                AppText(
                                                  ' ${patientsProv.patientVitalSignList[index].clinicName}',
                                                  fontSize: 2 *
                                                      SizeConfig.textMultiplier,
                                                  color: Theme.of(context)
                                                      .primaryColor,
                                                ),
                                                SizedBox(
                                                  height: 8,
                                                ),
                                              ],
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                              onTap: () {
                                Navigator.of(context)
                                    .pushNamed(VITAL_SIGN_DETAILS, arguments: {
                                  'vitalSing':
                                      patientsProv.patientVitalSignList[index]
                                });
                              },
                            );
                          }),
                    ),
    );
  }
}
