import 'package:hmg_patient_app/core/model/pharmacies/PointsAmountPerYear.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/lacum-viewmodel.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/pages/pharmacies/screens/lacum-registration-page.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/lacum-setting-page.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/lacum-transfer-page.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/lakum-points-year-page.dart';
import 'package:hmg_patient_app/pages/pharmacies/widgets/lacum-banner-widget.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/data_display/text.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/material.dart';
import 'package:provider/provider.dart';

import 'lacum-activitaion-vida-page.dart';

class LakumMainPage extends StatelessWidget {
  ProjectViewModel? projectViewModel;

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    projectViewModel = Provider.of(context);

    return BaseView<LacumViewModel>(
      onModelReady: (model) async {
        await model.getLacumData(projectViewModel!.user!.patientIdentificationNo!);
        if (model.lacumInformation!.yahalaAccountNo == 0 || model.lacumInformation!.yahalaAccountNo == null) {
          navigateToLakumRegister(context);
        } else {
          if (model.lacumInformation!.status == "Hold") {
            Navigator.pushReplacement(context, FadePage(page: LakumActivationVidaPage()));
          }
        }
      },
      builder: (_, model, wi) => AppScaffold(
        appBarTitle: TranslationBase.of(context).lakum,
        isShowAppBar: true,
        isPharmacy: true,
        showPharmacyCart: false,
        isShowDecPage: false,
        showHomeAppBarIcon: false,
        isBottomBar: true,
        backgroundColor: Colors.white,
        baseViewModel: model,
        appBarIcons: _buildAppBarICons(context, model),
        // preferredSize: mediaQuery.size.height * 0.20,
        body: Container(
          width: double.infinity,
          child: SingleChildScrollView(
            child: (model.lacumGroupInformation != null && model.lacumGroupInformation!.lakumInquiryInformationObjVersion != null)
                ? Column(
                    children: [
                      Stack(
                        children: [
                          Container(
                            height: mediaQuery.size.height * 0.15,
                            color: Colors.green,
                          ),
                          Column(
                            children: [
                              SizedBox(
                                height: mediaQuery.size.height * 0.02,
                              ),
                              Container(width: mediaQuery.size.width * 1, height: mediaQuery.size.width * .6, child: LakumBannerWidget(model, mediaQuery, true)),
                            ],
                          )
                        ],
                      ),
                      SizedBox(
                        height: 12,
                      ),
                      LakumHomeButtons(mediaQuery, model, projectViewModel!),
                      SizedBox(
                        height: 12,
                      ),
                      Container(
                        height: 110,
                        margin: EdgeInsets.symmetric(horizontal: 16, vertical: 12.0),
                        child: ListView(
                          scrollDirection: Axis.horizontal,
                          children: <Widget>[
                            LacumPointsWidget(mediaQuery, 1, TranslationBase.of(context).balance, model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.pointsBalanceAmount!,
                                model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.pointsBalance!, []),
                            SizedBox(
                              width: 8,
                            ),
                            LacumPointsWidget(mediaQuery, 2, TranslationBase.of(context).gained, model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.gainedPointsAmount!,
                                model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.gainedPoints!, model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.gainedPointsAmountPerYear!),
                            SizedBox(
                              width: 8,
                            ),
                            LacumPointsWidget(
                                mediaQuery,
                                3,
                                TranslationBase.of(context).consumed,
                                model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.consumedPointsAmount! != null
                                    ? num.parse(model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.consumedPointsAmount!)
                                    : 0,
                                model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.consumedPoints!,
                                model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.consumedPointsAmountPerYear!),
                            SizedBox(
                              width: 8,
                            ),
                            LacumPointsWidget(mediaQuery, 4, TranslationBase.of(context).transferred, 0, model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.transferPoints!,
                                model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.transferPointsAmountPerYear!),
                          ],
                        ),
                      ),
                      // LacumPointsWidget(
                      //     mediaQuery,
                      //     3,
                      //     TranslationBase.of(context).consumed,
                      //     model.lacumGroupInformation.lakumInquiryInformationObjVersion.consumedPointsAmount != null
                      //         ? int.parse(model.lacumGroupInformation.lakumInquiryInformationObjVersion.consumedPointsAmount)
                      //         : 0,
                      //     model.lacumGroupInformation.lakumInquiryInformationObjVersion.consumedPoints,
                      //     model.lacumGroupInformation.lakumInquiryInformationObjVersion.consumedPointsAmountPerYear),
                      SizedBox(
                        width: 8,
                      ),
                      SizedBox(
                        height: 8,
                        width: double.infinity,
                        child: Container(
                          color: Color(0xffefefef),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(vertical: 16, horizontal: 8),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Image.asset(
                                  "assets/images/pharmacy_module/lakum/expired_icon.png",
                                  fit: BoxFit.fill,
                                  width: 25,
                                  height: 25,
                                ),
                                Padding(
                                  padding: EdgeInsets.symmetric(horizontal: 8),
                                  child: Texts(
                                    TranslationBase.of(context).expiryPoints,
//                                          "Expired",
                                    fontSize: 14,
                                  ),
                                )
                              ],
                            ),
                            Texts(
                              "${model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.expiredPoints} ${TranslationBase.of(context).lakumPoint} ",
                              fontWeight: FontWeight.bold,
                              fontSize: 14,
                            ),
                          ],
                        ),
                      ),
                      // Texts(
                      //   "${model.lacumGroupInformation.lakumInquiryInformationObjVersion.expiredPoints} Points",
                      //   fontWeight: FontWeight.bold,
                      //   fontSize: 14,
                      // ),
                      Container(
                          margin: EdgeInsets.symmetric(vertical: 16, horizontal: 8),
                          child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                            Row(
                              children: [
                                Image.asset(
                                  "assets/images/pharmacy_module/lakum/waiting_gained_icon.png",
                                  fit: BoxFit.fill,
                                  width: 20,
                                  height: 25,
                                ),
                                Padding(
                                  padding: EdgeInsets.symmetric(horizontal: 8),
                                  child: Texts(
                                    TranslationBase.of(context).Waitinggained,
//                                          "Waiting gained",
                                    fontSize: 14,
                                  ),
                                )
                              ],
                            ),
                            Texts(
                              "${model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.waitingPoints} ${TranslationBase.of(context).lakumPoint}",
                              fontWeight: FontWeight.bold,
                              fontSize: 14,
                            ),
                          ])),
                      // Texts(
                      //   "${model.lacumGroupInformation.lakumInquiryInformationObjVersion.waitingPoints} Points",
                      //   fontWeight: FontWeight.bold,
                      //   fontSize: 14,
                      // ),
                      Container(
                        margin: EdgeInsets.symmetric(vertical: 16, horizontal: 8),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Image.asset(
                                  "assets/images/pharmacy_module/lakum/will_be_expired_icon.png",
                                  fit: BoxFit.fill,
                                  width: 25,
                                  height: 25,
                                ),
                                Padding(
                                  padding: EdgeInsets.symmetric(horizontal: 8),
                                  child: Texts(
                                    TranslationBase.of(context).WillBeExpired,
//                                          "Will Be Expired",
                                    fontSize: 14,
                                  ),
                                )
                              ],
                            ),
                            Texts(
                              "${model.lacumGroupInformation!.lakumInquiryInformationObjVersion!.pointsWillBeExpired} ${TranslationBase.of(context).lakumPoint}",
                              fontWeight: FontWeight.bold,
                              fontSize: 14,
                            ),
                          ],
                        ),
                        // Texts(
                        //   "${model.lacumGroupInformation.lakumInquiryInformationObjVersion.pointsWillBeExpired} Points",
                        //   fontWeight: FontWeight.bold,
                        //   fontSize: 14,
                        // ),
                      ),

                      SizedBox(
                        height: 2,
                        width: double.infinity,
                        child: Container(
                          color: Color(0xffefefef),
                        ),
                      ),
                    ],
                  )
                : Container(),
          ),
        ),
      ),
    );
  }

  navigateToLakumRegister(BuildContext context) {
    Navigator.pushReplacement(context, FadePage(page: LakumRegistrationPage(projectViewModel!.user!.patientIdentificationNo!)));
  }

  List<Widget> _buildAppBarICons(BuildContext context, LacumViewModel model) {
    return [
      IconButton(
        icon: Icon(Icons.settings),
        color: Colors.white,
        onPressed: () {
          Navigator.push(context, FadePage(page: LakumSettingPage(model.lacumInformation!, model.lacumGroupInformation!))).then((result) => {model.getLacumGroupData(projectViewModel!.user!.patientIdentificationNo!)});
        },
      ),
    ];
  }
}

