import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';

class DeviceInfoPlus {
  static List<Map<String, dynamic>> deviceDataList = [];

  static Future<List<Map<String, dynamic>>> getDeviceInfo() async {
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

    try {
      if (Platform.isAndroid) {
        AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
        Map<String, dynamic> deviceData = {
          'platform': 'Android',
          'brand': androidInfo.brand,
          'model': androidInfo.model,
          'androidId': androidInfo.device,
          'versionRelease': androidInfo.version.release,
          'sdkVersion': androidInfo.version.sdkInt,
          'manufacturer': androidInfo.manufacturer,
          'isPhysicalDevice': androidInfo.isPhysicalDevice,
          'serialNumber': androidInfo.serialNumber,
          'fingerprint': androidInfo.fingerprint,
          'type': androidInfo.type,
          'data': androidInfo.data,
          'version': androidInfo.version,
          'tags': androidInfo.tags,
          'display': androidInfo.display,
        };
        deviceDataList.add(deviceData);
      } else if (Platform.isIOS) {
        IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
        Map<String, dynamic> deviceData = {
          'brand': 'Apple',
          'model': iosInfo.model,
          'systemName': iosInfo.systemName,
          'systemVersion': iosInfo.systemVersion,
          'utsname': iosInfo.utsname,
          'isPhysicalDevice': iosInfo.isPhysicalDevice,
          'identifierForVendor': iosInfo.identifierForVendor,
          'name': iosInfo.name,
          'localizedModel': iosInfo.localizedModel,
        };
        deviceDataList.add(deviceData);
      } else if (Platform.isMacOS) {
        MacOsDeviceInfo macInfo = await deviceInfoPlugin.macOsInfo;
        Map<String, dynamic> deviceData = {
          'platform': 'macOS',
          'model': macInfo.model,
          'version': macInfo.systemGUID,
        };
        deviceDataList.add(deviceData);
      } else if (Platform.isWindows) {
        WindowsDeviceInfo windowsInfo = await deviceInfoPlugin.windowsInfo;
        Map<String, dynamic> deviceData = {
          'platform': 'Windows',
          'manufacturer': windowsInfo.computerName,
          'version': windowsInfo.majorVersion,
          'deviceId': windowsInfo.deviceId,
          'userName': windowsInfo.userName,
          'productName': windowsInfo.productName,
          'installDate': windowsInfo.installDate,
          'productId': windowsInfo.productId,
          'numberOfCores': windowsInfo.numberOfCores,
          'systemMemoryInMegabytes': windowsInfo.systemMemoryInMegabytes,
        };
        deviceDataList.add(deviceData);
      } else if (Platform.isLinux) {
        LinuxDeviceInfo linuxInfo = await deviceInfoPlugin.linuxInfo;
        Map<String, dynamic> deviceData = {
          'platform': 'Linux',
          'manufacturer': linuxInfo.name,
          'version': linuxInfo.version,
        };
        deviceDataList.add(deviceData);
      }
    } catch (e) {
      print('Failed to get device info: $e');
    }

    return deviceDataList;
  }

  // Method to print all device data
  static void printDeviceInfo() {
    for (Map<String, dynamic> deviceData in deviceDataList) {
      print('Platform: ${deviceData['platform']}');
      print('Brand: ${deviceData['brand']}');
      print('Model: ${deviceData['model']}');
      print(
          'Version: ${deviceData['version'] ?? deviceData['versionRelease'] ?? 'N/A'}');
      print('Manufacturer: ${deviceData['manufacturer'] ?? 'N/A'}');
      print('Is Physical Device: ${deviceData['isPhysicalDevice']}');
      print('Serial Number: ${deviceData['serialNumber'] ?? 'N/A'}');
      print('Fingerprint: ${deviceData['fingerprint'] ?? 'N/A'}');
      print('Type: ${deviceData['type'] ?? 'N/A'}');
      print('Data: ${deviceData['data'] ?? 'N/A'}');
      print('Tags: ${deviceData['tags'] ?? 'N/A'}');
      print('Display: ${deviceData['display'] ?? 'N/A'}');
      print('--------------------');
    }
  }
}
