import 'package:badges/badges.dart' as badge_import;
import 'package:hmg_patient_app/Constants.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Appointments/toDoCountProviderModel.dart';
import 'package:hmg_patient_app/models/InPatientServices/get_inpatient_advance_requests_response_model.dart';
import 'package:hmg_patient_app/pages/InPatientServices/inpatient_advance_payment.dart';
import 'package:hmg_patient_app/pages/ToDoList/ToDo.dart';
import 'package:hmg_patient_app/pages/medical/balance/advance_payment_page.dart';
import 'package:hmg_patient_app/pages/medical/balance/my_balance_page.dart';
import 'package:hmg_patient_app/services/clinic_services/get_clinic_service.dart';
import 'package:hmg_patient_app/uitl/app_toast.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/widgets/data_display/medical/medical_profile_item.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.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';

class PaymentService extends StatelessWidget {
  ToDoCountProviderModel? model;
  late ProjectViewModel projectViewModel;

  InPatientAdvanceResponseModel inPatientAdvanceResponseModel = InPatientAdvanceResponseModel();

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    model = Provider.of<ToDoCountProviderModel>(context);
    List<Widget> paymentServiceList = getPaymentServiceList(context);
    return AppScaffold(
      isShowAppBar: true,
      isShowDecPage: false,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      appBarTitle: TranslationBase.of(context).paymentService,
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.all(20.0),
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.only(left: 12, right: 12),
                child: GridView.builder(
                  shrinkWrap: true,
                  primary: false,
                  physics: NeverScrollableScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, childAspectRatio: 2 / 2, crossAxisSpacing: 12, mainAxisSpacing: 12),
                  padding: EdgeInsets.zero,
                  itemCount: paymentServiceList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return paymentServiceList[index];
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> getPaymentServiceList(BuildContext context) {
    List<Widget> medical = [];

    medical.add(
      InkWell(
        onTap: () {
          Navigator.push(context, FadePage(page: AdvancePaymentPage()));
          projectViewModel!.analytics.advancePayments.payment_services(service_type: 'payment service');
        },
        child: MedicalProfileItem(
          title: TranslationBase.of(context).onlinePayment,
          imagePath: 'assets/images/new-design/online_payment_icon.png',
          subTitle: TranslationBase.of(context).onlinePaymentSubtitle,
          isPngImage: true,
          width: 65.0,
          height: 55.0,
        ),
      ),
    );

    medical.add(
      InkWell(
        onTap: () {
          navigateToToDoPage(context);
          projectViewModel!.analytics.advancePayments.payment_services(service_type: 'online check-in appointment');
        },
        child: Stack(
          children: [
            MedicalProfileItem(
              title: TranslationBase.of(context).pendingOnly,
              imagePath: 'pendingPayment.svg',
              subTitle: TranslationBase.of(context).paymentOnly,
              isPngImage: false,
              width: 45.0,
              height: 45.0,
            ),
            projectViewModel!.isArabic
                ? Positioned(
                    left: 8,
                    top: 4,
                    child: badge_import.Badge(
                      badgeStyle: badge_import.BadgeStyle(
                        shape: badge_import.BadgeShape.circle,
                        elevation: 0,
                        badgeColor: secondaryColor!.withOpacity(1.0),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      position: badge_import.BadgePosition.topEnd(),
                      badgeContent: Container(
                        padding: EdgeInsets.all(2.0),
                        child: Text(model!.count.toString(), style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12.0)),
                      ),
                    ),
                  )
                : Positioned(
                    right: 8,
                    top: 4,
                    child: badge_import.Badge(
                      position: badge_import.BadgePosition.topEnd(),
                      badgeStyle: badge_import.BadgeStyle(
                        shape: badge_import.BadgeShape.circle,
                        elevation: 0,
                        badgeColor: secondaryColor!.withOpacity(1.0),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      badgeContent: Container(
                        padding: EdgeInsets.all(2.0),
                        child: Text(model!.count.toString(), style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12.0)),
                      ),
                    ),
                  )
          ],
        ),
      ),
    );

    medical.add(
      InkWell(
        onTap: () {
          Navigator.push(context, FadePage(page: MyBalancePage()));
          projectViewModel!.analytics.advancePayments.payment_services(service_type: 'alhabib wallet');
        },
        child: MedicalProfileItem(
          title: TranslationBase.of(context).hmg,
          imagePath: 'assets/images/check-in.png',
          subTitle: TranslationBase.of(context).wallet,
          isPngImage: true,
          width: 45.0,
          height: 45.0,
        ),
      ),
    );

    // medical.add(
    //   InkWell(
    //     onTap: () {
    //       if(projectViewModel.isPatientAdmitted) {
    //         getInPatientAdvancePaymentRequests(context);
    //         projectViewModel.analytics.advancePayments.payment_services(service_type: 'inpatient advance payment');
    //       } else {
    //         AppToast.showErrorToast(message: TranslationBase.of(context).onlyAdmitted);
    //       }
    //     },
    //     child: MedicalProfileItem(
    //       title: "InPatient",
    //       imagePath: 'assets/images/inpatient_advance.png',
    //       subTitle: "Advance Payment",
    //       isPngImage: true,
    //       width: 45.0,
    //       height: 45.0,
    //     ),
    //   ),
    // );

    return medical;
  }

  void getInPatientAdvancePaymentRequests(BuildContext context) {
    ClinicListService service = new ClinicListService();
    GifLoaderDialogUtils.showMyDialog(context);
    service
        .getInPatientAdvancePaymentRequests(
            projectViewModel.inPatientProjectID,
            projectViewModel.isPatientAdmitted ? projectViewModel.getAdmissionInfoResponseModel.admissionNo! : 0,
            projectViewModel.isPatientAdmitted ? projectViewModel.getAdmissionInfoResponseModel.admissionRequestNo! : projectViewModel.getAdmissionRequestInfoResponseModel.admissionRequestNo!,
            projectViewModel.isArabic ? 1 : 2,
            context)
        .then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res["MessageStatus"] == 1) {
        if (res['responseInpatient'] != null) {
          inPatientAdvanceResponseModel = InPatientAdvanceResponseModel.fromJson(res["responseInpatient"]);
          Navigator.push(context,
              FadePage(page: InPatientAdvancePayment(isHasData: inPatientAdvanceResponseModel.responseInpatientAdvanceInfo != null, inPatientAdvanceResponseModel: inPatientAdvanceResponseModel)));
        } else {
          AppToast.showErrorToast(message: TranslationBase.of(context).noData);
        }
      } else {
        Navigator.push(context,
            FadePage(page: InPatientAdvancePayment(isHasData: inPatientAdvanceResponseModel.responseInpatientAdvanceInfo != null, inPatientAdvanceResponseModel: inPatientAdvanceResponseModel)));
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  navigateToToDoPage(BuildContext context) {
    if (projectViewModel!.isLogin) {
      // if (model.count != 0) {
      Navigator.push(context, FadePage(page: ToDo(isShowAppBar: true)));
      // } else {
      //   AppToast.showErrorToast(message: TranslationBase.of(context).upcomingEmpty);
      // }
    } else {
      Navigator.push(context, FadePage(page: ToDo(isShowAppBar: true)));
    }
  }
}
