import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/insurance/Insurance_card_details.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/base/base_view.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/date_uitl.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/buttons/secondary_button.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'AttachInsuranceCardImageDialog.dart';

class InsuranceCardUpdateDetails extends StatelessWidget {
  final List<InsuranceCardDetailsModel> insuranceCardDetailsModel;
  final String patientIdentificationID;
  final int patientID;
  final String name;
  final String mobileNo;

  const InsuranceCardUpdateDetails({Key? key, required this.insuranceCardDetailsModel, required this.patientIdentificationID, required this.patientID, required this.name, required this.mobileNo}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<InsuranceViewModel>(
      builder: (context, model, w) => AppScaffold(
        isShowAppBar: true,
        isShowDecPage: false,
        appBarTitle: TranslationBase.of(context).cardDetail,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        backgroundColor: CustomColors.appBackgroudGreyColor,
        body: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: Column(
                    children: [
                      ListView.separated(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemBuilder: (context, index) {
                          return Container(
                            width: double.maxFinite,
                            // height: projectViewModel.isArabic ? 320 : 240,
                            // decoration: containerRadius(CustomColors.accentColor, 12),
                            child: Container(
                              decoration: cardRadius(12, color: CustomColors.accentColor),
                              child: Padding(
                                padding: const EdgeInsets.all(12.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      insuranceCardDetailsModel[index].memberID!,
                                      style: TextStyle(
                                        color: Colors.white,
                                        letterSpacing: -0.48,
                                        fontWeight: FontWeight.w600,
                                        fontSize: 16,
                                      ),
                                    ),
                                    SizedBox(
                                      height: 2,
                                    ),
                                    Text(
                                      insuranceCardDetailsModel[index].companyName!,
                                      style: TextStyle(
                                        color: Colors.white,
                                        letterSpacing: -0.48,
                                        fontWeight: FontWeight.w600,
                                        fontSize: 14,
                                      ),
                                    ),
                                    SizedBox(
                                      height: 14,
                                    ),
                                    Row(
                                      children: [
                                        Expanded(
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                TranslationBase.of(context).policyHolder,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontWeight: FontWeight.w600,
                                                  fontSize: 12,
                                                ),
                                              ),
                                              Text(
                                                insuranceCardDetailsModel[index].memberName!,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontSize: 12,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              TranslationBase.of(context).policyNo,
                                              style: TextStyle(
                                                color: Colors.white,
                                                letterSpacing: -0.48,
                                                fontWeight: FontWeight.w600,
                                                fontSize: 12,
                                              ),
                                            ),
                                            Text(
                                              insuranceCardDetailsModel[index].policyNumber!,
                                              style: TextStyle(
                                                color: Colors.white,
                                                letterSpacing: -0.48,
                                                fontSize: 12,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                    SizedBox(
                                      height: 14,
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: [
                                        Expanded(
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                TranslationBase.of(context).expiryDateTitle,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontWeight: FontWeight.w600,
                                                  fontSize: 12,
                                                ),
                                              ),
                                              Text(
                                                insuranceCardDetailsModel[index].effectiveTo!,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontSize: 12,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        Expanded(
                                          child: Column(
                                            children: [
                                              Text(
                                                TranslationBase.of(context).classTitle,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontWeight: FontWeight.w600,
                                                  fontSize: 12,
                                                ),
                                              ),
                                              Text(
                                                insuranceCardDetailsModel[index].subCategory!,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontSize: 12,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        Expanded(
                                          child: Column(
                                            children: [
                                              Text(
                                                TranslationBase.of(context).approval,
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontWeight: FontWeight.w600,
                                                  fontSize: 12,
                                                ),
                                              ),
                                              Text(
                                                insuranceCardDetailsModel[index].approvalLimit.toString(),
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  letterSpacing: -0.48,
                                                  fontSize: 12,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    )
                                  ],
                                ),
                              ),
                            ),
                          );
                        },
                        separatorBuilder: (context, index) {
                          return mHeight(12);
                        },
                        itemCount: insuranceCardDetailsModel.length,
                      ),
                      if (insuranceCardDetailsModel.isEmpty)
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Container(
                              child: Container(
                                decoration: cardRadius(12),
                                child: Container(
                                  width: MediaQuery.of(context).size.width,
                                  padding: EdgeInsets.all(10.0),
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisSize: MainAxisSize.max,
                                    children: <Widget>[
                                      Expanded(
                                        child: Container(
                                          margin: EdgeInsets.only(top: 2.0, left: 10.0, right: 20.0),
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Text(
                                                name,
                                                style: TextStyle(
                                                  fontSize: 14,
                                                  fontWeight: FontWeight.w600,
                                                  letterSpacing: -0.46,
                                                ),
                                              ),
                                              Text(
                                                TranslationBase.of(context).fileno + ": " + patientID.toString(),
                                                style: TextStyle(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w600,
                                                  letterSpacing: -0.46,
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(
                              height: 150,
                            ),
                            Image.asset(
                              'assets/images/no-data-found.png',
                              width: 80,
                              height: 80,
                            ),
                            SizedBox(
                              height: 8,
                            ),
                            Text(
                              TranslationBase.of(context).noDataAvailable,
                              style: TextStyle(
                                fontSize: 14,
                                fontWeight: FontWeight.w600,
                                letterSpacing: -0.48,
                              ),
                            )
                          ],
                        )
                    ],
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                children: [
                  if (insuranceCardDetailsModel.isEmpty)
                    SecondaryButton(
                      textColor: Colors.white,
                      label: TranslationBase.of(context).scanNow.toUpperCase(),
                      color: CustomColors.accentColor,
                      small: true,
                      onTap: () async {
                        confirmAttachInsuranceCardImageDialogDialog(
                            context: context, name: name, fileNo: patientID.toString(), identificationNo: patientIdentificationID, mobileNo: mobileNo, model: model);
                      },
                    ),
                  if (insuranceCardDetailsModel.isNotEmpty)
                    SecondaryButton(
                      textColor: Colors.white,
                      label: TranslationBase.of(context).agree.toUpperCase(),
                      color: CustomColors.accentColor,
                      onTap: () async {
                        GifLoaderDialogUtils.showMyDialog(context);
                        model.uploadInsuranceCard(context, patientIdentificationID: patientIdentificationID, patientID: patientID).then((res) {
                          print(res);
                          GifLoaderDialogUtils.hideDialog(context);
                        }).catchError((err) {
                          GifLoaderDialogUtils.hideDialog(context);
                          AppToast.showErrorToast(message: err.toString());
                          print(err);
                        });
                      },
                    ),
                  if (insuranceCardDetailsModel.isNotEmpty)
                    SizedBox(
                      height: 12,
                    ),
                  if (insuranceCardDetailsModel.isNotEmpty)
                    SecondaryButton(
                      textColor: Colors.white,
                      label: TranslationBase.of(context).disagree.toUpperCase(),
                      color: Colors.grey[800],
                      onTap: () async {
                        confirmAttachInsuranceCardImageDialogDialog(
                            context: context, name: name, fileNo: patientID.toString(), identificationNo: patientIdentificationID, mobileNo: mobileNo, model: model);
                      },
                    )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void confirmAttachInsuranceCardImageDialogDialog({required BuildContext context, required String name, required String fileNo, required String identificationNo, required String mobileNo, required InsuranceViewModel model}) {
    showDialog(
      context: context,
      builder: (cxt) => AttachInsuranceCardImageDialog(
        fileNo: fileNo,
        name: name,
        identificationNo: identificationNo,
        mobileNo: mobileNo,
        image: (file, image) async {
          GifLoaderDialogUtils.showMyDialog(context);
          await model.uploadInsuranceCard(context, patientIdentificationID: patientIdentificationID, patientID: patientID, image: image);
          GifLoaderDialogUtils.hideDialog(context);
          if (model.state == ViewState.ErrorLocal || model.state == ViewState.Error) {
            AppToast.showErrorToast(message: model.error);
          } else {
            AppToast.showSuccessToast(message: TranslationBase.of(context).requestSent);
          }
          Navigator.pop(context);
        },
      ),
    );
  }
}
