import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/lab_result/laboratory_result/laboratory_result_page.dart';
import 'package:doctor_app_flutter/screens/procedures/base_add_procedure_tab_page.dart';
import 'package:doctor_app_flutter/screens/procedures/procedure_type.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_service_title.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/add-order/addNewOrder.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/app_bar/patient-profile-app-bar.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/doctor_card.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../../widgets/shared/app_texts_widget.dart';
import '../../../../widgets/shared/errors/error_message.dart';

class LabsHomePage extends StatefulWidget {
  @override
  _LabsHomePageState createState() => _LabsHomePageState();
}

class _LabsHomePageState extends State<LabsHomePage> {
  String? patientType;

  String? arrivalType;
  PatiantInformtion? patient;
  bool? isInpatient;
  bool? isFromLiveCare;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    patient = routeArgs['patient'];
    patientType = routeArgs['patientType'];
    arrivalType = routeArgs['arrivalType'];
    isInpatient = routeArgs['isInpatient'];
    isFromLiveCare = routeArgs['isFromLiveCare'];

    print(arrivalType);
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<ProcedureViewModel>(
      onModelReady: (model) {
        model.getLabs(patient!, isInpatient: false);
        model.isPrincipalCovered(patient: patient!);
      },
      builder: (context, ProcedureViewModel model, widget) =>

        //   AppScaffold(
        // baseViewModel: model,
        // backgroundColor: Colors.grey[100]!,
        // isShowAppBar: true,
        // appBar: PatientProfileAppBar(
        //   patient!,
        //   isInpatient: isInpatient!,
        // ),
        // body:
     Material(
         child: Column(
           children: [
             PatientProfileAppBar(
               patient!,
               isInpatient: isInpatient!,
             ),
             Expanded(
               child: SingleChildScrollView(
                  physics: BouncingScrollPhysics(),
                  child: FractionallySizedBox(
                    widthFactor: 1.0,
                    child:   NetworkBaseView(
                      baseViewModel: model,
                      child:Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        SizedBox(
                          height: 12,
                        ),
                        if ((model.patientLabOrdersList.isNotEmpty && patient!.patientStatusType != 43) || (patient!.patientStatusType != null && patient!.patientStatusType == 43))
                          ServiceTitle(
                            title: TranslationBase.of(context).lab,
                            subTitle: TranslationBase.of(context).result,
                          ),
                        SizedBox(
                          height: 20,
                        ),
                        !model.isPrincipalCovered_
                            ? Center(
                                child: AppText(
                                TranslationBase.of(context).principalCoveredOrNot,
                                color: Colors.red,
                                textAlign: TextAlign.center,
                              ))
                            : SizedBox(),
                        if ((patient!.patientStatusType != null && patient!.patientStatusType == 43) || (isFromLiveCare! && patient!.appointmentNo != null))
                          AddNewOrder(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => BaseAddProcedureTabPage(
                                    patient: patient!,
                                    previousProcedureViewModel: model,
                                    procedureType: ProcedureType.LAB_RESULT,
                                  ),
                                  settings: RouteSettings(name: 'AddProcedureTabPage'),
                                ),
                              );
                            },
                            label: TranslationBase.of(context).applyForNewLabOrder,
                          ),
                        ...List.generate(
                          model.patientLabOrdersList.length,
                          (index) => Container(
                            margin: EdgeInsets.all(10),
                            decoration: BoxDecoration(
                                border: Border.all(
                                  width: 0.5,
                                  color: Colors.white,
                                ),
                                borderRadius: BorderRadius.all(
                                  Radius.circular(8.0),
                                ),
                                color: Colors.white),
                            child: Stack(
                              children: [
                                Positioned(
                                  bottom: 1,
                                  top: 1,
                                  left: 1,
                                  child: Container(
                                    width: 20,
                                    decoration: BoxDecoration(
                                      color: model.patientLabOrdersList![index].isLiveCareAppointment!
                                          ? Colors.red[900]
                                          : !model.patientLabOrdersList[index].isInOutPatient!
                                              ? Colors.black
                                              : Color(0xffa9a089),
                                      borderRadius: BorderRadius.only(
                                          topLeft: projectViewModel.isArabic ? Radius.circular(0) : Radius.circular(8),
                                          bottomLeft: projectViewModel.isArabic ? Radius.circular(0) : Radius.circular(8),
                                          topRight: projectViewModel.isArabic ? Radius.circular(8) : Radius.circular(0),
                                          bottomRight: projectViewModel.isArabic ? Radius.circular(8) : Radius.circular(0)),
                                    ),
                                    child: RotatedBox(
                                        quarterTurns: 3,
                                        child: Center(
                                          child: Text(
                                            model.patientLabOrdersList[index].isLiveCareAppointment!
                                                ? TranslationBase.of(context).liveCare.toUpperCase()
                                                : !model.patientLabOrdersList[index].isInOutPatient!
                                                    ? TranslationBase.of(context).inPatientLabel.toUpperCase()
                                                    : TranslationBase.of(context).outpatient.toUpperCase(),
                                            style: TextStyle(color: Colors.white),
                                          ),
                                        )),
                                  ),
                                ),
                                Container(
                                  color: Colors.white,
                                  padding: EdgeInsets.all(0),
                                  margin: EdgeInsets.only(left: 20),
                                  child: DoctorCard(
                                    isNoMargin: true,
                                    onTap: () => Navigator.push(
                                      context,
                                      FadePage(
                                        page: LaboratoryResultPage(
                                          patientLabOrders: model.patientLabOrdersList[index],
                                          patient: patient!,
                                          isInpatient: isInpatient!,
                                          arrivalType: arrivalType!,
                                          patientType: patientType!,
                                        ),
                                      ),
                                    ),
                                    doctorName: Utils.convertToTitleCase(model.patientLabOrdersList[index].doctorName),
                                    invoiceNO: ' ${model.patientLabOrdersList[index].invoiceNo}',
                                    profileUrl: model.patientLabOrdersList[index].doctorImageURL,
                                    branch: model.patientLabOrdersList[index].projectName,
                                    clinic: Utils.convertToTitleCase(model.patientLabOrdersList[index].clinicDescription),
                                    appointmentDate: model.patientLabOrdersList[index].createdOn,
                                    orderNo: model.patientLabOrdersList[index].orderNo,
                                    isShowTime: false,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        if (model.patientLabOrdersList.isEmpty && patient!.patientStatusType != 43)
                          Center(
                              child: ErrorMessage(
                            error: TranslationBase.of(context).noDataAvailable,
                          ))
                      ],
                    ),)
                  ),
                ),
             ),
           ],
         ),
        ),
      );

  }
}
