import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/service/AnalyticsService.dart';
import 'package:doctor_app_flutter/core/viewModel/InsuranceViewModel.dart';
import 'package:doctor_app_flutter/core/viewModel/project_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/patients/insurance_approvals_details.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_service_title.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_insurance.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

class InsuranceApprovalScreenNew extends StatefulWidget {
  final int appointmentNo;

  InsuranceApprovalScreenNew({this.appointmentNo = 0});

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

class _InsuranceApprovalScreenNewState
    extends State<InsuranceApprovalScreenNew> {
  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    patient = routeArgs['patient'];
    String patientType = routeArgs['patientType'];
    bool isInpatient = routeArgs['isInpatient'];
    return BaseView<InsuranceViewModel>(
      onModelReady: patient.admissionNo != null
          ? (model) => model.getInsuranceInPatient(mrn: patient.patientId)
          : patient.appointmentNo != null
              ? (model) => model.getInsuranceApproval(patient,
                  appointmentNo: int.parse(patient.appointmentNo!.toString()),
                  projectId: patient.projectId)
              : (model) => model.getInsuranceApproval(patient),
      builder: (BuildContext context, InsuranceViewModel model, Widget? child) =>
          AppScaffold(
        appBar: PatientProfileAppBar(
          patient,
          isInpatient: isInpatient,
        ),
        isShowAppBar: true,
        baseViewModel: model,
        appBarTitle: TranslationBase.of(context).approvals,
        body: patient.admissionNo != null
            ? SingleChildScrollView(
                child: Container(
                  margin: EdgeInsets.only(
                    left: SizeConfig.screenWidth! * 0.004,
                    right: SizeConfig.screenWidth! * 0.004,
                    top: SizeConfig.screenWidth! * 0.04,
                  ),
                  child: model.insuranceApprovalInPatient.length != 0
                      ? Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            ServiceTitle(
                              title: TranslationBase.of(context).insurance22,
                              subTitle: TranslationBase.of(context).approvals22,
                            ),
                            ...List.generate(
                              model.insuranceApprovalInPatient.length,
                              (index) => Container(
                                child: InkWell(
                                  onTap: () {
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                        builder: (context) =>
                                            InsuranceApprovalsDetails(
                                          patient: patient,
                                          indexInsurance: index,
                                          patientType: patientType,
                                        ),
                                        settings: RouteSettings(
                                            name: 'InsuranceApprovalsDetails'),
                                      ),
                                    );
                                  },
                                  child: DoctorCardInsurance(
                                    patientOut: "In Patient",
                                    onTap: (){},
                                    profileUrl: model
                                        .insuranceApprovalInPatient[index]
                                        .doctorImage??'',
                                    clinic: model
                                        .insuranceApprovalInPatient[index]
                                        .clinicName!,
                                    doctorName: model
                                        .insuranceApprovalInPatient[index]
                                        .doctorName!,
                                    approvalNo: model
                                        .insuranceApprovalInPatient[index]
                                        .approvalNo
                                        .toString(),
                                    isInsurance: true,
                                    approvalStatus: model
                                            .insuranceApprovalInPatient[index]
                                            .approvalStatusDescption ??
                                        '',
                                    branch2: model
                                        .insuranceApprovalInPatient[index]
                                        .projectName!,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        )
                      : Center(
                          child: ErrorMessage(
                          error: TranslationBase.of(context).errorNoInsuranceApprovals,
                        )),
                ),
              )
            : SingleChildScrollView(
                child: Container(
                    margin: EdgeInsets.only(
                      left: SizeConfig.screenWidth! * 0.004,
                      right: SizeConfig.screenWidth! * 0.004,
                      top: SizeConfig.screenWidth! * 0.04,
                    ),
                    child: model.insuranceApproval.length != 0
                        ? Column(
                            children: <Widget>[
                              ServiceTitle(
                                title: TranslationBase.of(context).insurance22,
                                subTitle:
                                    TranslationBase.of(context).approvals22,
                              ),
                              ...List.generate(
                                model.insuranceApproval.length,
                                (index) => Container(
                                  child: InkWell(
                                    onTap: () async {
                                      await locator<AnalyticsService>()
                                          .logEvent(
                                        eventCategory:
                                            "Insurance Approval Screen New",
                                        eventAction:
                                            "Insurance Approval Details",
                                      );
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) =>
                                              InsuranceApprovalsDetails(
                                            patient: patient,
                                            indexInsurance: index,
                                            patientType: patientType,
                                          ),
                                          settings: RouteSettings(
                                              name:
                                                  'InsuranceApprovalsDetails'),
                                        ),
                                      );
                                    },
                                    child: DoctorCardInsurance(
                                      patientOut: model.insuranceApproval[index]
                                          .patientDescription!,
                                      profileUrl: model
                                          .insuranceApproval[index].doctorImage!,
                                      clinic: model
                                          .insuranceApproval[index].clinicName!,
                                      doctorName: model
                                          .insuranceApproval[index].doctorName!,
                                      approvalNo: model
                                          .insuranceApproval[index].approvalNo
                                          .toString(),
                                      isInsurance: true,
                                      approvalStatus: model
                                              .insuranceApproval[index]
                                              .approvalStatusDescption ??
                                          '',
                                      branch2: model
                                          .insuranceApproval[index].projectName!,
                                      onTap: (){},
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          )
                        : Center(
                            child: ErrorMessage(
                            error: TranslationBase.of(context)
                                .noInsuranceApprovalFound,
                          ))),
              ),
      ),
    );
  }
}
