import 'package:hmg_patient_app/core/service/insurance_service.dart';
import 'package:hmg_patient_app/core/viewModels/insurance_card_View_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/insurance/UpdateInsuranceManually.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../locator.dart';
import '../../widgets/dialogs/confirm_dialog.dart';
import 'insurance_card_update_details.dart';

class InsurancePage extends StatelessWidget {
  final InsuranceViewModel model;
  InsuranceCardService _insuranceCardService = locator<InsuranceCardService>();

  InsurancePage({Key? key, required this.model}) : super(key: key);

  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Column(
          children: [
            Container(
              width: double.infinity,
              child: Container(
                decoration: cardRadius(12),
                child: Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: InkWell(
                    onTap: () {
                      getDetails(
                        setupID: '010266',
                        projectID: 15,
                        patientIdentificationID: projectViewModel.user!.patientIdentificationNo!,
                        //model.user.patientIdentificationNo,
                        patientID: projectViewModel.user!.patientID!,
                        //model.user.patientID,
                        parentID: 0,
                        isFamily: projectViewModel.isLoginChild,
                        //false,
                        name: projectViewModel.user!.firstName! + " " + projectViewModel.user!.lastName!,
                        mobileNumber: projectViewModel.user!.mobileNumber!,
                        //model.user.firstName + " " + model.user.lastName,
                        context: context,
                      );
                    },
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      mainAxisSize: MainAxisSize.max,
                      children: <Widget>[
                        if (model.user != null)
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                  projectViewModel.user!.firstName! + " " + projectViewModel.user!.lastName!,
                                  style: TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.w600,
                                    letterSpacing: -0.46,
                                  ),
                                ),
                                Text(
                                  TranslationBase.of(context).fileno + ": " + projectViewModel.user!.patientID.toString(), //model.user.patientID.toString(),
                                  style: TextStyle(
                                    fontSize: 12,
                                    fontWeight: FontWeight.w600,
                                    letterSpacing: -0.46,
                                  ),
                                )
                              ],
                            ),
                          ),
                        Icon(Icons.arrow_forward),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            if (!projectViewModel.isLoginChild)
              if (model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList != null ?? false)
                ListView.separated(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].status == 3
                        ? InkWell(
                            onTap: () {
                              getDetails(
                                  projectID: 15,
                                  patientIdentificationID: model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].patientIdenficationNumber!,
                                  setupID: '010266',
                                  patientID: model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].responseID!,
                                  name: model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].patientName!,
                                  parentID: model.user!.patientID!,
                                  isFamily: true,
                                  mobileNumber: model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].mobileNumber!,
                                  context: context);
                            },
                            child: Container(
                              width: double.infinity,
                              margin: EdgeInsets.only(top: 12.0),
                              child: Container(
                                decoration: cardRadius(12),
                                child: Padding(
                                  padding: const EdgeInsets.all(12.0),
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    mainAxisSize: MainAxisSize.max,
                                    children: [
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].patientName!,
                                              style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, letterSpacing: -0.46),
                                            ),
                                            Text(
                                              TranslationBase.of(context).fileno + ": " + model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].responseID.toString(),
                                              style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, letterSpacing: -0.46),
                                            ),
                                          ],
                                        ),
                                      ),
                                      Icon(Icons.arrow_forward),
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          )
                        : Container();
                  },
                  separatorBuilder: (context, index) {
                    return mHeight(model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList![index].status == 3 ? 8 : 0);
                  },
                  itemCount: model.getAllSharedRecordsByStatusResponse.getAllSharedRecordsByStatusList!.length,
                ),
          ],
        ),
      ),
    );
  }

  getDetails({required String setupID, required int projectID, required String patientIdentificationID, required int patientID, required String name, required String mobileNumber, required bool isFamily, int parentID = 0, required BuildContext context}) {
    GifLoaderDialogUtils.showMyDialog(context);
    _insuranceCardService
        .getPatientInsuranceDetails(setupID: setupID, projectID: projectID, patientID: patientID, patientIdentificationID: patientIdentificationID, isFamily: isFamily, parentID: parentID)
        .then((value) {
      GifLoaderDialogUtils.hideDialog(context);
      if (!_insuranceCardService.hasError && _insuranceCardService.isHaveInsuranceCard) {
        Navigator.push(
            context,
            FadePage(
                page: InsuranceCardUpdateDetails(
              insuranceCardDetailsModel: _insuranceCardService.insuranceCardDetailsList,
              patientID: patientID,
              patientIdentificationID: patientIdentificationID,
              name: name,
              mobileNo: mobileNumber,
            ))).then((value) {
          model.getInsuranceUpdated();
        });
      } else {
        // AppToast.showErrorToast(message: _insuranceCardService.error);
        updateManually(context, _insuranceCardService.error!, patientIdentificationID, patientID, mobileNumber);
      }
    });
  }

  void updateManually(BuildContext context, String errorMsg, String patientIdentificationID, int patientID, String mobileNumber) {
    ConfirmDialog dialog = new ConfirmDialog(
        context: context,
        confirmMessage: errorMsg + ". " + TranslationBase.of(context).habibCallCenter,
        okText: TranslationBase.of(context).ok,
        cancelText: TranslationBase.of(context).cancel,
        // confirmMessage: errorMsg + ". " + TranslationBase.of(context).updateInsuranceManuallyDialog,
        // okText: TranslationBase.of(context).yes,
        // cancelText: TranslationBase.of(context).no,
        okFunction: () => {
              Navigator.pop(context),
              // Navigator.push(
              //   context,
              //   FadePage(
              //     page: UpdateInsuranceManually(
              //       patientIdentificationNo: patientIdentificationID,
              //       patientID: patientID,
              //       patientMobileNumber: mobileNumber,
              //     ),
              //   ),
              // ),
            },
        cancelFunction: () => {});
    dialog.showAlertDialog(context);
  }
}