class LakumHomeButtons extends StatelessWidget {
  final MediaQueryData mediaQuery;
  final LacumViewModel model;
  ProjectViewModel projectViewModel;

  LakumHomeButtons(this.mediaQuery, this.model, this.projectViewModel);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        children: [
          Expanded(
            child: InkWell(
//              onTap: () {
//                print("Account activate click");
//                Navigator.push(
//                        context, FadePage(page: LakumActivationVidaPage()))
//                    .then((result) => {model.getLacumGroupData()});
//              },
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 8),
                decoration: BoxDecoration(
                    shape: BoxShape.rectangle,
                    borderRadius: BorderRadius.circular(8),
                    border: Border.fromBorderSide(BorderSide(
                      color: Color(0xffe1e1e1),
                      width: 0.4,
                    )),
                    color: Color(0xffe1e1e1)),
                height: mediaQuery.size.height * 0.08,
                child: Row(
                  children: [
                    Image.asset(
                      "assets/images/pharmacy_module/lakum/Account_activation_icon.png",
                      fit: BoxFit.fill,
                      width: 30,
                      height: 30,
                    ),
                    Expanded(
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        child: Texts(
                          TranslationBase.of(context).accountActivation,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
          SizedBox(
            width: 8,
          ),
          Expanded(
            child: InkWell(
              onTap: () {
                Navigator.push(context, FadePage(page: LacumTransferPage(model.lacumInformation!, model.lacumGroupInformation!))).then((result) {
                  model.getLacumGroupData(projectViewModel.user!.patientIdentificationNo!);
                });
              },
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 8),
                decoration: BoxDecoration(
                    shape: BoxShape.rectangle,
                    borderRadius: BorderRadius.circular(8),
                    border: Border.fromBorderSide(BorderSide(
                      color: Color(0xffe1e1e1),
                      width: 0.4,
                    )),
                    color: Color(0xffe1e1e1)),
                height: mediaQuery.size.height * 0.08,
                child: Row(
                  children: [
                    Image.asset(
                      "assets/images/pharmacy_module/lakum/akum_transfer.png",
                      //   "assets/images/pharmacy_module/lakum/Lakum_transfer_icon.png",
                      fit: BoxFit.fill,
                      width: 35,
                      height: 30,
                    ),
                    Expanded(
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        child: Texts(
                          TranslationBase.of(context).lakumTransfer,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class LacumPointsWidget extends StatelessWidget {
  final MediaQueryData mediaQuery;
  final int pointType; // 1. balance, 2. gained, 3. consume, 4. transfer
  final String title;
  final num riyal;
  final num point;
  Color? titleColor;
  final List<PointsAmountPerYear> pointsAmountPerYear;

  LacumPointsWidget(this.mediaQuery, this.pointType, this.title, this.riyal, this.point, this.pointsAmountPerYear) {
    if (pointType == 1) {
      titleColor = Color(0xffefefef);
    } else if (pointType == 2) {
      //  titleColor = Color(0xff004bcc);
      titleColor = Color(0xff339933);
    } else if (pointType == 3) {
      titleColor = Colors.grey;
    } else {
      titleColor = Color(0xffffa500);
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (pointType != 1) {
          if (pointsAmountPerYear != null && pointsAmountPerYear.length > 0) {
            Navigator.push(context, FadePage(page: LakumPointsYearPage(pointsAmountPerYear)));
          } else {
            AppToast.showErrorToast(message: TranslationBase.of(context).lakumMsg);
            // show snackBar No Details Points are there
          }
        }
      },
      child: Container(
        width: mediaQuery.size.width / 2 - 25,
        padding: EdgeInsets.only(top: 12, left: 8, right: 8, bottom: 8),
        decoration: BoxDecoration(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.circular(8),
          border: Border.fromBorderSide(BorderSide(
            color: Color(0xffe1e1e1),
            width: 0.4,
          )),
          color: pointType == 1 ? Color(0xff339933) : Color(0xffefefef),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: EdgeInsets.only(bottom: 10.0),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child: Texts(
                          title,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: titleColor,
                        ),
                      ),
                      if (pointType != 1)
                        Icon(
                          Icons.arrow_forward,
                          color: titleColor,
                        )
                    ],
                  ),
                ],
              ),
            ),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  if (pointType != 4)
                    Expanded(
                      child: Column(
                        // crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Texts(
                            TranslationBase.of(context).sar,
                            fontSize: 13,
                            fontWeight: FontWeight.bold,
                            color: pointType == 1 ? Colors.white : Colors.black,
                          ),
                          SizedBox(
                            height: 4,
                          ),
                          Texts(
                            "$riyal",
                            fontSize: 13,
                            fontWeight: FontWeight.bold,
                            color: pointType == 1 ? Colors.white : Colors.black,
                          ),
                        ],
                      ),
                    ),
                  if (pointType != 4)
                    SizedBox(
                      child: Container(
                        color: pointType == 1 ? Colors.white : Colors.grey,
                      ),
                      width: 1,
                    ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(left: 8, right: 8),
                      child: Column(
                        // crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Texts(
                            TranslationBase.of(context).points,
                            fontSize: 12,
                            fontWeight: FontWeight.bold,
                            color: pointType == 1 ? Colors.white : Colors.black,
                          ),
                          SizedBox(
                            height: 4,
                          ),
                          Texts(
                            "$point",
                            fontSize: 12,
                            fontWeight: FontWeight.bold,
                            color: pointType == 1 ? Colors.white : Colors.black,
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
