import 'dart:developer';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';

import 'package:flutter/services.dart';

class DeviceUtil {
  static const MethodChannel _channel =
      MethodChannel("com.freaccess.hub/getAndroidId");

  static Future<String?> getAndroidId() async {
    try {
      final String? androidId = await _channel.invokeMethod('getAndroidId');

      return androidId;
    } on PlatformException catch (e) {
      log("Failed to get Android ID: '${e.message}'.");

      return null;
    }
  }

  static Future<String?> getDevUUID() async {
    var deviceInfo = DeviceInfoPlugin();

    if (Platform.isIOS) {
      var iosDeviceInfo = await deviceInfo.iosInfo;
      return iosDeviceInfo.identifierForVendor;
    } else if (Platform.isAndroid) {
      try {
        final String? secureAndroidId = await getAndroidId();
        return secureAndroidId;
      } catch (e) {
        final build = await deviceInfo.androidInfo;
        return build.id;
      }
    }

    return null;
  }

  static Future<String?> getSerialNumber() async {
    var deviceInfo = DeviceInfoPlugin();

    if (Platform.isIOS) {
      // import 'dart:io'

      var iosDeviceInfo = await deviceInfo.iosInfo;

      return iosDeviceInfo.identifierForVendor; // unique ID on iOS
    } else if (Platform.isAndroid) {
      var androidDeviceInfo = await deviceInfo.androidInfo;

      return androidDeviceInfo.serialNumber; // unique ID on Android
    }

    return null;
  }

  static Future<String?> getDeviceOSVersion() async {
    var deviceInfo = DeviceInfoPlugin();

    if (Platform.isIOS) {
      var iosDeviceInfo = await deviceInfo.iosInfo;

      return iosDeviceInfo.systemVersion; // unique ID on iOS. e.g. 14.5
    } else if (Platform.isAndroid) {
      var androidDeviceInfo = await deviceInfo.androidInfo;

      return androidDeviceInfo
          .version.release; // unique ID on Android. e.g . 11
    }

    return null;
  }
}
