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/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/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';

class ExceptionBottomSheet extends StatefulWidget {
  final String? title;
  final String message;
  final bool showOKButton;
  final bool showCancel;
  final Function() onOkPressed;
  final Function()? onCancelPressed;

  const ExceptionBottomSheet({
    super.key,
    this.title,
    required this.message,
    this.showOKButton = true,
    this.showCancel = false,
    required this.onOkPressed,
    this.onCancelPressed,
  });

  @override
  State<ExceptionBottomSheet> createState() => _ExceptionBottomSheetState();
}

class _ExceptionBottomSheetState extends State<ExceptionBottomSheet> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      bottom: Platform.isIOS ? false : true, // Adjust for iOS to avoid bottom padding
      child: GestureDetector(
        onTap: () {
          FocusScope.of(context).unfocus(); // Dismiss the keyboard when tapping outside
        },
        child: Builder(builder: (context) {
          return Directionality(
            textDirection: Directionality.of(context),
            child: Container(
              padding: EdgeInsets.all(24.h),
              decoration: BoxDecoration(
                color: AppColors.whiteColor,
                borderRadius: const BorderRadius.vertical(top: Radius.circular(16)),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      (widget.title ?? LocaleKeys.notice.tr()).toText28(),
                      InkWell(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: Utils.buildSvgWithAssets(
                          icon: AppAssets.cross_circle,
                          height: 24.h,
                        ),
                      )
                    ],
                  ),
                  SizedBox(height: 10.h),
                  (widget.message).toText16(isBold: false, color: AppColors.textColor),
                  SizedBox(height: 10.h),
                  SizedBox(height: 24.h),
                  if (widget.showOKButton && widget.showCancel)
                    Row(
                      children: [
                        Expanded(
                          child: CustomButton(
                            height: 56.h,
                            text: LocaleKeys.cancel.tr(),
                            onPressed: widget.onCancelPressed != null
                                ? widget.onCancelPressed!
                                : () {
                                    Navigator.of(context).pop();
                                  },
                            backgroundColor: AppColors.secondaryLightRedColor,
                            borderColor: AppColors.secondaryLightRedColor,
                            textColor: AppColors.primaryRedColor,
                            icon: AppAssets.cancel,
                            iconColor: AppColors.primaryRedColor,
                          ),
                        ),
                        SizedBox(width: 5.h),
                        Expanded(
                          child: CustomButton(
                            height: 56.h,
                            text: widget.showCancel ? LocaleKeys.confirm.tr() : LocaleKeys.ok.tr(),
                            onPressed: widget.onOkPressed,
                            backgroundColor: AppColors.bgGreenColor,
                            borderColor: AppColors.bgGreenColor,
                            textColor: Colors.white,
                            icon: AppAssets.confirm,
                          ),
                        ),
                      ],
                    ),
                  if (widget.showOKButton && !widget.showCancel)
                    Padding(
                      padding: EdgeInsets.only(bottom: 10.h),
                      child: CustomButton(
                        height: 56.h,
                        text: LocaleKeys.ok.tr(),
                        onPressed: widget.onOkPressed,
                        backgroundColor: AppColors.primaryRedColor,
                        borderColor: AppColors.primaryRedBorderColor,
                        textColor: Colors.white,
                        icon: AppAssets.confirm,
                      ),
                    ),
                ],
              ),
            ),
          );
        }),
      ),
    );
  }
}
