import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';

class GenericBottomSheet extends StatefulWidget {
  String? countryCode;
  String? initialPhoneNumber;
  final List<Widget> buttons;
  TextEditingController? textController;
  final bool isForEmail;
  Function(CountryEnum)? onCountryChange;
  final bool isEnableCountryDropdown;
  final bool isFromSavedLogin;
  Function(String?)? onChange;

  // FocusNode myFocusNode;

  GenericBottomSheet({
    super.key,
    this.countryCode = "",
    this.initialPhoneNumber = "",
    required this.buttons,
    this.textController,
    this.isForEmail = false,
    this.onCountryChange,
    this.isEnableCountryDropdown = false,
    this.isFromSavedLogin = false,
    this.onChange,
    // required this.myFocusNode
  });

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

class _GenericBottomSheetState extends State<GenericBottomSheet> {
  @override
  void initState() {
    super.initState();

    if (!widget.isForEmail && widget.textController != null) {
      widget.textController!.text = widget.initialPhoneNumber ?? "";
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      bottom: Platform.isIOS ? false : true,
      child: GestureDetector(
        onTap: () {
          FocusScope.of(context).unfocus();
        },
        child: Directionality(
          textDirection: Directionality.of(context),
          child: Container(
            padding: EdgeInsets.all(24.h),
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.bgScaffoldColor, borderRadius: 16),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // Title
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Flexible(
                        child: widget.isFromSavedLogin
                            ? LocaleKeys.receiveOtpToast.tr().toText24()
                            : widget.isForEmail
                                ? LocaleKeys.enterEmail.tr().toText24()
                                : LocaleKeys.enterPhoneNumber.tr().toText24()),
                    InkWell(
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                      child: Padding(
                        padding: EdgeInsets.only(top: 10.h),
                        child: Utils.buildSvgWithAssets(icon: AppAssets.cross_circle),
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 8.h),
                // Subtitle
                widget.isFromSavedLogin
                    ? LocaleKeys.pleaseChooseOption.tr().toText16()
                    : widget.isForEmail
                        ? LocaleKeys.enterEmailDesc.tr().toText16()
                        : LocaleKeys.enterPhoneDesc.tr().toText16(),

                if (widget.isFromSavedLogin)
                  ...[]
                else ...[
                  widget.textController != null
                      ? TextInputWidget(
                          labelText: widget.isForEmail ? LocaleKeys.email : LocaleKeys.phoneNumber,
                          hintText: widget.isForEmail ? "demo@gmail.com" : "5xxxxxxxx",
                          controller: widget.textController!,
                          padding: EdgeInsets.all(8.h),
                          keyboardType: widget.isForEmail ? TextInputType.emailAddress : TextInputType.number,
                          onChange: (value) {
                            if (widget.onChange != null) {
                              widget.onChange!(value);
                            }
                          },
                          onCountryChange: (value) {
                            if (widget.onCountryChange != null) {
                              widget.onCountryChange!(value);
                            }
                          },
                          isEnable: true,
                          isReadOnly: widget.isFromSavedLogin,
                          prefix: widget.isForEmail ? null : widget.countryCode,
                          isBorderAllowed: false,
                          isAllowLeadingIcon: true,
                          isCountryDropDown: widget.isEnableCountryDropdown,
                          leadingIcon: widget.isForEmail ? AppAssets.email : AppAssets.smart_phone,
                        )
                      : SizedBox(),
                ],

                SizedBox(height: 24.h),
                ...widget.buttons,
              ],
            ),
          ),
        ),
      ),
    );
  }
}
