import 'package:doctor_app_flutter/core/viewModel/prescription_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/prescription/add_prescription_form.dart';
import 'package:doctor_app_flutter/screens/prescription/update_prescription_form.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/patients/profile/patient-page-header-widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class NewPrescriptionScreen extends StatefulWidget {
  @override
  _NewPrescriptionScreenState createState() => _NewPrescriptionScreenState();
}

class _NewPrescriptionScreenState extends State<NewPrescriptionScreen> {
  PersistentBottomSheetController _controller;
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  TextEditingController strengthController = TextEditingController();
  int testNum = 0;
  int strengthChar;
  PatiantInformtion patient;

  @override
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    patient = routeArgs['patient'];
    return BaseView<PrescriptionViewModel>(
      onModelReady: (model) => model.getPrescription(mrn: patient.patientId),
      builder:
          (BuildContext context, PrescriptionViewModel model, Widget child) =>
              AppScaffold(
                  isShowAppBar: true,
                  appBarTitle: TranslationBase.of(context).prescription,
                  body: NetworkBaseView(
                    baseViewModel: model,
                    child: SingleChildScrollView(
                      child: Container(
                        color: Colors.white,
                        child: Column(
                          children: [
                            PatientPageHeaderWidget(patient),
                            Divider(
                              height: 1.0,
                              thickness: 1.0,
                              color: Colors.grey,
                            ),
                            (model.prescriptionList.length != 0)
                                ? SizedBox(
                                    height:
                                        model.prescriptionList[0].rowcount == 0
                                            ? 200.0
                                            : 10.0)
                                : SizedBox(height: 200.0),
                            //model.prescriptionList == null
                            (model.prescriptionList.length != 0)
                                ? model.prescriptionList[0].rowcount == 0
                                    ? Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.stretch,
                                        children: [
                                          InkWell(
                                            onTap: () {
                                              addPrescriptionForm(
                                                  context,
                                                  model,
                                                  patient,
                                                  model.prescriptionList);
                                              //model.postPrescription();
                                            },
                                            child: CircleAvatar(
                                              radius: 65,
                                              backgroundColor:
                                                  Color(0XFFB8382C),
                                              child: CircleAvatar(
                                                radius: 60,
                                                backgroundColor: Colors.white,
                                                child: Icon(
                                                  Icons.add,
                                                  color: Colors.black,
                                                  size: 45.0,
                                                ),
                                              ),
                                            ),
                                          ),
                                          SizedBox(
                                            height: 15.0,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              AppText(
                                                TranslationBase.of(context)
                                                    .noPrescriptionListed,
                                                color: Colors.black,
                                                fontWeight: FontWeight.w900,
                                              ),
                                            ],
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              AppText(
                                                TranslationBase.of(context)
                                                    .addNow,
                                                color: Color(0XFFB8382C),
                                                fontWeight: FontWeight.w900,
                                              ),
                                            ],
                                          ),
                                        ],
                                      )
                                    : Padding(
                                        padding: EdgeInsets.all(14.0),
                                        child: NetworkBaseView(
                                          baseViewModel: model,
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: [
                                              InkWell(
                                                child: Container(
                                                  height: 50.0,
                                                  width: 450.0,
                                                  decoration: BoxDecoration(
                                                    color: Colors.white,
                                                    border: Border.all(
                                                        color: Colors.grey),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            10.0),
                                                  ),
                                                  child: Padding(
                                                    padding:
                                                        EdgeInsets.all(8.0),
                                                    child: Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        AppText(
                                                          ' Add more medication',
                                                          fontWeight:
                                                              FontWeight.w100,
                                                          fontSize: 12.5,
                                                        ),
                                                        Icon(
                                                          Icons.add,
                                                          color:
                                                              Color(0XFFB8382C),
                                                        )
                                                      ],
                                                    ),
                                                  ),
                                                ),
                                                onTap: () {
                                                  addPrescriptionForm(
                                                      context,
                                                      model,
                                                      patient,
                                                      model.prescriptionList);
                                                  //model.postPrescription();
                                                },
                                              ),
                                              SizedBox(
                                                height: 10.0,
                                              ),
                                              ...List.generate(
                                                model.prescriptionList[0]
                                                    .rowcount,
                                                (index) => Container(
                                                  color: Colors.white,
                                                  child: Column(
                                                    children: [
                                                      SizedBox(
                                                        height: MediaQuery.of(
                                                                    context)
                                                                .size
                                                                .height *
                                                            0.022,
                                                      ),
                                                      Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .spaceBetween,
                                                        // crossAxisAlignment:
                                                        //     CrossAxisAlignment.start,
                                                        children: [
                                                          Container(
                                                            color: Colors.white,
                                                            height: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .height *
                                                                0.21,
                                                            width: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .width *
                                                                0.1,
                                                            child: Column(
                                                              children: [
                                                                AppText(
                                                                  (DateTime.parse(model.prescriptionList[0].entityList[index].createdOn) !=
                                                                              null
                                                                          ? (DateTime.parse(model.prescriptionList[0].entityList[index].createdOn).year)
                                                                              .toString()
                                                                          : DateTime.now()
                                                                              .year)
                                                                      .toString(),
                                                                  color: Colors
                                                                      .green,
                                                                  fontSize:
                                                                      13.5,
                                                                ),
                                                                AppText(
                                                                  Helpers.getMonth(model.prescriptionList[0].entityList[index].createdOn !=
                                                                              null
                                                                          ? (DateTime.parse(model.prescriptionList[0].entityList[index].createdOn)
                                                                              .month)
                                                                          : DateTime.now()
                                                                              .month)
                                                                      .toUpperCase(),
                                                                  color: Colors
                                                                      .green,
                                                                ),
                                                                AppText(
                                                                  DateTime.parse(model
                                                                          .prescriptionList[
                                                                              0]
                                                                          .entityList[
                                                                              index]
                                                                          .createdOn)
                                                                      .day
                                                                      .toString(),
                                                                  color: Colors
                                                                      .green,
                                                                ),
                                                                AppText(
                                                                  Helpers.getTimeFormated(DateTime.parse(model
                                                                          .prescriptionList[
                                                                              0]
                                                                          .entityList[
                                                                              index]
                                                                          .createdOn))
                                                                      .toString(),
                                                                  color: Colors
                                                                      .green,
                                                                ),
                                                              ],
                                                            ),
                                                          ),
                                                          Container(
                                                            color: Colors.white,
                                                            // height: MediaQuery.of(
                                                            //             context)
                                                            //         .size
                                                            //         .height *
                                                            //     0.3499,
                                                            width: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .width *
                                                                0.77,
                                                            child: Column(
                                                              children: [
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'Start Date:',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          14.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        Helpers.getDateFormatted(DateTime.parse(model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .startDate)),
                                                                        fontSize:
                                                                            13.5,
                                                                      ),
                                                                    ),
                                                                    SizedBox(
                                                                      width:
                                                                          6.0,
                                                                    ),
                                                                    AppText(
                                                                      'Order Type:',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          14.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .orderTypeDescription,
                                                                        fontSize:
                                                                            13.0,
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 5.5,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Container(
                                                                      color: Colors
                                                                          .white,
                                                                      child:
                                                                          Expanded(
                                                                        child:
                                                                            AppText(
                                                                          model
                                                                              .prescriptionList[0]
                                                                              .entityList[index]
                                                                              .medicationName,
                                                                          fontWeight:
                                                                              FontWeight.w700,
                                                                          fontSize:
                                                                              15.0,
                                                                        ),
                                                                      ),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 5.5,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .doseDetail,
                                                                        fontSize:
                                                                            15.0,
                                                                      ),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 10.0,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'Indication: ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].indication),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'UOM: ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].uom),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'BOX Quantity: ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].quantity.toString() == null ? "" : model.prescriptionList[0].entityList[index].quantity.toString()),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'pharmacy Intervention ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].pharmacyInervention == null ? "" : model.prescriptionList[0].entityList[index].pharmacyInervention.toString()),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                    height:
                                                                        5.0),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'pharmacist Remarks : ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          15.0,
                                                                    ),
                                                                    Expanded(
                                                                      child: AppText(
                                                                          // commening below code because there is an error coming in the model please fix it before pushing it
                                                                          model.prescriptionList[0].entityList[index].pharmacistRemarks == null ? "" : model.prescriptionList[0].entityList[index].pharmacistRemarks,
                                                                          fontSize: 15.0),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 20.0,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      TranslationBase.of(context)
                                                                              .doctorName +
                                                                          ": ",
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w600,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .doctorName,
                                                                        fontWeight:
                                                                            FontWeight.w700,
                                                                      ),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 8.0,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'Doctor Remarks : ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          13.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          Container(
                                                                        color: Colors
                                                                            .white,
                                                                        // height: MediaQuery.of(context).size.height *
                                                                        //     0.038,
                                                                        child:
                                                                            RichText(
                                                                          // maxLines:
                                                                          //     2,
                                                                          // overflow:
                                                                          //     TextOverflow.ellipsis,
                                                                          strutStyle:
                                                                              StrutStyle(fontSize: 10.0),
                                                                          text:
                                                                              TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].remarks != null
                                                                                ? model.prescriptionList[0].entityList[index].remarks
                                                                                : "",
                                                                          ),
                                                                        ),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 10.0,
                                                                ),

                                                                // SizedBox(
                                                                //   height: 40,
                                                                // ),
                                                              ],
                                                            ),
                                                          ),
                                                          Container(
                                                            color: Colors.white,
                                                            height: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .height *
                                                                0.16,
                                                            width: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .width *
                                                                0.06,
                                                            child: Column(
                                                              children: [
                                                                InkWell(
                                                                  child: Icon(
                                                                      Icons
                                                                          .edit),
                                                                  onTap: () {
                                                                    updatePrescriptionForm(
                                                                        box: model
                                                                            .prescriptionList[
                                                                                0]
                                                                            .entityList[
                                                                                index]
                                                                            .quantity,
                                                                        uom: model
                                                                            .prescriptionList[
                                                                                0]
                                                                            .entityList[
                                                                                index]
                                                                            .uom,
                                                                        drugNameGeneric: model
                                                                            .prescriptionList[
                                                                                0]
                                                                            .entityList[
                                                                                index]
                                                                            .medicationName,
                                                                        doseUnit: model.prescriptionList[0].entityList[index].doseDailyUnitID
                                                                            .toString(),
                                                                        doseStreangth: model.prescriptionList[0].entityList[index].doseDailyQuantity
                                                                            .toString(),
                                                                        duration:
                                                                            model.prescriptionList[0].entityList[index].doseDurationDays
                                                                                .toString(),
                                                                        startDate:
                                                                            model.prescriptionList[0].entityList[index].startDate
                                                                                .toString(),
                                                                        dose: model
                                                                            .prescriptionList[
                                                                                0]
                                                                            .entityList[
                                                                                index]
                                                                            .doseTimingID
                                                                            .toString(),
                                                                        frequency:
                                                                            model.prescriptionList[0].entityList[index].frequencyID
                                                                                .toString(),
                                                                        rouat: model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .routeID
                                                                            .toString(),
                                                                        patient: patient,
                                                                        drugId: model.prescriptionList[0].entityList[index].medicineCode,
                                                                        drugName: model.prescriptionList[0].entityList[index].medicationName,
                                                                        remarks: model.prescriptionList[0].entityList[index].remarks,
                                                                        model: model,
                                                                        enteredRemarks: model.prescriptionList[0].entityList[index].remarks,
                                                                        context: context);
                                                                    //model.postPrescription();
                                                                  },
                                                                ),
                                                              ],
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                      Divider(
                                                        height: 0,
                                                        thickness: 1.0,
                                                        color: Colors.grey,
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      )
                                : Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.stretch,
                                    children: [
                                      InkWell(
                                        onTap: () {
                                          addPrescriptionForm(context, model,
                                              patient, model.prescriptionList);
                                          //model.postPrescription();
                                        },
                                        child: CircleAvatar(
                                          radius: 65,
                                          backgroundColor: Color(0XFFB8382C),
                                          child: CircleAvatar(
                                            radius: 60,
                                            backgroundColor: Colors.white,
                                            child: Icon(
                                              Icons.add,
                                              color: Colors.black,
                                              size: 45.0,
                                            ),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        height: 15.0,
                                      ),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          AppText(
                                            TranslationBase.of(context)
                                                .noPrescriptionListed,
                                            color: Colors.black,
                                            fontWeight: FontWeight.w900,
                                          ),
                                        ],
                                      ),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          AppText(
                                            TranslationBase.of(context).addNow,
                                            color: Color(0XFFB8382C),
                                            fontWeight: FontWeight.w900,
                                          ),
                                        ],
                                      ),
                                    ],
                                  )
                          ],
                        ),
                      ),
                    ),
                  )),
    );
  }

  selectDate(BuildContext context, PrescriptionViewModel model) async {
    DateTime selectedDate;
    selectedDate = DateTime.now();
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: DateTime.now().add(Duration(hours: 2)),
      lastDate: DateTime(2040),
      initialEntryMode: DatePickerEntryMode.calendar,
    );
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }
}
