import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:functions/src/features/remote_connection/domain/entities/picture_entity.dart';
import 'package:functions/src/features/remote_connection/domain/get_pictures_use_case.dart';
import 'package:functions/src/features/remote_connection/domain/take_picture_use_case.dart';
import 'package:functions/src/features/remote_connection/presentation/providers/get_pictures_use_case_provider.dart';
import 'package:functions/src/features/remote_connection/presentation/providers/take_picture_use_case_provider.dart';
import 'package:functions/src/features/remote_connection/presentation/state/remote_connection_view_state.dart';
// import 'package:legacy_shared/src/providers/logged_user_provider.dart';
// import 'package:legacy_shared/src/data/models/entities/user_entity.dart';
// import 'package:sf_localizations/sf_localizations.dart';

final remoteConnectionViewModelProvider =
NotifierProvider.autoDispose<RemoteConnectionViewModel, RemoteConnectionViewState>(
  RemoteConnectionViewModel.new,
);

class RemoteConnectionViewModel extends Notifier<RemoteConnectionViewState> {
  late final GetPicturesUseCase _getPicturesUseCase;
  late final TakePictureUseCase _takePictureUseCase;

  late final TextEditingController phoneController;

  // late final UserEntity loggedUser;

  static final RegExp _phoneRegex = RegExp(r'^\+?\d{6,15}$');

  @override
  RemoteConnectionViewState build() {
    _getPicturesUseCase = ref.read(getPicturesUseCaseProvider);
    _takePictureUseCase = ref.read(takePictureUseCaseProvider);

    // loggedUser = ref.read(loggedUserProvider);

    phoneController = TextEditingController();

    phoneController.addListener(_onPhoneChanged);

    _getPicturesUseCase.getPictures(userId: '').then(setImages);

    ref.onDispose(disposeControllers);

    return const RemoteConnectionViewState();
  }

  void setImages(List<PictureEntity> pictures) {
    state = state.copyWith(
      pictures: pictures
    );
  }

  void _onPhoneChanged() {
    final text = phoneController.text;
    if (text == state.phone) return;

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

  Future<void> takePicture() async {
    try {
      state = state.copyWith(isTakingPicture: true);

      await _takePictureUseCase.takePicture(userId: '')
        .then((picture) {
          List<PictureEntity> pictures = state.pictures;
          //pictures.add(picture);
          state = state.copyWith(
            isTakingPicture: true,
          );
        });
    } catch (e){
      state = state.copyWith(
        isTakingPicture: false,
        errorMessage: e.toString(),
      );
    }
  }

  Future<void> call() async {
    final phone = phoneController.text;
    if (phone.isEmpty){
      state = state.copyWith(errorMessage: 'errorMessagePhoneIsEmpty');
      return;
    }
    if (!_phoneRegex.hasMatch(phone)) {
      state = state.copyWith(errorMessage: 'errorMessagePhoneIsInvalid');
      return;
    }

  }

  void disposeControllers() {
    phoneController.removeListener(_onPhoneChanged);

    phoneController.dispose();
  }
}
