import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/helper/utils.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/app_text_form_field.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/providers/service_request_providers/reject_reason_provider.dart';

class RejectionReasonBottomSheet extends StatefulWidget {
  final Future<bool> Function(int reasonId, String comment) onRejectPressed;
  final VoidCallback? onRefreshed;

  const RejectionReasonBottomSheet({Key? key, this.onRefreshed, required this.onRejectPressed}) : super(key: key);

  @override
  State<RejectionReasonBottomSheet> createState() => _RejectionReasonBottomSheetState();
}

class _RejectionReasonBottomSheetState extends State<RejectionReasonBottomSheet> {
  late TextEditingController _commentController;
  late GlobalKey<FormState> _formKey;
  Lookup? selectedReason;

  @override
  void initState() {
    super.initState();
    _commentController = TextEditingController();
    _formKey = GlobalKey<FormState>();
  }

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

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Padding(
        padding: EdgeInsetsGeometry.only(
          bottom: MediaQuery.of(context).viewInsets.bottom + MediaQuery.of(context).padding.bottom,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            SingleItemDropDownMenu<Lookup, RejectReasonProvider>(
              context: context,
              title: context.translation.reason,
              backgroundColor: AppColor.fieldBgColor(context),
              showShadow: false,
              showAsBottomSheet: true,
              validator: (value) {
                if (value == null) return "Mandatory";
                return null;
              },
              onSelect: (value) {
                selectedReason = value;
              },
            ),
            12.height,
            AppTextFormField(
              backgroundColor: AppColor.fieldBgColor(context),
              labelText: context.translation.comments,
              controller: _commentController,
              textInputType: TextInputType.multiline,
              alignLabelWithHint: true,
              showSpeechToText: true,
              showShadow: false,
              style: TextStyle(fontWeight: FontWeight.w500, fontSize: context.isTablet() ? 16 : 12, color: context.isDark ? Colors.white : const Color(0xff3B3D4A)),
              labelStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: context.isTablet() ? 16 : 12, color: context.isDark ? Colors.white : const Color(0xff767676)),
              floatingLabelStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: context.isTablet() ? 16 : 12, color: context.isDark ? Colors.white : const Color(0xff3B3D4A)),
              validator: (value) {
                if ((value ?? "").isEmpty) return "Mandatory";
                return null;
              },
            ),
            16.height,
            AppFilledButton(
              label: context.translation.reject,
              buttonColor: AppColor.red30,
              onPressed: () async {
                if (_formKey.currentState!.validate()) {
                  _formKey.currentState!.save();
                  Utils.showLoading(context);
                  bool isSuccess = await widget.onRejectPressed(selectedReason!.id!, _commentController.text);
                  Utils.hideLoading(context);
                  if (isSuccess) {
                    Navigator.of(context).pop();
                    widget.onRefreshed?.call();
                  }
                }
              },
            ),
            16.height,
          ],
        ),
      ),
    );
  }
}
