// import 'package:customer_service/src/domain/send_email_use_case.dart';
// import 'package:customer_service/src/presentation/providers/send_email_use_case_provider.dart';
import 'package:customer_service/src/presentation/state/contact_view_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:sf_localizations/sf_localizations.dart';
import 'package:url_launcher/url_launcher.dart';

final contactViewModelProvider =
NotifierProvider.autoDispose<ContactViewModel, ContactViewState>(
  ContactViewModel.new,
);

class ContactViewModel extends Notifier<ContactViewState> {
  //late final SendEmailUseCase _sendEmailUseCase;

  late final TextEditingController nameController;
  late final TextEditingController emailController;
  late final TextEditingController subjectController;
  late final TextEditingController bodyController;

  static final RegExp _emailRegex = RegExp(
    r'^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$',
    caseSensitive: false,
  );

  @override
  ContactViewState build() {
    //_sendEmailUseCase = ref.read(sendEmailUseCaseProvider);

    nameController = TextEditingController();
    emailController = TextEditingController();
    subjectController = TextEditingController();
    bodyController = TextEditingController();

    nameController.addListener(_onNameChanged);
    emailController.addListener(_onEmailChanged);
    subjectController.addListener(_onSubjectChanged);
    bodyController.addListener(_onBodyChanged);

    ref.onDispose(disposeControllers);

    return const ContactViewState();
  }

  void setCountry(String value) {
    if (value == state.country) return;

    state = state.copyWith(country: value, errorMessage: '');
  }

  void setChannel(String value) {
    if (value == state.channel) return;

    state = state.copyWith(channel: value, errorMessage: '');
  }

  void _onNameChanged() {
    final text = nameController.text;
    if (text == state.name) return;

    state = state.copyWith(name: text, errorMessage: '');
  }

  void _onEmailChanged() {
    final text = emailController.text;
    if (text == state.email) return;

    state = state.copyWith(email: text, errorMessage: '');
    state = state.copyWith(emailError: _emailErrorFor(text));
  }

  bool _isValidEmail(String email) => _emailRegex.hasMatch(email);

  String _emailErrorFor(String value) {
    final email = value.trim();
    if (email.isEmpty) return I18n.errorEmailRequired;
    if (!_isValidEmail(email)) return I18n.errorEmailInvalid;
    return '';
  }

  void _onSubjectChanged() {
    final text = subjectController.text;
    if (text == state.subject) return;

    state = state.copyWith(subject: text, errorMessage: '');
  }

  void _onBodyChanged() {
    final text = bodyController.text;
    if (text == state.message) return;

    state = state.copyWith(message: text, errorMessage: '');
  }

  void sendEmail() async {
    final receiver = 'info@savefamilygps.com';

    final name = state.name;
    final country = state.country;
    final channel = state.channel;
    final sender = state.email;
    final subject = state.subject;
    final message = state.message;

    if (sender.isEmpty) {
      state = state.copyWith(errorMessage: I18n.errorEmailRequired);
      return;
    }
    if (!_isValidEmail(sender)) {
      state = state.copyWith(errorMessage: I18n.errorEmailInvalid);
      return;
    }

    final body =
        'country:$country\n'
        'Purchase channel:$channel\n'
        'name:$name\n'
        'email:$sender\n'
        '$message';

    final Uri url = Uri.parse('mailto:$receiver?from=$sender&subject=$subject&body=$body');
    if (!await launchUrl(url)) {
      throw Exception('Could not launch $url');
    }
  }

  void disposeControllers() {
    nameController.removeListener(_onNameChanged);
    emailController.removeListener(_onEmailChanged);
    subjectController.removeListener(_onSubjectChanged);
    bodyController.removeListener(_onBodyChanged);

    nameController.dispose();
    emailController.dispose();
    subjectController.dispose();
    bodyController.dispose();
  }
}