import 'package:flutter/cupertino.dart';

extension NavigatorStateExtension on NavigatorState {
  void pushNamedIfNotCurrent(String routeName, {Object? arguments}) {
    if (!isCurrent(routeName)) {
      pushNamed(routeName, arguments: arguments);
    }
  }

  bool isCurrent(String routeName) {
    bool isCurrent = false;
    popUntil((route) {
      if (route.settings.name == routeName) {
        isCurrent = true;
      }
      return true;
    });
    return isCurrent;
  }
}

extension StringUrl on String {
  bool startsWithEnglish() {
    // Regular expression to check if the first character is an English letter
    return RegExp(r'^[A-Za-z]').hasMatch(this);
  }

  bool startsWithPersian() {
    // Regular expression to check if the first character is a Persian letter
    return RegExp(r'^[\u0600-\u06FF]').hasMatch(this);
  }

  bool isImage() {
    final extension = split('.').last.toLowerCase();
    const imageExtensions = [
      'jpg',
      'jpeg',
      'png',
      'gif',
      'bmp',
      'webp',
      'tiff'
    ];
    return imageExtensions.contains(extension);
  }

  bool isDocument() {
    final extension = split('.').last.toLowerCase();
    const documentExtensions = [
      'pdf',
      'doc',
      'docx',
      'xls',
      'xlsx',
      'ppt',
      'pptx',
      'txt'
    ];
    return documentExtensions.contains(extension);
  }

  bool isAudio() {
    final extension = split('.').last.toLowerCase();
    const audioExtensions = ['mp3', 'wav', 'aac', 'ogg', 'flac'];
    return audioExtensions.contains(extension);
  }

  bool isVideo() {
    final extension = split('.').last.toLowerCase();
    const videoExtensions = ['mp4', 'avi', 'mov', 'wmv', 'mkv', 'flv'];
    return videoExtensions.contains(extension);
  }

  String convertToEnglishNumber() {
    final Map<String, String> persianToEnglishMap = {
      '۰': '0',
      '۱': '1',
      '۲': '2',
      '۳': '3',
      '۴': '4',
      '۵': '5',
      '۶': '6',
      '۷': '7',
      '۸': '8',
      '۹': '9',
    };

    return split('').map((char) {
      return persianToEnglishMap[char] ??
          char; // Replace with English number if exists, else keep original char
    }).join('');
  }
}
