import 'package:legacy_shared/src/data/models/entities/device_entity.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'get_linked_devices_response_model.freezed.dart';
part 'get_linked_devices_response_model.g.dart';

@freezed
abstract class GetLinkedDevicesResponseModel with _$GetLinkedDevicesResponseModel {
  const factory GetLinkedDevicesResponseModel({
    required List<GetLinkedDevicesItemResponseModel> items,
  }) = _GetLinkedDevicesResponseModel;

  factory GetLinkedDevicesResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetLinkedDevicesResponseModelFromJson(json);
}

@freezed
abstract class GetLinkedDevicesItemResponseModel with _$GetLinkedDevicesItemResponseModel {
  const factory GetLinkedDevicesItemResponseModel({
    required String identificator,
    required String carrierName,
    required String phone,
    required String id,
    required GetLinkedDevicesSettingsResponseModel settings,
    required String protocol,
    required String type,
    required String connectionServer,
    required int createdAt,
    required GetLinkedDevicesFlagsResponseModel flags,
  }) =
  _GetLinkedDevicesItemResponseModel;

  factory GetLinkedDevicesItemResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetLinkedDevicesItemResponseModelFromJson(json);
}

extension GetDevicesResponseModelMapper on GetLinkedDevicesResponseModel {
  List<DeviceEntity> toEntity() {
    return items.map((GetLinkedDevicesItemResponseModel item) => DeviceEntity(
      identificator: item.identificator,
      carrierName: item.carrierName,
      phone: item.phone,
      id: item.id,
      settings: item.settings.toEntity(),
      protocol: item.protocol,
      type: item.type,
      connectionServer: item.connectionServer,
      createdAt: item.createdAt,
      flags: item.flags.toEntity(),
    )).toList();
  }
}

@freezed
abstract class GetLinkedDevicesSettingsResponseModel with _$GetLinkedDevicesSettingsResponseModel {
  const factory GetLinkedDevicesSettingsResponseModel({
    required int frequency,
    required int frequencyHeartRate,
    required int timezone,
    required bool pedometer,
    required String language,
    required List<String> alerts,
  }) = _GetLinkedDevicesSettingsResponseModel;

  factory GetLinkedDevicesSettingsResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetLinkedDevicesSettingsResponseModelFromJson(json);
}

extension GetLinkedDevicesSettingsResponseModelMapper on GetLinkedDevicesSettingsResponseModel {
  DeviceSettingsEntity toEntity() {
    return DeviceSettingsEntity(
      frequency: frequency,
      frequencyHeartRate: frequencyHeartRate,
      timezone: timezone,
      pedometer: pedometer,
      language: language,
      alerts: alerts,
    );
  }
}

@freezed
abstract class GetLinkedDevicesFlagsResponseModel with _$GetLinkedDevicesFlagsResponseModel {
  const factory GetLinkedDevicesFlagsResponseModel({
    required String isInOrOut,
    required String geofenceId,
    required bool isBatteryLow,
    required bool isDisconnect,
  }) = _GetLinkedDevicesFlagsResponseModel;

  factory GetLinkedDevicesFlagsResponseModel.fromJson(Map<String, dynamic> json) =>
      _$GetLinkedDevicesFlagsResponseModelFromJson(json);
}

extension GetLinkedDevicesFlagsResponseModelMapper on GetLinkedDevicesFlagsResponseModel {
  DeviceFlagsEntity toEntity() {
    return DeviceFlagsEntity(
      isInOrOut: isInOrOut,
      geofenceId: geofenceId,
      isBatteryLow: isBatteryLow,
      isDisconnect: isDisconnect,
    );
  }
}