import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/patient_er_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/home_health_care_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/HomeHealthCare/Dialog/confirm_cancel_order_dialog.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/HomeHealthCare/NewHomeHealthCare/new_Home_health_care_step_three_page.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/HomeHealthCare/NewHomeHealthCare/new_Home_health_care_step_tow_page.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:provider/provider.dart';

import '../StepsWidget.dart';
import 'new_Home_health_care_step_one_page.dart';

class NewHomeHealthCarePage extends StatefulWidget {
  NewHomeHealthCarePage({this.model});

  final HomeHealthCareViewModel model;
  @override
  _NewHomeHealthCarePageState createState() => _NewHomeHealthCarePageState();
}

class _NewHomeHealthCarePageState extends State<NewHomeHealthCarePage>
    with TickerProviderStateMixin {
  PageController _controller;
  int _currentIndex = 1;
  double _latitude;
  double _longitude;

  PatientERInsertPresOrderRequestModel patientERInsertPresOrderRequestModel =
      new PatientERInsertPresOrderRequestModel();

  @override
  void initState() {
    super.initState();
    _controller = new PageController();
    _getCurrentLocation();
  }

  _getCurrentLocation() async {
    await getLastKnownPosition().then((value) {
      _latitude = value.latitude;
      _longitude = value.longitude;
    }).catchError((e) {
      _longitude = 0;
      _latitude = 0;
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  _changeCurrentTab(int tab) {
    setState(() {
      _currentIndex = tab;
    });
    _controller.animateToPage(tab,
        duration: Duration(milliseconds: 800), curve: Curves.easeOutQuart);
  }

  @override
  Widget build(BuildContext context) {
    void showConfirmMessage(HomeHealthCareViewModel model,
        GetOrderDetailByOrderIDResponseModel order) {
      showDialog(
          context: context,
          child: ConfirmCancelOrderDialog(
            model: model,
            onTap: () async {
              UpdatePresOrderRequestModel updatePresOrderRequestModel =
                  UpdatePresOrderRequestModel(
                      presOrderID: order.presOrderID,
                      rejectionReason: "",
                      presOrderStatus: 4,
                      editedBy: 3);
              await model.updateHHCPresOrder(updatePresOrderRequestModel);
              if (model.state == ViewState.ErrorLocal) {
                Utils.showErrorToast(model.error);
              } else {
                AppToast.showSuccessToast(
                    message:
                        TranslationBase.of(context).processDoneSuccessfully);
                await model.getHHCAllPresOrders();
                // await model.getHHCAllServices();
              }
            },
          ));
    }

    ProjectViewModel projectViewModel = Provider.of(context);

    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Container(
            height: MediaQuery.of(context).size.height * 0.8,
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(
                      left: MediaQuery.of(context).size.width * 0.05,
                      right: MediaQuery.of(context).size.width * 0.05),
                  child: StepsWidget(
                    index: _currentIndex,
                    changeCurrentTab: _changeCurrentTab,
                  ),
                ),
                Expanded(
                  child: PageView(
                    physics: NeverScrollableScrollPhysics(),
                    controller: _controller,
                    onPageChanged: (index) {
                      setState(() {
                        _currentIndex = index;
                      });
                    },
                    scrollDirection: Axis.horizontal,
                    children: <Widget>[
                      widget.model.pendingOrder != null
                          ? FractionallySizedBox(
                              widthFactor: 0.9,
                              child: SingleChildScrollView(
                                child: Container(
                                  width: double.infinity,
                                  margin: EdgeInsets.only(top: 15),
                                  decoration: BoxDecoration(
                                      border: Border.all(
                                          color: Colors.grey, width: 1),
                                      borderRadius: BorderRadius.circular(12),
                                      color: Theme.of(context).primaryColor),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(
                                        height: 12,
                                      ),
                                      Container(
                                        width: double.infinity,
                                        padding: EdgeInsets.only(
                                            left: 15,
                                            bottom: 15,
                                            top: 15,
                                            right: 15),
                                        decoration: BoxDecoration(
                                            border: Border(
                                              bottom: BorderSide(
                                                color: Colors.grey,
                                                width: 1.0,
                                              ),
                                            ),
                                            // borderRadius: BorderRadius.circular(12),
                                            color:
                                                Theme.of(context).primaryColor),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Texts(
                                              TranslationBase.of(context)
                                                  .requestID,
                                              bold: false,
                                              fontSize: 13,
                                            ),
                                            SizedBox(
                                              height: 4,
                                            ),
                                            Texts(
                                              widget.model.pendingOrder.iD
                                                  .toString(),
                                              fontSize: 22,
                                            ),
                                          ],
                                        ),
                                      ),
                                      Container(
                                        width: double.infinity,
                                        padding: EdgeInsets.only(
                                            left: 15,
                                            bottom: 15,
                                            top: 15,
                                            right: 15),
                                        decoration: BoxDecoration(
                                            border: Border(
                                              bottom: BorderSide(
                                                color: Colors.grey,
                                                width: 1.0,
                                              ),
                                            ),
                                            // borderRadius: BorderRadius.circular(12),
                                            color:
                                                Theme.of(context).primaryColor),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Texts(
                                              TranslationBase.of(context)
                                                  .OrderStatus,
                                              bold: false,
                                              fontSize: 13,
                                            ),
                                            SizedBox(
                                              height: 4,
                                            ),
                                            Texts(
                                              projectViewModel.isArabic
                                                  ? widget.model.pendingOrder
                                                      .descriptionN
                                                  : widget.model.pendingOrder
                                                      .description,
                                              fontSize: 22,
                                            ),
                                          ],
                                        ),
                                      ),
                                      Container(
                                        width: double.infinity,
                                        padding: EdgeInsets.only(
                                            left: 15,
                                            bottom: 15,
                                            top: 15,
                                            right: 15),
                                        decoration: BoxDecoration(
                                            border: Border(
                                              bottom: BorderSide(
                                                color: Colors.grey,
                                                width: 1.0,
                                              ),
                                            ),
                                            // borderRadius: BorderRadius.circular(12),
                                            color:
                                                Theme.of(context).primaryColor),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Texts(
                                              TranslationBase.of(context)
                                                  .pickupDate,
                                              bold: false,
                                              fontSize: 13,
                                            ),
                                            SizedBox(
                                              height: 4,
                                            ),
                                            Texts(
                                              DateUtil
                                                  .getDayMonthYearDateFormatted(
                                                      DateUtil
                                                          .convertStringToDate(
                                                              widget
                                                                  .model
                                                                  .pendingOrder
                                                                  .createdOn)),
                                              fontSize: 22,
                                            ),
                                          ],
                                        ),
                                      ),
                                      ...List.generate(
                                        widget.model.hhcAllOrderDetail.length,
                                        (index) => Container(
                                          width: double.infinity,
                                          padding: EdgeInsets.only(
                                              left: 15,
                                              bottom: 15,
                                              top: 15,
                                              right: 15),
                                          decoration: BoxDecoration(
                                              border: Border(
                                                bottom: BorderSide(
                                                  color: Colors.grey,
                                                  width: 1.0,
                                                ),
                                              ),
                                              // borderRadius: BorderRadius.circular(12),
                                              color: Theme.of(context)
                                                  .primaryColor),
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Texts(
                                                TranslationBase.of(context)
                                                    .serviceName,
                                                bold: false,
                                                fontSize: 13,
                                              ),
                                              SizedBox(
                                                height: 4,
                                              ),
                                              Texts(
                                                projectViewModel.isArabic
                                                    ? widget
                                                        .model
                                                        .hhcAllOrderDetail[
                                                            index]
                                                        .descriptionN
                                                    : widget
                                                        .model
                                                        .hhcAllOrderDetail[
                                                            index]
                                                        .description,
                                                fontSize: 22,
                                                bold: true,
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        height: 12,
                                      ),
                                      Center(
                                        child: Container(
                                          width: MediaQuery.of(context)
                                                  .size
                                                  .width *
                                              0.85,
                                          child: SecondaryButton(
                                              label: TranslationBase.of(context)
                                                  .cancel
                                                  .toUpperCase(),
                                              onTap: () {
                                                showConfirmMessage(
                                                    widget.model,
                                                    widget.model
                                                        .hhcAllOrderDetail[0]);
                                              },
                                              color: Colors.red[800],
                                              disabled: false,
                                              textColor: Theme.of(context)
                                                  .backgroundColor),
                                        ),
                                      ),
                                      SizedBox(
                                        height: 12,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            )
                          : NewHomeHealthCareStepOnePage(
                              changePageViewIndex: _changeCurrentTab,
                              patientERInsertPresOrderRequestModel:
                                  patientERInsertPresOrderRequestModel,
                              model: widget.model,
                            ),
                      NewHomeHealthCareStepTowPage(
                        latitude: _latitude,
                        longitude: _longitude,
                        changePageViewIndex: _changeCurrentTab,
                        patientERInsertPresOrderRequestModel:
                            patientERInsertPresOrderRequestModel,
                        model: widget.model,
                      ),
                      NewHomeHealthCareStepThreePage(
                        changePageViewIndex: _changeCurrentTab,
                        patientERInsertPresOrderRequestModel:
                            patientERInsertPresOrderRequestModel,
                        model: widget.model,
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
