import 'dart:developer';

import 'package:intl/intl.dart';

extension formatDateTime on DateTime {
  String convertLocalDateTimeToStringUtcDateTime() {
    try {
      String utcDateString = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(toUtc());
      return utcDateString;
    } catch (_) {
      return "";
    }
  }

  String displayDateTime_DDMMYYYY_HHmm() {
    try {
      return DateFormat("dd/MM/yyyy HH:mm").format(this);
    } catch (_) {
      return "";
    }
  }
}

extension ddMM_HHmm on String {
  //"2020-08-13T02:11:32Z" => 13/08 02:11
  // convert utc to local timezone
  String format_DDMM_HHmm() {
    try {
      final str = toString();
      var dateFromString = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(str, true).toLocal();
      return DateFormat("dd/MM HH:mm").format(dateFromString);
    } catch (_) {
      return "";
    }
  }

  // Convert server timezone + 0 -> local timezone
  String format_DDMMYY_HHmm() {
    try {
      final str = toString();
      var dateFromString = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(str, true).toLocal();
      return DateFormat("dd/MM/yyyy HH:mm").format(dateFromString);
    } catch (_) {
      return "";
    }
  }

  DateTime? convertStringServerDateTimeToLocalDateTime() {
    try {
      return DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(this, true).toLocal();
    } catch (_) {
      return null;
    }
  }

  String formatStringToStringDecimal() {
    try {
      var numOfString = double.tryParse(toString());
      var numWithLocalSeparator = NumberFormat.decimalPattern("en_US");
      final str = numWithLocalSeparator.format(numOfString);
      return str;
    } catch (_) {
      return "";
    }
  }
}

extension numToString on num {
  String formatNumtoStringDecimal() {
    try {
      var numWithLocalSeparator = NumberFormat.decimalPattern("en_US");
      final str = numWithLocalSeparator.format(this);
      return str;
    } catch (_) {
      return "";
    }
  }
}

extension HHmm on Duration {
  String formatHHmm() {
    //1:34:00.000000
    final str = toString();

    final texts = str.split(":");
    final textHour = texts[0].padLeft(2, '0');
    final textMinute = texts[1].padLeft(2, '0');

    return "${textHour}h ${textMinute}m";
  }
}

extension FormatNumber on int {
  String formatDecimalThousand() {
    //1403 -> 1,403
    var f = NumberFormat.decimalPattern("en_US");
    return f.format(this);
  }
}

extension FormatNumberToDecimal on double {
  String formatDecimalThousand() {
    var f = NumberFormat.decimalPattern("en_US");
    return f.format(this);
  }
}

extension FormatDate on int {
  String MMM_dd_yyyy() {
    return DateFormat("MMM dd, yyyy").format(DateTime.fromMillisecondsSinceEpoch(this * 1000));
  }
}

extension DoubleParsing on String {
  double parseDoubleThousand() {
    var newValue = replaceAll(",", "");
    return double.tryParse(newValue) ?? 0.0;
  }
}

extension IntParsing on String {
  int parseIntThousand() {
    var newValue = replaceAll(",", "");
    return int.tryParse(newValue) ?? 0;
  }
}
