import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/service/AnalyticsService.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientMedicalReportViewModel.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.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/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/add-order/addNewOrder.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/large_avatar.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/card_with_bg_widget.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../core/model/doctor/doctor_profile_model.dart';
import '../../../../routes.dart';
import 'AddVerifyMedicalReport.dart';

class MedicalReportPage extends StatefulWidget {
  @override
  _MedicalReportPageState createState() => _MedicalReportPageState();
}

class _MedicalReportPageState extends State<MedicalReportPage> {
  bool isVidaPlusProject = false;
  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    String patientType = routeArgs['patientType'];
    String arrivalType = routeArgs['arrivalType'];
    isVidaPlusProject = routeArgs['isVidaPlusProject'];
    ProjectViewModel projectViewModel = Provider.of(context);

    return BaseView<PatientMedicalReportViewModel>(
      onModelReady: (model) async {
        await model.getDoctorProfile(isGetProfile: true);
        await model.getMedicalReportList(patient, isVidaPlusProject: isVidaPlusProject);
        await model.getMedicalReportTemplate();
      },
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: true,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        appBar: PatientProfileAppBar(
          patient,
        ),
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    AppText(
                      "${TranslationBase.of(context).medical}",
                      fontSize: SizeConfig.textMultiplier! * 1.6,
                      fontWeight: FontWeight.w700,
                      color: Color(0xFF2E303A),
                    ),
                    AppText(
                      TranslationBase.of(context).report,
                      fontSize: SizeConfig.textMultiplier! * 3,
                      fontWeight: FontWeight.bold,
                      color: Color(0xFF2E303A),
                    )
                  ],
                ),
              ),
              AddNewOrder(
                onTap: () async {
                  // if (model.hasOnHold()) {
                  //   Helpers.showErrorToast("Please Verified the on hold report to be able to add new one");
                  // } else
                  await locator<AnalyticsService>().logEvent(
                    eventCategory: "Medical Report Page",
                    eventAction: "Add New Medical Report",
                  );
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => AddVerifyMedicalReport(
                        patient: patient,
                        patientType: patientType,
                        arrivalType: arrivalType,
                        model: model,
                        status: MedicalReportStatus.ADD,
                          isVidaPlusProject: isVidaPlusProject
                      ),
                      settings: RouteSettings(name: 'AddVerifyMedicalReport'),
                    ),
                  );
                },
                label: TranslationBase.of(context).createNewMedicalReport,
              ),
              //  if (model.state != ViewState.ErrorLocal)ß
              ...List.generate(
                model.medicalReportList.length,
                (index) => InkWell(
                  onTap: () {
                    if (model.medicalReportList[index].status == 1) {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => AddVerifyMedicalReport(
                                  patient: patient,
                                  patientType: patientType,
                                  arrivalType: arrivalType,
                                  medicalReport: model.medicalReportList[index],
                                  model: model,
                                  medicalNote: model
                                      .medicalReportList[index].reportDataHtml,
                                  isVidaPlusProject: isVidaPlusProject
                                ),
                            settings:
                                RouteSettings(name: 'AddVerifyMedicalReport')),
                      );
                    } else {
                      Navigator.of(context)
                          .pushNamed(PATIENT_MEDICAL_REPORT_DETAIL, arguments: {
                        'patient': patient,
                        'patientType': patientType,
                        'arrivalType': arrivalType,
                        'type': MedicalReportStatus.ADD,
                        'medicalReport': model.medicalReportList[index],
                        'model': model,
                      });
                    }
                  },
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 8),
                    child: CardWithBgWidget(
                      hasBorder: false,
                      bgColor: model.medicalReportList[index].status == 1
                          ? Color(0xFFCC9B14)
                          : AppGlobal.appGreenColor,
                      widget: Column(
                        children: [
                          Row(
                            children: [
                              Expanded(
                                  child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  AppText(
                                    model.medicalReportList[index].status == 1
                                        ? isVidaPlusProject
                                            ? TranslationBase.of(context)
                                                .pending
                                            :   TranslationBase.of(context).onHold
                                        : TranslationBase.of(context).verified,
                                    color:
                                        model.medicalReportList[index].status ==
                                                1
                                            ? Color(0xFFCC9B14)
                                            : AppGlobal.appGreenColor,
                                    fontSize: 1.4 * SizeConfig.textMultiplier!,
                                    bold: true,
                                  ),
                                  AppText(
                                    projectViewModel.isArabic
                                        ? model.medicalReportList[index]
                                                .doctorNameN ??
                                            ""
                                        : model.medicalReportList[index]
                                                .doctorName ??
                                            "",
                                    fontSize: 1.9 * SizeConfig.textMultiplier!,
                                    fontWeight: FontWeight.w700,
                                    color: Color(0xFF2E303A),
                                  ),
                                ],
                              )),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  AppText(
                                    '${AppDateUtils.convertDateFromServerFormat(model.medicalReportList[index].editedOn ?? model.medicalReportList[index].createdOn!, "dd MMM yyyy")}',
                                    color: Color(0xFF2E303A),
                                    fontWeight: FontWeight.w600,
                                    fontSize: 1.6 * SizeConfig.textMultiplier!,
                                  ),
                                  AppText(
                                    '${AppDateUtils.convertDateFromServerFormat(model.medicalReportList[index].editedOn ?? model.medicalReportList[index].createdOn!, "hh:mm a")}',
                                    color: Color(0xFF2E303A),
                                    fontWeight: FontWeight.w600,
                                    fontSize: 1.5 * SizeConfig.textMultiplier!,
                                  ),
                                ],
                              ),
                            ],
                          ),
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                margin: EdgeInsets.only(
                                    left: 0, top: 4, right: 8, bottom: 0),
                                child: LargeAvatar(
                                  name: projectViewModel.isArabic
                                      ? model
                                          .medicalReportList[index].doctorNameN!
                                      : model
                                          .medicalReportList[index].doctorName!,
                                  url: model
                                      .medicalReportList[index].doctorImageURL??"",
                                ),
                                width: 50,
                                height: 50,
                              ),
                              Expanded(
                                child: Container(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      AppText(
                                        projectViewModel.isArabic
                                            ? model.medicalReportList[index]
                                                .projectNameN!
                                            : model.medicalReportList[index]
                                                .projectName!,
                                        fontSize:
                                            1.6 * SizeConfig.textMultiplier!,
                                        color: Color(0xFF2E303A),
                                      ),
                                      AppText(
                                        projectViewModel.isArabic
                                            ? model.medicalReportList[index]
                                                .clinicNameN!
                                            : model.medicalReportList[index]
                                                .clinicName!,
                                        fontSize:
                                            1.6 * SizeConfig.textMultiplier!,
                                        color: Color(0xFF2E303A),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Container(
                                height: 50,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Icon(
                                      model.medicalReportList[index].status == 1
                                          ? DoctorApp.edit_1
                                          : EvaIcons.eye,
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 15,
              )
            ],
          ),
        ),
      ),
    );
  }
}
