import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/cmc_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/ComprehensiveMedicalCheckup/Dialog/confirm_cancel_order_dialog.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/HomeHealthCare/StepsWidget.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 'new_cmc_step_one_page.dart';
import 'new_cmc_step_three_page.dart';
import 'new_cmc_step_tow_page.dart';

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

  final CMCViewModel model;

  @override
  _NewCMCPageState createState() => _NewCMCPageState();
}

class _NewCMCPageState extends State<NewCMCPage> with TickerProviderStateMixin {
  PageController _controller;
  int _currentIndex = 1;

  double _latitude;
  double _longitude;

  CMCInsertPresOrderRequestModel cMCInsertPresOrderRequestModel;

  @override
  void initState() {
    super.initState();
    PatientERCMCInsertServicesList patientERCMCInsertServicesList;
    if (widget.model.cmcAllServicesList.length > 0) {
      patientERCMCInsertServicesList = new PatientERCMCInsertServicesList(
          price: widget.model.cmcAllServicesList[0].price,
          serviceID: widget.model.cmcAllServicesList[0].serviceID.toString(),
          selectedServiceName: widget.model.cmcAllServicesList[0].description,
          selectedServiceNameAR:
              widget.model.cmcAllServicesList[0].descriptionN,
          recordID: 1,
          totalPrice: widget.model.cmcAllServicesList[0].totalPrice,
          vAT: widget.model.cmcAllServicesList[0].vAT);
      cMCInsertPresOrderRequestModel = new CMCInsertPresOrderRequestModel(
          patientERCMCInsertServicesList: [patientERCMCInsertServicesList]);
    }

    cMCInsertPresOrderRequestModel = new CMCInsertPresOrderRequestModel(
        patientERCMCInsertServicesList: patientERCMCInsertServicesList != null
            ? [patientERCMCInsertServicesList]
            : []);

    _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();
  }

  changePageViewIndex(pageIndex) {
    _controller.jumpToPage(pageIndex);
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    void showConfirmMessage(
        CMCViewModel 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.updateCmcPresOrder(updatePresOrderRequestModel);
              if (model.state == ViewState.ErrorLocal) {
                Utils.showErrorToast(model.error);
              } else {
                AppToast.showSuccessToast(
                    message:
                        TranslationBase.of(context).processDoneSuccessfully);
                await model.getCmcAllPresOrders();
              }
            },
          ));
    }

    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Container(
            height: MediaQuery.of(context).size.height * 0.8,
            child: Column(
              children: [
                if (widget.model.cmcAllOrderDetail.length == 0)
                  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: changePageViewIndex,
                    ),
                  ),
                Expanded(
                  child: PageView(
                    physics: NeverScrollableScrollPhysics(),
                    controller: _controller,
                    onPageChanged: (index) {
                      setState(() {
                        _currentIndex = index;
                      });
                    },
                    scrollDirection: Axis.horizontal,
                    children: <Widget>[
                      widget.model.cmcAllOrderDetail.length != 0
                          ? FractionallySizedBox(
                              widthFactor: 0.9,
                              child: SingleChildScrollView(
                                child: Column(
                                  children: [
                                    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
                                                      .cmcAllOrderDetail[0].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
                                                          .cmcAllOrderDetail[0]
                                                          .descriptionN
                                                      : widget
                                                          .model
                                                          .cmcAllOrderDetail[0]
                                                          .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
                                                              .cmcAllOrderDetail[
                                                                  0]
                                                              .createdOn)),
                                                  fontSize: 22,
                                                ),
                                              ],
                                            ),
                                          ),
                                          Container(
                                            width: double.infinity,
                                            padding: EdgeInsets.only(
                                                left: 15, bottom: 15, top: 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
                                                          .cmcAllOrderDetail[0]
                                                          .description
                                                          .toString()
                                                      : widget
                                                          .model
                                                          .cmcAllOrderDetail[0]
                                                          .descriptionN
                                                          .toString(),
                                                  fontSize: 22,
                                                ),
                                              ],
                                            ),
                                          ),
                                          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
                                                            .cmcAllOrderDetail[0]);
                                                  },
                                                  color: Colors.red[800],
                                                  disabled: false,
                                                  textColor: Theme.of(context)
                                                      .backgroundColor),
                                            ),
                                          ),
                                          SizedBox(
                                            height: 22,
                                          ),
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      height: 22,
                                    ),
                                  ],
                                ),
                              ),
                            )
                          : NewCMCStepOnePage(
                              changePageViewIndex: changePageViewIndex,
                              cMCInsertPresOrderRequestModel:
                                  cMCInsertPresOrderRequestModel,
                              model: widget.model,
                            ),
                      NewCMCStepTowPage(
                        longitude: _longitude,
                        latitude: _latitude,
                        changePageViewIndex: changePageViewIndex,
                        cmcInsertPresOrderRequestModel:
                            cMCInsertPresOrderRequestModel,
                        model: widget.model,
                      ),
                      NewCMCStepThreePage(
                        changePageViewIndex: changePageViewIndex,
                        cmcInsertPresOrderRequestModel:
                            cMCInsertPresOrderRequestModel,
                        model: widget.model,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
