import 'package:doctor_app_flutter/core/viewModel/labs_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/lab_result/special_result/special_lab_result_details_page.dart';
import 'package:doctor_app_flutter/utils/date-utils.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/profile/app_bar/patient-profile-app-bar.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_service_title.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:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../../widgets/shared/errors/error_message.dart';

class AllLabSpecialResult extends StatefulWidget {
  const AllLabSpecialResult({Key? key}) : super(key: key);

  @override
  _AllLabSpecialResultState createState() => _AllLabSpecialResultState();
}

class _AllLabSpecialResultState extends State<AllLabSpecialResult> {
  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<LabsViewModel>(
      onModelReady: (model) =>
          model.getAllSpecialLabResult(patientId: patient!.patientMRN!),
      builder: (context, LabsViewModel model, widget) => AppScaffold(
        baseViewModel: model,
        backgroundColor: Colors.grey[100]!,
        isShowAppBar: true,
        appBar: PatientProfileAppBar(
          patient!,
          isInpatient: isInpatient!,
        ),
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: FractionallySizedBox(
            widthFactor: 1.0,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  height: 12,
                ),
                ServiceTitle(
                  title: TranslationBase.of(context).special +
                      " " +
                      TranslationBase.of(context).lab,
                  subTitle: TranslationBase.of(context).result,
                ),
                if(model.allSpecialLabList.length == 0)
                  ErrorMessage(error: TranslationBase.of(context).noItem),
                if(model.allSpecialLabList.length > 0)
                ListView.builder(
                    physics: NeverScrollableScrollPhysics(),
                    itemCount: model.allSpecialLabList.length,
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemBuilder: (BuildContext ctxt, int index) {
                      return 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(
                                color: model.allSpecialLabList[index]
                                        .isLiveCareAppointment!
                                    ? Colors.red[900]!
                                    : !model.allSpecialLabList[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.allSpecialLabList[index]
                                              .isLiveCareAppointment!
                                          ? TranslationBase.of(context)
                                              .liveCare
                                              .toUpperCase()
                                          : !model.allSpecialLabList[index]
                                                  .isInOutPatient!
                                              ? TranslationBase.of(context)
                                                  .inPatientLabel
                                                  .toUpperCase()
                                              : TranslationBase.of(context)
                                                  .outpatient
                                                  .toUpperCase(),
                                      style: TextStyle(color: Colors.white),
                                    ),
                                  )),
                            ),
                            Expanded(
                              child: DoctorCard(
                                isNoMargin: true,
                                onTap: () => Navigator.push(
                                  context,
                                  FadePage(
                                    page: SpecialLabResultDetailsPage(
                                      resultData: model.allSpecialLabList[index]
                                          .resultDataHTML!,
                                      patient: patient!,
                                    ),
                                  ),
                                ),
                                doctorName:
                                Utils.convertToTitleCase(model.allSpecialLabList[index].doctorName!),
                                invoiceNO:
                                    ' ${model.allSpecialLabList[index].invoiceNo}',
                                profileUrl: model
                                    .allSpecialLabList[index].doctorImageURL!,
                                branch:
                                    model.allSpecialLabList[index].projectName!,
                                clinic:
                                Utils.convertToTitleCase(model.allSpecialLabList[index].clinicDescription!),
                                appointmentDate:
                                    AppDateUtils.getDateTimeFromServerFormat(
                                  model.allSpecialLabList[index].createdOn,
                                ),
                                orderNo: model.allSpecialLabList[index].orderNo!,
                                isShowTime: false,
                              ),
                            ),
                          ],
                        ),
                      );
                    }),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
