import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hmg_patient_app/pages/AlHabibMedicalService/health_calculator/carbs/carbs.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/uitl/font_utils.dart';
import 'package:hmg_patient_app/widgets/text/app_texts_widget.dart';
import 'package:smooth_corner/smooth_corner.dart';

class GenericBottomSheet extends StatefulWidget {
  String? countryCode;
  String? initialPhoneNumber;
  final List<Widget> buttons;
  TextEditingController? textController;
  final bool isForEmail;
  Function(Country)? onCountryChange;
  final bool isEnableCountryDropdown;
  final bool isFromSavedLogin;
  Function(String?)? onChange;
  FocusNode myFocusNode;
  GenericBottomSheet(
      {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 = TextEditingController(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: () {
          // Only unfocus if the tap is not on the text field area
          FocusScope.of(context).unfocus();
        },
        child: Directionality(
          textDirection: Directionality.of(context),
          child: Container(
            padding: const EdgeInsets.all(24),
            decoration: BoxDecoration(
              color: Color(0xFFF8F8FA),
              borderRadius: const BorderRadius.vertical(top: Radius.circular(16)),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // Title
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Flexible(
                      child: AppText(
                          widget.isFromSavedLogin
                              ? TranslationBase.of(context).receiveOtpToast
                              : widget.isForEmail
                                  ? TranslationBase.of(context).enterEmail
                                  : TranslationBase.of(context).enterPhoneNumber,
                          fontSize: 28,
                          letterSpacing: -1,
                          height: 1.33,
                          color: Color(0xFF2E3039),
                          fontWeight: FontWeight.w600),
                    ),
                    InkWell(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: Padding(
                          padding: const EdgeInsets.only(top: 10),
                          child: SvgPicture.asset("assets/images/svg/cross-circle.svg", width: 24, height: 24),
                        )),
                  ],
                ),
                const SizedBox(height: 8),
                // Subtitle
                AppText(
                    widget.isFromSavedLogin
                        ? TranslationBase.of(context).pleaseChooseOption
                        : widget.isForEmail
                            ? TranslationBase.of(context).enterEmailDesc
                            : TranslationBase.of(context).enterPhoneDesc,
                    fontSize: 16,
                    height: 1.33,
                    color: Color(0xFF2E3039),
                    fontWeight: FontWeight.w500),

                if (widget.isFromSavedLogin)
                  ...[]
                else ...[
                  widget.textController != null
                      ? Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            Directionality(
                              textDirection: TextDirection.ltr,
                              child: newInputWidget(
                                widget.isForEmail ? TranslationBase.of(context).email : TranslationBase.of(context).phoneNumber,
                                widget.isForEmail ? "demo@gmail.com" : "5xxxxxxxx",
                                widget.textController!,
                                // focusNode: widget.focusNode!,
                                padding: EdgeInsets.only(top: 8, bottom: 8, left: 8, right: 8),
                                keyboardType: widget.isForEmail ? TextInputType.emailAddress : TextInputType.number,
                                onChange: (value) {
                                  widget.textController!.text = value!;
                                  if (widget.onChange != null) {
                                    widget.onChange!(value);
                                  }
                                },
                                isEnable: true,
                                focusNode: widget.myFocusNode,
                                isReadOnly: widget.isFromSavedLogin,
                                prefix: widget.isForEmail ? null : widget.countryCode,
                                hasSelection: false,
                                isBorderAllowed: false,
                                isAllowLeadingIcon: true,
                                countryList: Country.values,
                                isCountryDropDown: widget.isEnableCountryDropdown,
                                onCountryChange: (Country country) {
                                  widget.onCountryChange!(country);
                                },
                                leadingIcon: widget.isForEmail ? "assets/images/svg/email.svg" : "assets/images/svg/smart-phone.svg",
                              ),
                            ),
                          ],
                        )
                      : SizedBox(),
                ],

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

class CustomButton extends StatelessWidget {
  final String text;
  String? icon;
  final VoidCallback onPressed;
  final Color backgroundColor;
  final Color borderColor;
  final Color textColor;
  final double borderRadius;
  final double borderWidth;
  final EdgeInsetsGeometry padding;
  final double fontSize;
  final String? fontFamily;
  final FontWeight fontWeight;
  final bool isDisabled;
  final Color iconColor;
  CustomButton({
    Key? key,
    required this.text,
    required this.onPressed,
    this.backgroundColor = const Color(0xFFED1C2B),
    this.borderColor = const Color(0xFFED1C2B),
    this.textColor = Colors.white,
    this.borderRadius = 12,
    this.borderWidth = 2,
    this.padding = const EdgeInsets.fromLTRB(8, 10, 8, 10),
    this.fontSize = 16,
    this.fontFamily,
    this.fontWeight = FontWeight.w500,
    this.isDisabled = false,
    this.icon,
    this.iconColor = Colors.white,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: isDisabled ? null : onPressed,
        child: Container(
          height: 56,
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
            color: isDisabled ? Colors.transparent : backgroundColor,
            borderRadius: borderRadius,
            side: BorderSide(width: borderWidth, color: isDisabled?  borderColor.withOpacity(0.5) : borderColor,)
          ),
          child: Padding(
            padding: padding,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (icon != null)
                  Padding(
                    padding: const EdgeInsets.only(right: 8.0),
                    child: SvgPicture.asset(
                      icon!,
                      colorFilter: ColorFilter.mode(
                        isDisabled ? iconColor.withOpacity(0.5) : iconColor,
                        BlendMode.srcIn,
                      ),
                      width: 24,
                      height: 24,
                    ),
                  ),
                Text(
                  text,
                  style: context.dynamicTextStyle(
                    fontSize: fontSize,
                    color: isDisabled ? textColor.withOpacity(0.5) : textColor,
                    fontWeight: fontWeight,
                  ),
                ),
              ],
            ),
          ),
        )

        //     .toSmoothContainer(
        //   smoothness: 1,
        //   side: BorderSide(width: borderWidth, color: backgroundColor),
        //   borderRadius: BorderRadius.circular(borderRadius * 1.2),
        //   foregroundDecoration: BoxDecoration(
        //     color: isDisabled ? backgroundColor.withOpacity(0.5) : Colors.transparent,
        //     borderRadius: BorderRadius.circular(borderRadius),
        //   ),
        // ),
        );
  }
}
