import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/patient-ucaf-viewmodel.dart';
import 'package:doctor_app_flutter/routes.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/UCAF/page_stepper_widget.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_service_title.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/material.dart';

import 'ucaf_detail_screen.dart';
import 'ucaf_input_screen.dart';

class UCAFPagerScreen extends StatefulWidget {
  const UCAFPagerScreen({Key? key}) : super(key: key);

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

class _UCAFPagerScreenState extends State<UCAFPagerScreen> with TickerProviderStateMixin {
  PageController? _controller;
  int _currentIndex = 0;
  bool _isLoading = true;

  PatiantInformtion? patient;
  String? patientType;
  String? arrivalType;

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

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

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

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    patient = routeArgs['patient'];
    patientType = routeArgs['patientType'];
    arrivalType = routeArgs['arrivalType'];

    final screenSize = MediaQuery.of(context).size;

    return BaseView<UcafViewModel>(
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        isLoading: _isLoading,
        appBar: PatientProfileAppBar(patient!),
        appBarTitle: TranslationBase.of(context).ucaf,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    ServiceTitle(
                      title: "${TranslationBase.of(context).patient}",
                      subTitle: "${TranslationBase.of(context).ucaf}",
                    ),
                    PageStepperWidget(
                      stepsCount: 2,
                      currentStepIndex: _currentIndex + 1,
                      screenSize: screenSize,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Expanded(
                      child: Container(
                        color: Theme.of(context).scaffoldBackgroundColor,
                        child: PageView(
                            physics: NeverScrollableScrollPhysics(),
                            controller: _controller,
                            onPageChanged: (index) {
                              setState(() {
                                _currentIndex = index;
                              });
                            },
                            scrollDirection: Axis.horizontal,
                            children: <Widget>[
                              UCAFInputScreen(
                                patient!,
                                changeLoadingState: changeLoadingState,
                              ),
                              UcafDetailScreen(
                                patient!,
                                model,
                                changeLoadingState: changeLoadingState,
                              ),
                            ]),
                      ),
                    ),
                    _isLoading
                        ? Container(
                      height: 0,
                    )
                        : UcafButtons(model),
                  ],
                ),
              ),
            ),
          ],
        ),
        // bottomSheet:
      ),
    );
  }

  /// TODO Elham* Move to real widget
  Widget UcafButtons(UcafViewModel model) {
    switch (_currentIndex) {
      case 0:
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          child: AppButton(
            title: TranslationBase.of(context).next,
            color: Color(0xFFD02127),
            onPressed: () {
              changePageViewIndex(1);
            },
          ),
        );
      case 1:
        return Container(
          margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
          child: Row(
            children: [
              Expanded(
                child: Container(
                  child: AppButton(
                    title: TranslationBase.of(context).cancel,
                    hasBorder: true,
                    vPadding: 8,
                    hPadding: 8,
                    borderColor: Colors.white,
                    color: Colors.white,
                    fontColor: Color(0xFFB8382B),
                    onPressed: () {
                      Navigator.of(context).popUntil((route) {
                        return route.settings.name == PATIENTS_PROFILE;
                      });
                    },
                  ),
                ),
              ),
              SizedBox(
                width: 8,
              ),
              Expanded(
                child: Container(
                  child: AppButton(
                    title: TranslationBase.of(context).save,
                    hasBorder: true,
                    vPadding: 8,
                    hPadding: 8,
                    borderColor: Color(0xFFB8382B),
                    color: AppGlobal.appRedColor,
                    fontColor: Colors.white,
                    onPressed: () {
                      model.saveUCAFOnTap!();
                    },
                  ),
                ),
              ),
            ],
          ),
        );
      default:
        return Container();
    }
  }
}
