import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/core/toast/toast_service.dart';
import 'package:kasy_kit/features/feedbacks/providers/feedback_page_notifier.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

Future<bool?> showAddFeatureSheet(BuildContext context) {
  return showKasyBlurBottomSheet<bool>(
    context: context,
    builder: (_) => const _AddFeatureSheet(),
  );
}

class _AddFeatureSheet extends ConsumerStatefulWidget {
  const _AddFeatureSheet();

  @override
  ConsumerState<_AddFeatureSheet> createState() => _AddFeatureSheetState();
}

class _AddFeatureSheetState extends ConsumerState<_AddFeatureSheet> {
  String title = '';
  String description = '';
  bool locked = false;

  bool get _titleFilled => title.trim().isNotEmpty;
  bool get _descriptionValid => description.length >= 40;
  bool get canSubmit => _titleFilled && _descriptionValid && !locked;

  void _onSubmitAttempt() {
    final tr = Translations.of(context).feature_requests.add_feature;
    if (!_titleFilled || description.trim().isEmpty) {
      ref.read(toastProvider).error(title: tr.error_title, text: tr.error_required);
      return;
    }
    if (!_descriptionValid) {
      ref.read(toastProvider).error(title: tr.error_title, text: tr.error_too_short);
      return;
    }
  }

  void _submit() {
    if (locked) return;
    final tr = Translations.of(context).feature_requests.add_feature;
    FocusScope.of(context).unfocus();
    setState(() => locked = true);
    ref
        .read(feedbackPageProvider.notifier)
        .createFeatureSuggestion(title: title, description: description)
        .then(
          (_) {
            if (!mounted) return;
            context.pop(true);
          },
          onError: (_) {
            if (!mounted) return;
            setState(() => locked = false);
            ref.read(toastProvider).error(title: tr.error_title, text: tr.error_sending);
          },
        );
  }

  @override
  Widget build(BuildContext context) {
    final tr = Translations.of(context).feature_requests.add_feature;

    final submitButton = KasyButton(
      label: tr.save_button,
      expand: true,
      onPressed: canSubmit ? _submit : null,
    );

    return KasyBottomSheet(
      title: tr.title,
      message: tr.description,
      addKeyboardInset: true,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          KasyTextField(
            label: tr.title_label,
            hint: tr.title_hint,
            showRequiredIndicator: true,
            onChanged: (v) => setState(() => title = v),
            textInputAction: TextInputAction.next,
            maxLength: 40,
          ),
          const SizedBox(height: KasySpacing.md),
          KasyTextField(
            label: tr.description_label,
            hint: tr.description_hint,
            showRequiredIndicator: true,
            minLines: 5,
            maxLines: 5,
            onChanged: (v) => setState(() => description = v),
            textInputAction: TextInputAction.done,
            maxLength: 1000,
          ),
        ],
      ),
      actions: [
        if (canSubmit)
          submitButton
        else
          GestureDetector(onTap: _onSubmitAttempt, child: submitButton),
      ],
    );
  }
}
