import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_allergy.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_history.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/objective/update_objective_page_vida_plus.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/plan/update_plan_page_vida_plus.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/update_subjective_page_vida_plus.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/app_bar/patient-profile-app-bar.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

import 'assessment/update_assessment_page.dart';

class UpdateSoapIndexVidaPlus extends StatefulWidget {
  final bool isUpdate;

  const UpdateSoapIndexVidaPlus({Key? key, this.isUpdate = false})
      : super(key: key);

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

class _UpdateSoapIndexVidaPlusState extends State<UpdateSoapIndexVidaPlus>
    with TickerProviderStateMixin {
  PageController? _controller;
  int _currentIndex = 0;
  List<MySelectedAllergy> myAllergiesList = [];
  List<MySelectedHistory> myHistoryList = [];

  changePageViewIndex(pageIndex, {isChangeState = true}) {
    if (pageIndex != _currentIndex && isChangeState) changeLoadingState(true);
    _controller!.jumpToPage(pageIndex);
    setState(() {
      _currentIndex = pageIndex;
    });
  }

  bool _isLoading = true;

  @override
  void initState() {
    _controller = new PageController();

    super.initState();
  }

  void changeLoadingState(isLoading) {
    setState(() {
      _isLoading = isLoading;
    });
  }

  void changeStateFun() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    return BaseView<SOAPViewModel>(
      builder: (_, model, w) => AppScaffold(
        isLoading: _isLoading,
        isShowAppBar: true,
        appBar: PatientProfileAppBar(patient),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                decoration: BoxDecoration(
                    boxShadow: <BoxShadow>[],
                    color: Theme.of(context).scaffoldBackgroundColor),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      color: Theme.of(context).scaffoldBackgroundColor,
                      height: MediaQuery.of(context).size.height * 0.73,
                      child: PageView(
                        physics: NeverScrollableScrollPhysics(),
                        controller: _controller,
                        onPageChanged: (index) {
                          setState(() {
                            _currentIndex = index;
                          });
                        },
                        scrollDirection: Axis.horizontal,
                        children: <Widget>[
                          UpdateSubjectivePageVidaPlus(
                              changePageViewIndex: changePageViewIndex,
                              currentIndex: _currentIndex,
                              patientInfo: patient,
                              changeLoadingState: changeLoadingState),
                          UpdateObjectivePageVidaPlus(
                              changePageViewIndex: changePageViewIndex,
                              currentIndex: _currentIndex,
                              patientInfo: patient,
                              changeLoadingState: changeLoadingState),
                          UpdateAssessmentPage(
                            changePageViewIndex: changePageViewIndex,
                            currentIndex: _currentIndex,
                            patientInfo: patient,
                            changeLoadingState: changeLoadingState,
                          ),
                          UpdatePlanPageVidaPlus(
                            changePageViewIndex: changePageViewIndex,
                            currentIndex: _currentIndex,
                            patientInfo: patient,
                            changeLoadingState: changeLoadingState,
                            sOAPViewModel: model,
                            changeStateFun: changeStateFun,
                          ),

                        ],
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
        bottomSheet: _isLoading
            ? Container(
                height: 0,
              )
            : Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(0.0),
                  ),
                  border: Border.all(color: HexColor('#707070'), width: 0),
                ),
                height: SizeConfig.heightMultiplier! *
                    (SizeConfig.isHeightVeryShort ? 12 : 10),
                width: double.infinity,
                child: Column(
                  children: [
                    SizedBox(
                      height: 10,
                    ),
                    Container(
                      child: FractionallySizedBox(
                          widthFactor: .9,
                          child: getBottomSheet(model, patient)),
                    ),
                    SizedBox(
                      height: 5,
                    ),
                  ],
                ),
              ),
      ),
    );
  }

  Widget getBottomSheet(SOAPViewModel model, PatiantInformtion patient) {
    switch (_currentIndex) {
      case 0:
        {
          return Center(
            child: AppButton(
              title: TranslationBase.of(context).next,
              fontWeight: FontWeight.w600,
              disabled: !model.isSubjectiveAssesmentCompleted(),
              color: Colors.red[700]!,
              onPressed: () async {
                changePageViewIndex(1);
              },
            ),
          );
        }
        break;
      case 1:
        {
          return Center(
            child: Row(
              children: [
                Expanded(
                  child: AppButton(
                    title: TranslationBase.of(context).previous,
                    color: Colors.grey[300]!,
                    // height: SizeConfig.heightMultiplier! *
                    //     (SizeConfig.isHeightVeryShort ? 8 : 6),
                    // padding: 10,
                    fontColor: Colors.black,
                    fontWeight: FontWeight.w600,
                    onPressed: () {
                      changePageViewIndex(0);
                    },
                  ),
                ),
                SizedBox(
                  width: 5,
                ),
                Expanded(
                  child: AppButton(
                    title: patient.admissionNo != null &&
                            patient.admissionNo!.isNotEmpty &&
                            !model.isAddExamInProgress
                        ? TranslationBase.of(context).finish
                        : TranslationBase.of(context).next,
                    fontWeight: FontWeight.w600,
                    color: Colors.red[700]!,
                    // height: SizeConfig.heightMultiplier! *
                    //     (SizeConfig.isHeightVeryShort ? 8 : 6),
                    // padding: 10,
                    disabled: model.state == ViewState.BusyLocal || !model.isPhysicalExaminationAdded(),
                    onPressed: () async {
                      changePageViewIndex(2);
                      // await model.nextOnObjectivePage(model);
                    },
                  ),
                ),
              ],
            ),
          );
        }
        break;
      case 2:
        {
          return Center(
            child: Row(
              children: [
                Expanded(
                  child: AppButton(
                    title: TranslationBase.of(context).previous,
                    color: Colors.grey[300]!,
                    fontColor: Colors.black,
                    fontWeight: FontWeight.w600,
                    disabled: model.state == ViewState.BusyLocal,
                    onPressed: () async {
                      changePageViewIndex(1);
                    },
                  ),
                ),
                SizedBox(
                  width: 5,
                ),
                Expanded(
                  child: AppButton(
                    title: TranslationBase.of(context).next,
                    fontWeight: FontWeight.w600,
                    color: Colors.red[700]!,
                    disabled: model.state == ViewState.BusyLocal || !model.isDiagnosticsAdded(),
                    onPressed: () async {
                      changePageViewIndex(3);
                    },
                  ),
                ),
              ],
            ),
          );
        }
        break;
      case 3:
        {
          return Center(
            child: Row(
              children: [
                Expanded(
                  child: AppButton(
                    title: TranslationBase.of(context).previous,
                    color: Colors.grey[300]!,
                    fontColor: Colors.black,
                    fontWeight: FontWeight.w600,
                    disabled: model.state == ViewState.BusyLocal,
                    onPressed: () async {
                      changePageViewIndex(2);
                    },
                  ),
                ),
                SizedBox(
                  width: 5,
                ),
                Expanded(
                  child: AppButton(
                    title: TranslationBase.of(context).submit,
                    fontWeight: FontWeight.w600,
                    color: AppGlobal.appGreenColor,
                    disabled: !model.isProgressAdded(),
                    onPressed: () async {
                      Navigator.pop(context);
                    },
                  ),
                ),
              ],
            ),
          );
        }
        break;
    }
    return SizedBox();
  }
}
