import 'dart:convert';
import 'package:crypto/crypto.dart';

import '../main.dart';
import 'box_name.dart';

class AC {
  gAK() async {
    if (box.read(BoxName.apiKeyRun).toString() != 'run') {
      // var res = await CRUD().get(link: AppLink.getApiKey, payload: {});
      // var decod = jsonDecode(res);
      // print(decod);
      //   Map<String, dynamic> jsonData = {};
      //   for (var i = 0; i < decod['message'].length; i++) {
      //     String h = decod['message'][i]['hashed_key'].toString();
      //     String retrievedString = r(r(r(h, cn), cC), cs);

      //     await storage.write(
      //       key: decod['message'][i]['name'].toString(),
      //       value: retrievedString.toString(),
      //     );
      //     //
      //     String name = decod['message'][i]['name'].toString();
      //     String value = decod['message'][i]['hashed_key'].toString();

      //     jsonData[name] = value;
      //   }
      //   String jsonString = json.encode(jsonData);
      //   print(jsonString);
      //   box.write(BoxName.apiKeyRun, 'run');
    }
  }

  String q(String b, String c) {
    final d = utf8.encode(c);
    final e = utf8.encode(b);

    final f = Hmac(sha256, d);
    final g = f.convert(e);

    final h = g.bytes;
    final i = base64Url.encode(h);
    return i;
  }

  String j(String k, String l) {
    final m = utf8.encode(l);
    final n = base64Url.decode(k);

    final o = Hmac(sha256, m);
    final p = o.convert(n);

    final q = utf8.decode(p.bytes);
    return q;
  }

  String a(String b, String c) {
    int d = b.length;
    int e = d ~/ 4;

    List<String> f = [];
    for (int g = 0; g < d; g += e) {
      int h = g + e;
      if (h > d) {
        h = d;
      }
      String i = b.substring(g, h);
      f.add(i);
    }

    print(f);
    Map<String, String> j = {};
    j['birinci'] = f[4];
    j['ikinci'] = f[2];
    j['üçüncü'] = c + f[1];
    j['dördüncü'] = f[0];
    j['beş'] = f[3];

    String k = '';
    j.forEach((l, m) {
      k += m;
    });

    return k;
  }

  Map<String, String> n(String o, String c) {
    String p = o.replaceAll(c, '');

    Map<String, String> q = {};
    q['birinci'] = p[p.length - 5] + p[p.length - 3];
    q['ikinci'] = p[p.length - 1] + p[p.length - 15];
    q['üçüncü'] = p[p.length - 9] + p[p.length - 12];
    q['dördüncü'] = p[p.length - 11] + p[p.length - 6];
    q['beş'] = p[p.length - 2] + p[p.length - 8];

    return q;
  }

  String c(String a, Map<String, String> b) {
    StringBuffer c = StringBuffer();
    c.write(a);

    String d = "Bl";
    c.write(b[d] ?? d);

    StringBuffer e = StringBuffer();
    String f = c.toString();

    for (int g = 0; g < f.length; g++) {
      String h = f[g];
      e.write(b[h] ?? h);
    }

    return e.toString();
  }

  String r(String a, Map<String, String> b) {
    StringBuffer c = StringBuffer();
    String d = "Bl";
    int e = d.length;

    for (int f = 0; f < a.length; f++) {
      String g = a[f];
      String h = b.keys.firstWhere(
        (i) => b[i] == g,
        orElse: () => g,
      );

      c.write(h);
    }

    String j = c.toString();

    if (j.endsWith(d)) {
      j = j.substring(0, j.length - e);
    }

    return j;
  }
}
