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/radiology/radiology_details_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/transitions/fade_page.dart';
import 'package:doctor_app_flutter/widgets/transitions/slide_up_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 RadiologyHomePage extends StatefulWidget {
  @override
  _RadiologyHomePageState createState() => _RadiologyHomePageState();
}

class _RadiologyHomePageState extends State<RadiologyHomePage> {
  String? patientType;
  late PatiantInformtion patient;
  String? arrivalType;
  bool isInpatient = false;
  bool isFromLiveCare = false;

  @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.getPatientRadOrders(
            patient, patientType: patientType, isInPatient: false);
        model.isPrincipalCovered(patient: patient);
      },
      builder: (_, model, widget) => AppScaffold(
        isShowAppBar: true,
        backgroundColor: Colors.grey[100]!,
        // appBarTitle: TranslationBase.of(context).radiology,
        appBar: PatientProfileAppBar(
          patient,
          isInpatient: isInpatient,
        ),
        baseViewModel: model,
        body: FractionallySizedBox(
          widthFactor: 1.0,
          child: ListView(
            physics: BouncingScrollPhysics(),
            children: <Widget>[
              SizedBox(
                height: 12,
              ),
              if (model.radiologyList.isNotEmpty && patient.patientStatusType != 43)
                ServiceTitle(
                  title: TranslationBase.of(context).radiology,
                  subTitle: TranslationBase.of(context).result,
                ),


              if (patient.patientStatusType != null && patient.patientStatusType == 43)
                ServiceTitle(
                  title: TranslationBase.of(context).radiology,
                  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,
                      SlideUpPageRoute(
                          widget: BaseAddProcedureTabPage(
                            patient: patient,
                            previousProcedureViewModel: model,
                            procedureType: ProcedureType.RADIOLOGY,
                          ),
                          settingRoute: 'AddProcedureTabPage'),
                    );
                  },
                  label: TranslationBase.of(context).applyForRadiologyOrder,
                ),

              ///TODO Elham * fix this to be list view builder
              ...List.generate(
                  model.radiologyList.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: Row(
                          children: [
                            Container(
                              width: 20,
                              height: 160,
                              decoration: BoxDecoration(
                                //Colors.red[900]  Color(0xff404545)
                                color: model.radiologyList[index].isLiveCareAppodynamicment ?? false
                                    ? Colors.red[900]
                                    : model.radiologyList[index].isInOutPatient !=null && model.radiologyList[index].isInOutPatient==false
                                        ? 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.radiologyList[index].isLiveCareAppodynamicment !=null &&  model.radiologyList[index].isLiveCareAppodynamicment==true
                                          ? TranslationBase.of(context).liveCare.toUpperCase()
                                          : model.radiologyList[index].isInOutPatient!=null && model.radiologyList[index].isInOutPatient==false
                                              ? TranslationBase.of(context).inPatientLabel.toUpperCase()
                                              : TranslationBase.of(context).outpatient.toUpperCase(),
                                      style: TextStyle(color: Colors.white),
                                    ),
                                  )),
                            ),
                            Expanded(
                              child: DoctorCard(
                                isNoMargin: true,
                                doctorName: Utils.convertToTitleCase(model.radiologyList[index].doctorName),
                                profileUrl: model.radiologyList[index].doctorImageURL,
                                invoiceNO: Utils.isVidaPlusProject(projectViewModel, model.radiologyList[index].projectID)
                                    ? '${model.radiologyList[index].invoiceNo_VP}'
                                    : '${model.radiologyList[index].invoiceNo}',
                                // model.isRadiologyVIDAPlus ? '${model.radiologyList[index].invoiceNo}' : '${model.radiologyList[index].invoiceNo}',
                                branch: '${model.radiologyList[index].projectName}',
                                clinic: Utils.convertToTitleCase(model.radiologyList[index].clinicDescription),
                                appointmentDate: model.radiologyList[index].orderDate ?? model.radiologyList[index].reportDate,
                                onTap: () {
                                  Navigator.push(
                                    context,
                                    FadePage(
                                      page: RadiologyDetailsPage(
                                          finalRadiology: model.radiologyList[index],
                                          patient: patient,
                                          isInpatient: isInpatient,
                                          isVidaPlus: Utils.isVidaPlusProject(projectViewModel, model.radiologyList[index].projectID)
                                          // false
                                          // model.radiologyList[index].isRecordFromVidaPlus
                                          ),
                                    ),
                                  );
                                },
                              ),
                            ),
                          ],
                        ),
                      )),
              if (model.radiologyList.isEmpty && patient.patientStatusType != 43)
                Center(
                    child: ErrorMessage(
                  error: TranslationBase.of(context).noDataAvailable,
                ))
            ],
          ),
        ),
      ),
    );
  }
}
