import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:functions/src/core/data/models/entities/locate_device_request_entity.dart';
import 'package:functions/src/features/locate_device/domain/locate_device_use_case.dart';
import 'package:functions/src/features/locate_device/presentation/providers/locate_device_use_case_provider.dart';
import 'package:functions/src/features/locate_device/presentation/state/locate_device_view_state.dart';
// import 'package:legacy_shared/src/providers/logged_user_provider.dart';
// import 'package:sf_localizations/sf_localizations.dart';

final locateDeviceViewModelProvider =
  NotifierProvider.autoDispose<LocateDeviceViewModel, LocateDeviceViewState>(
    LocateDeviceViewModel.new,
  );

class LocateDeviceViewModel extends Notifier<LocateDeviceViewState> {
  late final LocateDeviceUseCase _locateDeviceUseCase;

  late final TextEditingController nameController;
  late final TextEditingController phoneController;

  // late final UserEntity loggedUser;

  @override
  LocateDeviceViewState build() {
    _locateDeviceUseCase = ref.read(locateDeviceUseCaseProvider);

    // loggedUser = ref.read(loggedUserProvider);

    return const LocateDeviceViewState();
  }

  Future<void> locateDevice() async {
    try {
      state = state.copyWith(
        isLoading: true,
        isComplete: false,
        errorMessage: '',
      );

      LocateDeviceRequestEntity request = LocateDeviceRequestEntity(deviceName: '');
      await _locateDeviceUseCase.locateDevice(request: request);

      state = state.copyWith(
        isLoading: false,
        isComplete: true,
      );
    } catch (e) {
      state = state.copyWith(
        isLoading: false,
        isComplete: false,
        errorMessage: e.toString(),
      );
    }
  }

  void endLocation() {
    state = state.copyWith(
      errorMessage: '',

    );
  }
}
