import 'package:account/src/features/linked_devices/domain/delete_device_use_case.dart';
import 'package:legacy_shared/src/data/models/entities/device_entity.dart';
import 'package:account/src/features/linked_devices/domain/entities/update_device_request_entity.dart';
import 'package:account/src/features/linked_devices/domain/get_linked_devices_use_case.dart';
import 'package:account/src/features/linked_devices/domain/update_device_use_case.dart';
import 'package:account/src/features/linked_devices/presentation/providers/delete_device_use_case_provider.dart';
import 'package:account/src/features/linked_devices/presentation/providers/get_linked_devices_use_case_provider.dart';
import 'package:account/src/features/linked_devices/presentation/providers/update_device_use_case_provider.dart';
import 'package:account/src/features/linked_devices/presentation/state/linked_devices_view_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:legacy_shared/legacy_shared.dart';
// import 'package:sf_localizations/sf_localizations.dart';

final linkedDevicesViewModelProvider =
NotifierProvider.autoDispose<LinkedDevicesViewModel, LinkedDevicesViewState>(
  LinkedDevicesViewModel.new,
);

class LinkedDevicesViewModel extends Notifier<LinkedDevicesViewState> {
  late final GetLinkedDevicesUseCase _getLinkedDevicesUseCase;
  late final UpdateDeviceUseCase _updateDeviceUseCase;
  late final DeleteDeviceUseCase _deleteDeviceUseCase;

  late final TextEditingController deviceNameController;

  @override
  LinkedDevicesViewState build() {
    _getLinkedDevicesUseCase = ref.read(getLinkedDevicesUseCaseProvider);
    _updateDeviceUseCase = ref.read(updateDeviceUseCaseProvider);
    _deleteDeviceUseCase = ref.read(deleteDeviceUseCaseProvider);

    deviceNameController = TextEditingController();
    deviceNameController.addListener(_onDeviceNameChanged);

    ref.read(loggedUserProvider.future)
        .then((user){
          setUser(user);
          return _getLinkedDevicesUseCase.getLinkedDevices(userId: user.id);
        }).then(setLinkedDevices);

    ref.onDispose(disposeControllers);

    return const LinkedDevicesViewState();
  }

  void setUser(UserEntity user) {
    state = state.copyWith(loggedUser: user);
  }

  void setLinkedDevices(List<DeviceEntity> linkedDevices) {
    state = state.copyWith(linkedDevices: linkedDevices);
  }

  void toggleIsEditing() {
    state = state.copyWith(isEditing: !state.isEditing);
  }

  void _onDeviceNameChanged() {
    final value = deviceNameController.text;

    if (value == state.deviceName) return;

    state = state.copyWith(
      deviceName: value,
    );
  }

  Future<bool> deleteDevice(DeviceEntity device) async {
    try {
      await _deleteDeviceUseCase.deleteDevice(userId: state.loggedUser!.id, deviceId: device.identificator);
      List<DeviceEntity> newList = state.linkedDevices;
      newList.remove(device);

      state = state.copyWith(
          linkedDevices: newList
      );

      return true;
    } catch (e) {
      return false;
    }
  }

  UpdateDeviceRequestEntity _toRequest(DeviceEntity device) {

    return UpdateDeviceRequestEntity(
      identificator: device.identificator,
      name: state.deviceName.trim(),
      // phone: /*state.dialCode.trim() + */state.phoneNumber.trim(),
    );
  }

  void updateDevice(DeviceEntity device) {
    final deviceName = state.deviceName;

    if (deviceName.isEmpty) {
      state = state.copyWith(
        errorMessage: ''
      );
      return;
    }
    final userId = 'test';
    _updateDeviceUseCase.updateDevice(
        userId: userId,
        deviceId: device.identificator,
        request: _toRequest(device));

  }

  void disposeControllers() {
    deviceNameController.removeListener(_onDeviceNameChanged);
    deviceNameController.dispose();
  }
}