/* 
 * This file is part of Stack Wallet.
 * 
 * Copyright (c) 2023 Cypher Stack
 * All Rights Reserved.
 * The code is distributed under GPLv3 license, see LICENSE file for details.
 * Generated by Cypher Stack on 2023-05-26
 *
 */

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

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:intl/number_symbols.dart';
import 'package:intl/number_symbols_data.dart';

abstract class Util {
  static Directory? libraryPath;
  static double? screenWidth;

  static NumberSymbols? getSymbolsFor({required String locale}) {
    return numberFormatSymbols[locale] as NumberSymbols? ??
        numberFormatSymbols[locale.replaceAll("-", "_")] as NumberSymbols? ??
        numberFormatSymbols[locale.substring(3).toLowerCase()]
            as NumberSymbols? ??
        numberFormatSymbols[locale.substring(0, 2)] as NumberSymbols?;
  }

  static bool get isDesktop {
    // special check for running on linux based phones
    if (Platform.isLinux && screenWidth != null && screenWidth! < 800) {
      return false;
    }

    // special check for running under ipad mode in macos
    if (Platform.isIOS &&
        libraryPath != null &&
        !libraryPath!.path.contains("/var/mobile/")) {
      return true;
    }

    return Platform.isLinux || Platform.isMacOS || Platform.isWindows;
  }

  static Future<bool> get isIPad async {
    final deviceInfo = (await DeviceInfoPlugin().deviceInfo);
    if (deviceInfo is IosDeviceInfo) {
      return (deviceInfo).name?.toLowerCase().contains("ipad") == true;
    }
    return false;
  }

  static MaterialColor createMaterialColor(Color color) {
    final List<double> strengths = <double>[.05];
    final swatch = <int, Color>{};
    final int r = color.red, g = color.green, b = color.blue;

    for (int i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }
    for (final strength in strengths) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        r + ((ds < 0 ? r : (255 - r)) * ds).round(),
        g + ((ds < 0 ? g : (255 - g)) * ds).round(),
        b + ((ds < 0 ? b : (255 - b)) * ds).round(),
        1,
      );
    }
    return MaterialColor(color.value, swatch);
  }

  static void printJson(dynamic json, [String? debugTitle]) {
    final String result;
    if (json is Map || json is List) {
      final spaces = ' ' * 4;
      final encoder = JsonEncoder.withIndent(spaces);
      final pretty = encoder.convert(json);
      result = pretty;
    } else {
      result = dynamic.toString();
    }

    if (debugTitle != null) {
      log("$debugTitle\n$result");
    } else {
      log(result);
    }
  }
}