import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/enum/filter_type.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/profile/intervention_medication_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.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 'package:hexcolor/hexcolor.dart';

import 'intervention_medication.dart';

class PharmacyInterventionScreen extends StatefulWidget {
  final OutPatientFilterType? outPatientFilterType;

  const PharmacyInterventionScreen({
    Key? key,
    this.outPatientFilterType,
  }) : super(key: key);

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

class _PharmacyInterventionScreenState extends State<PharmacyInterventionScreen> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    return BaseView<InterventionMedicationViewModel>(
        builder: (_, model, w) => AppScaffold(
            isShowAppBar: false,
            backgroundColor: Theme.of(context).scaffoldBackgroundColor,
            body: SingleChildScrollView(
              child: Container(
                height: MediaQuery.of(context).size.height * 1.0,
                child: Padding(
                  padding: EdgeInsets.all(0.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      BottomSheetTitle(
                        title: (OutPatientFilterType.Previous == widget.outPatientFilterType) ? "" : TranslationBase.of(context).pharmacyIntervention,
                      ),
                      SizedBox(
                        height: 10.0,
                      ),
                      Center(
                        child: FractionallySizedBox(
                          widthFactor: 0.9,
                          child: Column(
                            children: [
                              Container(
                                color: Colors.white,
                                child: InkWell(
                                  onTap: () => selectDate(
                                    context,
                                    model,
                                    firstDate: DateTime(DateTime.now().year - 20, DateTime.now().month, DateTime.now().day),
                                    lastDate: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day + 100),
                                    selectedDate: model.selectedFromDate != null ? model.selectedFromDate : DateTime.now(),
                                  ),
                                  child: TextField(
                                    decoration: textFieldSelectorDecoration(
                                        TranslationBase.of(context).fromDate, "${AppDateUtils.convertStringToDateFormat(model.selectedFromDate.toString(), "yyyy-MM-dd")}", true,
                                        suffixIcon: Icon(
                                          Icons.calendar_today,
                                          color: Colors.black,
                                        )),
                                    enabled: false,
                                  ),
                                ),
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              Container(
                                color: Colors.white,
                                child: InkWell(
                                  onTap: () => selectDate(
                                    context,
                                    model,
                                    isFromDate: false,
                                    firstDate: DateTime(DateTime.now().year - 20, DateTime.now().month, DateTime.now().day),
                                    lastDate: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day + 100),
                                    selectedDate: model.selectedToDate != null ? model.selectedToDate : DateTime.now().add(Duration(days: 1)),
                                  ),
                                  child: TextField(
                                    decoration: textFieldSelectorDecoration(
                                        TranslationBase.of(context).toDate, "${AppDateUtils.convertStringToDateFormat(model.selectedToDate.toString(), "yyyy-MM-dd")}", true,
                                        suffixIcon: Icon(
                                          Icons.calendar_today,
                                          color: Colors.black,
                                        )),
                                    enabled: false,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            bottomSheet: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(
                  Radius.circular(0.0),
                ),
                border: Border.all(color: HexColor('#707070'), width: 0),
              ),
              height: MediaQuery.of(context).size.height * 0.1,
              width: double.infinity,
              child: Column(
                children: [
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    child: FractionallySizedBox(
                      widthFactor: .80,
                      child: Center(
                        child: AppButton(
                          title: TranslationBase.of(context).search,
                          padding: 5,
                          color: AppGlobal.appGreenColor,
                          onPressed: () async {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => InterventionMedicationScreen(
                                        patient,
                                        previousModel: model,
                                      )),
                            );
                          },
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                ],
              ),
            )));
  }

  selectDate(BuildContext context, InterventionMedicationViewModel model, {bool isFromDate = true, DateTime? firstDate, lastDate, selectedDate}) async {
    Utils.hideKeyboard(context);
    final DateTime? picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: firstDate!,
      lastDate: lastDate,
      initialEntryMode: DatePickerEntryMode.calendar,
    );
    if (picked != null) {
      if (isFromDate) {
        setState(() {
          model.selectedFromDate = picked;
          var date = picked.add(Duration(days: 1));
          if (date.isBefore(lastDate)) {
            model.selectedToDate = date;
          } else
            model.selectedToDate = lastDate;
        });
      } else {
        setState(() {
          model.selectedToDate = picked;
        });
      }
    }
  }

  InputDecoration textFieldSelectorDecoration(String hintText, String selectedText, bool isDropDown, {Icon? suffixIcon}) {
    return InputDecoration(
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFEFEFEF), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      disabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFEFEFEF), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      hintText: selectedText != null ? selectedText : hintText,
      suffixIcon: isDropDown
          ? suffixIcon != null
              ? suffixIcon
              : Icon(
                  Icons.keyboard_arrow_down_sharp,
                  color: Color(0xff2E303A),
                )
          : null,
      hintStyle: TextStyle(
        fontSize: 13,
        color: Color(0xff2E303A),
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w600,
      ),
      labelText: selectedText != null ? '$hintText\n$selectedText' : null,
      labelStyle: TextStyle(
        fontSize: 13,
        color: Color(0xff2E303A),
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w600,
      ),
    );
  }
}
