import 'dart:async';
import 'package:sprintf/sprintf.dart';
import 'package:xml/xml.dart' as xml;
import 'package:flutter/services.dart';

class StringsLocalization {

  static String localeCode;
  static Map<String, String> strings = new Map<String, String>();

  static Future load(String l) async {
    localeCode = l;
    var document = xml.parse(await loadStrings(l));
    strings.clear();
    document.findAllElements('string').forEach((node) {
      strings[node.attributes[0].value] = node.text.toString();
    });
    return strings;
  }

  static Future<String> loadStrings(String locale) async {
    return await rootBundle.loadString('assets/values-$locale/strings.xml');
  }

  static String declineCurrency(int num, int code) {
    int residual = num % 100;
    if (residual >= 20) {
      residual %= 10;
    }

    List<String> strings = currencies(code);

    switch (residual) {
      case 1:
        return strings[0];
      case 2:
      case 3:
      case 4:
        return strings[1];
      default: // case 0, 5-19
        return strings[2];
    }
  }

  static List<String> currencies(int code) {
    String nominative, singular, plural;

    switch (code) {
      case 643:
        nominative = nominativeRuble();
        singular = singularRuble();
        plural = pluralRuble();
        break;
      case 840:
        nominative = nominativeDollar();
        singular = singularDollar();
        plural = pluralDollar();
        break;
      case 980:
        nominative = nominativeHryvna();
        singular = singularHryvna();
        plural = pluralHryvna();
        break;
      case 978:
        nominative = nominativeEuro();
        singular = singularEuro();
        plural = pluralEuro();
        break;
      case 398:
        nominative = nominativeTenge();
        singular = singularTenge();
        plural = pluralTenge();
        break;
    }

    return [nominative, singular, plural];
  }


  static String confirmPurchase(String val, int code) {
    String trimmedVal = val.substring(0, val.length - 3);
    return sprintf(strings['confirm_purchase'], [val, declineCurrency(int.parse(trimmedVal), code)]);
  }

  static String purchaseCompleted(String val, int code) {
    String trimmedVal = val.substring(0, val.length - 3);
    return sprintf(strings['purchase_complite'], [val, declineCurrency(int.parse(trimmedVal), code)]);
  }

  static String registration() => strings['registration'];
  static String usage() => strings['usage'];
  static String support() => strings['support'];
  static String common() => strings['common'];
  static String idStore() => strings['ID_Store'];
  static String signUp() => strings['sign_up'];
  static String specifyDinStore() => strings['specify_din_store'];
  static String confirmation() => strings['confirmation'];
  static String askChangeStore() => strings['ask_change_store'];
  static String yes() => strings['yes'];
  static String no() => strings['no'];
  static String requestSentWaitActivation() => strings['request_sent_wait_activ'];
  static String refreshActivationStatus() => strings['update_activ_status'];
  static String appActivated() => strings['app_activ'];
  static String completeRegistration() => strings['complite_activ'];
  static String cardScanner() => strings['card_scaner'];
  static String card() => strings['card'];
  static String reward() => strings['reward'];
  static String sum() => strings['sum'];
  static String bonus() => strings['bonus'];
  static String couponsUsed() => strings['coupons_used'];
  static String selectCoupons() => strings['select_coupons'];
  static String carryingPurchase() => strings['carry_purchase'];
  static String completePurchase() => strings['complite_purchase'];
  static String scan() => strings['scan'];
  static String buyer() => strings['buyer'];
  static String bonusHint() => strings['bonus_hint'];
  static String bonusExplanation() => strings['bonus_explanation'];
  static String discountRate() => strings['discount_rate'];
  static String discountSum() => strings['discount_sum'];
  static String bonusPlus() => strings['bonus_plus'];
  static String bonusMinus() => strings['bonus_minus'];
  static String idNotFound() => strings['ID_not_found'];
  static String settings() => strings['settings'];
  static String help() => strings['help'];
  static String exit() => strings['exit'];
  static String logout() => strings['logout'];
  static String currency() => strings['currency'];
  static String locale() => strings['locale'];
  static String nominativeRuble() => strings['nominative_ruble'];
  static String singularRuble() => strings['singular_ruble'];
  static String pluralRuble() => strings['plural_ruble'];
  static String nominativeEuro() => strings['nominative_euro'];
  static String singularEuro() => strings['singular_euro'];
  static String pluralEuro() => strings['plural_euro'];
  static String nominativeDollar() => strings['nominative_dollar'];
  static String singularDollar() => strings['singular_dollar'];
  static String pluralDollar() => strings['plural_dollar'];
  static String nominativeHryvna() => strings['nominative_hryvna'];
  static String singularHryvna() => strings['singular_hryvna'];
  static String pluralHryvna() => strings['plural_hryvna'];
  static String nominativeTenge() => strings['nominative_tenge'];
  static String singularTenge() => strings['singular_tenge'];
  static String pluralTenge() => strings['plural_tenge'];
  static String registrationGuide() => strings['registration_guide'];
  static String usageGuide() => strings['usage_guide'];
  static String commonGuide() => strings['common_guide'].replaceAll('\n', "\n");
  static String supportGuide(String phone, String url) => sprintf(strings['support_guide'], [phone, url]);
}
