import 'package:intl/intl.dart';

import '../../models/ExchangeRate.dart';
import 'SessionCache.dart';


class AppUtils {
  /// Balance show format method
  static String formatCurrency(num value) {
    final currencyFormatter = NumberFormat.currency(
      // locale: 'eu',
      customPattern: '#,##,###',
      symbol: '', // Set the currency symbol
      decimalDigits: 2, // Set the number of decimal places
    );

    return currencyFormatter.format(value);
  }

  bool isValidEmail(String email) {
    final RegExp emailRegex = RegExp(r'^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$');
    return emailRegex.hasMatch(email);
  }

  static String formatDateTime(String dateTimeString, String format) {
    DateTime dateTime = DateTime.parse(dateTimeString);
    final formatter = DateFormat(format);
    String formattedDateTime = formatter.format(dateTime);
    return formattedDateTime;
  }

  static String showAmountWithCurr(String amount, String currencyCode) {
    ExchangeRate exchangeRate = SessionCache.instance.exchangeRateList.firstWhere((rate) => rate.pcrCurrcode == currencyCode, orElse: () => ExchangeRate.empty());
    return exchangeRate.pcrCurrshort.isEmpty ? '$currencyCode $amount' : '${exchangeRate.pcrCurrshort} $amount';
  }

  static String accountNumberFormatter(String accountNumber) {
    return accountNumber;
  }

  static bool isPasswordValid(String password, [int minLength = 8]) {
    bool hasUppercase = password.contains(RegExp(r'[A-Z]'));
    bool hasDigits = password.contains(RegExp(r'[0-9]'));
    bool hasLowercase = password.contains(RegExp(r'[a-z]'));
    bool hasSpecialCharacters = password.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
    bool hasMinLength = password.length >= minLength;
    return hasDigits & hasUppercase & hasLowercase & hasSpecialCharacters & hasMinLength;
  }

  static String getInitials(String fullName) {
    List<String> words = fullName.split(' ');
    if (words.isNotEmpty) {
      String initials = words.map((word) => word.isNotEmpty ? word[0].toUpperCase() : '').join();
      return initials.length >= 2 ? initials.substring(0, 2) : initials;
    } else {
      return '';
    }
  }

  static String formatPhoneNumber(String phoneNumber) {
    if (phoneNumber.startsWith("0")) {
      phoneNumber = "+92${phoneNumber.substring(1)}";
    }
    if (phoneNumber.startsWith("3")) {
      phoneNumber = "+92$phoneNumber";
    }
    if (phoneNumber.startsWith("92")) {
      phoneNumber = "+$phoneNumber";
    }
    if (phoneNumber.contains("-")) {
      phoneNumber = phoneNumber.replaceAll("-", "");
    }
    phoneNumber = phoneNumber.replaceAll(" ", "");
    return phoneNumber;
  }

  static String convertDateTime(String timestamp) {
    // Parse the timestamp string into a DateTime object
    DateTime dateTime = DateTime.parse(timestamp);

    // Format the DateTime object to display only the date and time
    String formattedDateTime =
        '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} '
        '${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}:${dateTime.second.toString().padLeft(2, '0')}';

    return formattedDateTime;
  }


  static String showAmountWithCurrencyCode(String amount) {
    return "Rs. " + amount;
  }
}
