//import "package:"
import "dart:convert";
import 'package:http/http.dart' as http;


//lb.eu1.gigya.com
//socialize.eu1.gigya.com
//myrapp-one-wired-prod-1-euw1.wrd-aws.com
//api-wired-prod-1-euw1.wrd-aws.com
//static-wrd-prod-1.wrd-aws.com
//myrbff-prod-1-euw1.wrd-aws.com
//wrd-p-ecsmg-1eqqjmriffcyj-1652433335.eu-west-1.elb.amazonaws.com

//final eingabe = "";


test() async {
  //void main(List<String> arguments) async {

  final user = "dschaal@web.de";
  final pw = "1Philips3+";
  final vin = "UU1DBG001PU125112";
  final country = "DE";
  final locale = "de_DE";
  //final gigyaUrl = "https://accounts.eu1.gigya.com/accounts.login";
  final gigyaUrl = "https://lb.eu1.gigya.com/accounts.login";
  final gigyaApiKey =
      "3_7PLksOyBRkHv126x5WhHb-5pqC1qFR8pQjxSeLB6nhAnPERTUlwnYoznHSxwX668";
  final kamereonRootUrl = "https://api-wired-prod-1-euw1.wrd-aws.com";
  final kamereonApiKey = "YjkKtHmGfaceeuExUDKGxrLZGGvtVS0J";
  var datenMap = Map<String, dynamic>();
  datenMap['ApiKey'] = gigyaApiKey;
  datenMap['loginId'] = user;
  datenMap['password'] = pw;
  datenMap['include'] = 'data';
  datenMap['sessionExpiration'] = "60";
  print("gigya1 Map");
  print(datenMap);
  final ergebnis = await http.post(
    Uri.parse(gigyaUrl),
    headers: {
      'User-Agent': 'YourApp/1.0',  // Gleicher Header wie auf Linux
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: datenMap,
    encoding: Encoding.getByName('utf-8'), // Explizites Encoding
  );
  print("ergebnis body");
  print(ergebnis.body);
  //gigya1-Anmeldung
  var ergebniss1 = jsonDecode(ergebnis.body);
  final loginToken = ergebniss1["sessionInfo"]["cookieValue"];
  final personId = ergebniss1["data"]["personId"];
  print("person-id ");
  print(personId);
  var daten2Map = Map<String, dynamic>();
  daten2Map["login_token"] = loginToken;
  daten2Map["ApiKey"] = gigyaApiKey;
  daten2Map["fields"] = "data.personId,data.gigyaDataCenter";
  daten2Map["expiration"] = "87000";

  //gigya2-Anmeldung
  var ergebniss2 = await http.post(
    //Uri.parse("https://accounts.eu1.gigya.com/accounts.getJWT"),
    Uri.parse("https://lb.eu1.gigya.com/accounts.getJWT"),
    body: daten2Map,
  );
  final ergebnis3 = jsonDecode(ergebniss2.body);
  final id_token = ergebnis3["id_token"];
  var kamareonMap = Map<String, dynamic>();
  kamareonMap["ApiKey"] = kamereonApiKey;
  kamareonMap["x-gigya-id_token"] = id_token;
  final kamereonUrl =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/persons/${personId}?country=${country}";
  final header = Map<String, String>() = {
    'apikey': kamereonApiKey,
    "x-gigya-id_token": id_token,
    "Content-type": "application/vnd.api+json",
  };

  //kamereon-Anmeldung
  var ergebniss3 = await http.get(Uri.parse(kamereonUrl), headers: header);
  var kamereonDecode = jsonDecode(ergebniss3.body);
  final accountId = kamereonDecode["accounts"][0]["accountId"];

  var urlBatteryStatus =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v2/cars/${vin}/battery-status?country=${country}";
  var urlCockpit =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/cockpit?country=${country}";
  var urlHvacStatus =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/hvac-status?country=${country}";
  var urlLocation =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/location?country=${country}";
  var urlklimaStart =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/actions/hvac-start?country=${country}";
  var urlbatteryLoad =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kcm/v1/vehicles/${vin}/charge/pause-resume?country=${country}";

  var batteryStopLoadJson = jsonEncode({
    "data": {
      "type": "ChargePauseResume",
      "attributes": {"action": true ? "pause" : "resume"}
    }
  });
  var batteryLoadJson = jsonEncode({
    "data": {
      "type": "ChargePauseResume",
      "attributes": {"action": false ? "pause" : "resume"}
    }
  });
  final url =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/05cdc620-5100-41c3-b323-7cef3cb1880f/kamereon/kca/car-adapter/v2/cars/0UU1DBG001PU125112/battery-status?country=DE";
  var klimaStartJson = jsonEncode({
    "data": {
      "type": "HvacStart",
      "attributes": {"action": "start", "targetTemperature": "21"}
    }
  });

  //Holen der Autodaten
  var autoDaten1 = await http.get(Uri.parse(urlBatteryStatus), headers: header);
  var autoDaten2 = await http.get(Uri.parse(urlHvacStatus), headers: header);
  var autoDaten3 = await http.get(Uri.parse(urlLocation), headers: header);
  var autoDaten4 = await http.get(Uri.parse(urlCockpit), headers: header);

  //wenn der Statuscode 200 it, dann ist alles ok

  //Die Ausgaben der einzelnen Autodaten
  print(autoDaten1.body);
  print(autoDaten2.body);
  print(autoDaten3.body);
  print(autoDaten4.body);
}


rest1() async {
//void main() async {

  //test();
  //void main(List<String> arguments) async {

  final user = "dschaal@web.de";
  final pw = "1Philips3+";
  final vin = "UU1DBG001PU125112";
  final country = "DE";
  final locale = "de_DE";
  //het auch mit accounts
  //final gigyaUrl = "https://accounts.eu1.gigya.com/accounts.login";
  final gigyaUrl = "https://lb.eu1.gigya.com/accounts.login";
  final gigyaApiKey =
      "3_7PLksOyBRkHv126x5WhHb-5pqC1qFR8pQjxSeLB6nhAnPERTUlwnYoznHSxwX668";
  final kamereonRootUrl = "https://api-wired-prod-1-euw1.wrd-aws.com";
  final kamereonApiKey = "YjkKtHmGfaceeuExUDKGxrLZGGvtVS0J";
  var datenMap = Map<String, dynamic>();
  datenMap['ApiKey'] = gigyaApiKey;
  datenMap['loginId'] = user;
  datenMap['password'] = pw;
  datenMap['include'] = 'data';
  datenMap['sessionExpiration'] = "60";
  print("gigya1 Map");
  print(datenMap);
  final ergebnis = await http.post(
    Uri.parse(gigyaUrl),
    body: datenMap,
  );
  //gigya1-Anmeldung
  var ergebniss1 = jsonDecode(ergebnis.body);
  print("body");
  print(ergebnis.body);
  final loginToken = ergebniss1["sessionInfo"]["cookieValue"];
  final personId = ergebniss1["data"]["personId"];
  print("person-id ");
  print(personId);
  var daten2Map = Map<String, dynamic>();
  daten2Map["login_token"] = loginToken;
  daten2Map["ApiKey"] = gigyaApiKey;
  daten2Map["fields"] = "data.personId,data.gigyaDataCenter";
  daten2Map["expiration"] = "87000";

  //gigya2-Anmeldung
  var ergebniss2 = await http.post(
    //Uri.parse("https://accounts.eu1.gigya.com/accounts.getJWT"),
    Uri.parse("https://lb.eu1.gigya.com/accounts.getJWT"),
    body: daten2Map,
  );
  final ergebnis3 = jsonDecode(ergebniss2.body);
  final id_token = ergebnis3["id_token"];
  var kamareonMap = Map<String, dynamic>();
  kamareonMap["ApiKey"] = kamereonApiKey;
  kamareonMap["x-gigya-id_token"] = id_token;
  final kamereonUrl =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/persons/${personId}?country=${country}";
  final header = Map<String, String>() = {
    'apikey': kamereonApiKey,
    "x-gigya-id_token": id_token,
    "Content-type": "application/vnd.api+json",
  };

  //kamereon-Anmeldung
  var ergebniss3 = await http.get(Uri.parse(kamereonUrl), headers: header);
  var kamereonDecode = jsonDecode(ergebniss3.body);
  final accountId = kamereonDecode["accounts"][0]["accountId"];

  var urlBatteryStatus =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v2/cars/${vin}/battery-status?country=${country}";
  var urlCockpit =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/cockpit?country=${country}";
  var urlHvacStatus =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/hvac-status?country=${country}";
  var urlLocation =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/location?country=${country}";
  var urlklimaStart =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kca/car-adapter/v1/cars/${vin}/actions/hvac-start?country=${country}";
  var urlbatteryLoad =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/${accountId}/kamereon/kcm/v1/vehicles/${vin}/charge/pause-resume?country=${country}";

  var batteryStopLoadJson = jsonEncode({
    "data": {
      "type": "ChargePauseResume",
      "attributes": {"action": true ? "pause" : "resume"}
    }
  });
  var batteryLoadJson = jsonEncode({
    "data": {
      "type": "ChargePauseResume",
      "attributes": {"action": false ? "pause" : "resume"}
    }
  });
  final url =
      "https://api-wired-prod-1-euw1.wrd-aws.com/commerce/v1/accounts/05cdc620-5100-41c3-b323-7cef3cb1880f/kamereon/kca/car-adapter/v2/cars/0UU1DBG001PU125112/battery-status?country=DE";
  var klimaStartJson = jsonEncode({
    "data": {
      "type": "HvacStart",
      "attributes": {"action": "start", "targetTemperature": "21"}
    }
  });

  /*
//Starten der Klimaanlage
  var autoklimaStart = await http.post(
    Uri.parse(urlklimaStart),
    headers: header,
    body: klimaStartJson,
  );

  //Starten des Ladevorganges
  var autoladenStart = await http.post(
    Uri.parse(urlbatteryLoad),
    headers: header,
    body: batteryLoadJson,
  );

  //Stopp des Ladevorganges
  var autoladenStopStart = await http.post(
    Uri.parse(urlbatteryLoad),
    headers: header,
    body: batteryStopLoadJson,
  );
 print(autoklimaStart.statusCode);
  print(autoladenStart.statusCode);
  print(autoladenStopStart.statusCode);
   */
  //Holen der Autodaten
  var autoDaten1 = await http.get(Uri.parse(urlBatteryStatus), headers: header);
  var autoDaten2 = await http.get(Uri.parse(urlHvacStatus), headers: header);
  var autoDaten3 = await http.get(Uri.parse(urlLocation), headers: header);
  var autoDaten4 = await http.get(Uri.parse(urlCockpit), headers: header);

  //wenn der Statuscode 200 it, dann ist alles ok

  //Die Ausgaben der einzelnen Autodaten
  print(autoDaten1.body);
  print(autoDaten2.body);
  print(autoDaten3.body);
  print(autoDaten4.body);


}
