import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:path/path.dart' as path;

class Globals {
  static final Globals _singleton = Globals._internal();

  factory Globals() {
    return _singleton;
  }

  Globals._internal();

  bool _initialized = false;

  String appName = '';
  String packageName = '';
  String version = '';
  String buildNumber = '';
  String platform = '';
  String hostname = '';
  String clientType = '';
  String deviceModel = '';

  late SharedPreferences sharedPrefs;

  late Directory appDocumentsDir;
  late Directory rawFailureLogsDir;

  bool get isInitialized => _initialized;

  Future<void> init() async {
    if (_initialized) return;

    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    this.appName = packageInfo.appName;
    this.packageName = packageInfo.packageName;
    this.version = packageInfo.version;
    this.buildNumber = packageInfo.buildNumber;
    this.platform = Platform.operatingSystem;
    this.hostname = Platform.localHostname;

    if (Platform.isAndroid) {
      this.clientType = 'ANDROID';
      this.deviceModel = (await DeviceInfoPlugin().androidInfo).model;
    } else if (Platform.isIOS) {
      this.clientType = 'IOS';
      this.deviceModel = (await DeviceInfoPlugin().iosInfo).model;
    } else if (Platform.isLinux) {
      this.clientType = 'LINUX';
      this.deviceModel = (await DeviceInfoPlugin().linuxInfo).prettyName;
    } else if (Platform.isWindows) {
      this.clientType = 'WINDOWS';
      this.deviceModel = (await DeviceInfoPlugin().windowsInfo).productName;
    } else if (Platform.isMacOS) {
      this.clientType = 'MACOS';
      this.deviceModel = (await DeviceInfoPlugin().macOsInfo).model;
    } else {
      this.clientType = '?';
    }

    this.sharedPrefs = await SharedPreferences.getInstance();

    this.appDocumentsDir = await getApplicationDocumentsDirectory();

    this.rawFailureLogsDir = Directory(path.join(Globals().appDocumentsDir.path, "rawlogs"));
    await this.rawFailureLogsDir.create(recursive: true);

    this._initialized = true;
  }

  String? getPrefFCMToken() {
    return sharedPrefs.getString("fcm.token");
  }

  Future<bool> setPrefFCMToken(String value) {
    return sharedPrefs.setString("fcm.token", value);
  }
}
