import 'package:doctor_app_flutter/core/service/AnalyticsService.dart';
import 'package:doctor_app_flutter/core/viewModel/medical_file_view_model.dart';
import 'package:doctor_app_flutter/locator.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/medical-file/medical_file_details.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/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/app_texts_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:flutter/material.dart';

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

class HealthSummaryPage extends StatefulWidget {
  @override
  _HealthSummaryPageState createState() => _HealthSummaryPageState();
}

class _HealthSummaryPageState extends State<HealthSummaryPage> {
  PatiantInformtion? patient;

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    patient = routeArgs['patient'];
    String patientType = routeArgs['patientType'];
    String arrivalType = routeArgs['arrivalType'];
    bool isInpatient = routeArgs['isInpatient'];
    return BaseView<MedicalFileViewModel>(
      onModelReady: (model) => model.getMedicalFile(mrn: patient!.patientId),
      builder: (BuildContext context, MedicalFileViewModel model, Widget? child) => AppScaffold(
        appBar: PatientProfileAppBar(
          patient!,
          isInpatient: isInpatient,
        ),
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).medicalReport.toUpperCase(),
        body: NetworkBaseView(
          baseViewModel: model,
          child: SingleChildScrollView(
            child: Container(
              child: Column(
                children: [
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
                    child: Container(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                AppText(
                                  TranslationBase.of(context).health,
                                  fontSize: 15.0,
                                  fontWeight: FontWeight.w600,
                                  fontFamily: 'Poppins',
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                AppText(
                                  TranslationBase.of(context).summary,
                                  fontSize: 30.0,
                                  fontWeight: FontWeight.w700,
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  (model.medicalFileList != null && model.medicalFileList.length != 0)
                      ? ListView.builder(
                          //physics: ,
                          physics: NeverScrollableScrollPhysics(),
                          scrollDirection: Axis.vertical,
                          shrinkWrap: true,
                          itemCount: model.medicalFileList[0].entityList![0].timelines!.length,
                          itemBuilder: (BuildContext ctxt, int index) {
                            return InkWell(
                              onTap: () async {
                                if (model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations!.length != 0)
                                  await locator<AnalyticsService>().logEvent(
                                    eventCategory: "Health Summary Page",
                                    eventAction: "Health Summary Details",
                                  );
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) => MedicalFileDetails(
                                        age: patient!.age is String ? patient!.age ?? "" : "${patient!.age}",
                                        firstName: patient!.firstName!,
                                        lastName: patient!.lastName!,
                                        gender: patient!.genderDescription!,
                                        encounterNumber: index,
                                        pp: patient!.patientId!,
                                        patient: patient!,
                                        doctorName: model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations!.isNotEmpty
                                            ? model.medicalFileList[0].entityList![0].timelines![index].doctorName!
                                            : "",
                                        clinicName: model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations!.isNotEmpty
                                            ? model.medicalFileList[0].entityList![0].timelines![index].clinicName!
                                            : "",
                                        doctorImage: model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations!.isNotEmpty
                                            ? model.medicalFileList[0].entityList![0].timelines![index].doctorImage!
                                            : "",
                                        episode: model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations!.isNotEmpty
                                            ? model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations![0].episodeID.toString()
                                            : "",
                                        vistDate: model.medicalFileList[0].entityList![0].timelines![index].date.toString()),
                                    settings: RouteSettings(name: 'MedicalFileDetails'),
                                  ),
                                );
                              },
                              child: DoctorCard(
                                  doctorName: model.medicalFileList[0].entityList![0].timelines![index].doctorName!,
                                  clinic: model.medicalFileList[0].entityList![0].timelines![index].clinicName!,
                                  branch: model.medicalFileList[0].entityList![0].timelines![index].projectName!,
                                  profileUrl: model.medicalFileList[0].entityList![0].timelines![index].doctorImage!,
                                  appointmentDate: AppDateUtils.getDateTimeFromServerFormat(
                                    model.medicalFileList[0].entityList![0].timelines![index].date!,
                                  ),
                                  isPrescriptions: true,
                                  isShowEye: model.medicalFileList[0].entityList![0].timelines![index].timeLineEvents![0].consulations!.length != 0 ? true : false),
                            );
                          })
                      : Center(
                          child: ErrorMessage(
                          error: TranslationBase.of(context).noMedicalFileFound,
                        ))
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
