import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/reports_monthly_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/reports/user_agreement_page.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/input/custom_switch.dart';
import 'package:diplomaticquarterapp/widgets/input/text_field.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MonthlyReportsPage extends StatefulWidget {
  @override
  _MonthlyReportsPageState createState() => _MonthlyReportsPageState();
}

class _MonthlyReportsPageState extends State<MonthlyReportsPage> {
  bool isAgree;
  bool isSummary;
  String email;
  final formKey = GlobalKey<FormState>();

  List<ImagesInfo> imagesInfo = List();

  @override
  void initState() {
    imagesInfo.add(ImagesInfo(
        imageEn:
        'https://hmgwebservices.com/Images/MobileApp/imges-info/monthly-reports/en/0.png',
        imageAr:
        'https://hmgwebservices.com/Images/MobileApp/imges-info/monthly-reports/ar/0.png'));
    imagesInfo.add(ImagesInfo(
        imageEn:
        'https://hmgwebservices.com/Images/MobileApp/imges-info/monthly-reports/en/1.png',
        imageAr:
        'https://hmgwebservices.com/Images/MobileApp/imges-info/monthly-reports/ar/1.png'));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<ReportsMonthlyViewModel>(builder: (_, model, w) {
      if (isSummary == null) {
        isSummary = model.receiveHealthSummaryReport;
      }
      if (isAgree == null) {
        isAgree = model.receiveHealthSummaryReport;
      }
      if (email == null) {
        email = model?.user?.emailAddress ?? "";
      }
      return AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).monthlyReports,
        imagesInfo: imagesInfo,
        description: TranslationBase.of(context).infoMonthReport,
        body: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(12),
            child: Form(
              key: formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    padding: EdgeInsets.all(9),
                    height: 55,
                    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(8)), shape: BoxShape.rectangle, border: Border.all(color: Colors.grey)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Texts(
                          TranslationBase.of(context).patientHealthSummaryReport,
                          bold: true,
                        ),
                        CustomSwitch(
                          value: isSummary,
                          activeColor: Colors.red,
                          inactiveColor: Colors.grey,
                          onChanged: () async {
                            setState(() {
                              isSummary = !isSummary;
                            });
                            if (!isSummary) {
                              GifLoaderDialogUtils.showMyDialog(context);
                              await model.updatePatientHealthSummaryReport(message: TranslationBase.of(context).updateSuccessfully, isSummary: isSummary);
                              model.receiveHealthSummaryReport = isSummary;
                              isAgree = isSummary;
                              model.user.emailAddress = "";
                              GifLoaderDialogUtils.hideDialog(context);
                            }
                          },
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  Container(
                    margin: EdgeInsets.all(8),
                    child: TextFields(
                      fillColor: Colors.red,
                      hintText: 'email@email.com',
                      fontSize: 20,
                      initialValue: email,
                      fontWeight: FontWeight.w600,
                      onChanged: (text) {
                        email = text;
                      },
                      validator: (value) {
                        if (value.isEmpty)
                          return TranslationBase.of(context).enterEmail;
                        else
                          return null;
                      },
                    ),
                  ),
                  Divider(
                    height: 10.4,
                    thickness: 1.0,
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  Container(
                    margin: EdgeInsets.all(8),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Expanded(
                          child: Texts(TranslationBase.of(context).toViewTheTermsAndConditions),
                        ),
                        InkWell(
                          onTap: () {
                            Navigator.push(
                              context,
                              FadePage(
                                page: UserAgreementContent(),
                              ),
                            );
                          },
                          child: Texts(
                            TranslationBase.of(context).clickHere,
                            color: Colors.blue,
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Checkbox(
                        value: isAgree,
                        onChanged: (value) {
                          setState(() {
                            isAgree = !isAgree;
                          });
                        },
                        activeColor: Colors.red,
                      ),
                      Texts(TranslationBase.of(context).iAgreeToTheTermsAndConditions),
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.all(8),
                    width: double.infinity,
                    child: SecondaryButton(
                      textColor: Colors.white,
                      label: TranslationBase.of(context).save,
                      disabled: (!isAgree || !isSummary),
                      onTap: () async {
                        final form = formKey.currentState;
                        if (form.validate()) {
                          GifLoaderDialogUtils.showMyDialog(context);
                          await model.updatePatientHealthSummaryReport(
                              message: TranslationBase.of(context).updateSuccessfully, isSummary: isSummary, isUpdateEmail: true, email: email.isNotEmpty ? email : model.user.emailAddress);
                          model.receiveHealthSummaryReport = isSummary;
                          model.user.emailAddress = email.isNotEmpty ? email : model.user.emailAddress;
                          GifLoaderDialogUtils.hideDialog(context);
                        }
                      },
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Texts(
                      TranslationBase.of(context).instructionAgree,
                      fontWeight: FontWeight.normal,
                    ),
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  Center(child: Image.asset('assets/images/report.jpg'))
                ],
              ),
            ),
          ),
        ),
      );
    });
  }
}
