import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart';


Future<Map<String, dynamic>> readJson() async {
  final data = await rootBundle.loadString('config/credentials.json');
  var data_ = json.decode(data) as Map<String, dynamic>;
  debugPrint(data_.toString());
  return data_;
}

class CloudServiceAPI{
  late final Map<String, dynamic> credentials;
  late Future loadJson;
  late String basicAuth;
  late String address;
  late Map<String, String> headers;

  CloudServiceAPI() {
    loadJson = loadConfig();
  }

  Future loadConfig() async{
    credentials = await readJson();
    String username = credentials['username'];
    String password = credentials['password'];
    address = credentials['address'];
    basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}';
    headers = {
      'authorization': basicAuth,
      'content-type': 'application/json',
      'accept': 'application/json'
    };
  }
  Future<List> getDevices() async {
    var url = Uri.https(address, '/api/devices');
    Response r = await get(url, headers: headers);
    return json.decode(r.body) as List<dynamic>;
  }
  Future<Map<String, dynamic>> getDeviceInfo(deviceID) async {
    var url = Uri.https(address, '/api/devices/$deviceID');
    Response r = await get(url, headers: headers);
    return json.decode(r.body) as Map<String, dynamic>;
  }
  Future<Map<String, dynamic>> getInformation() async {
    var url = Uri.https(address, '/api/app');
    Response r = await get(url, headers: headers);
    return json.decode(r.body) as Map<String, dynamic>;
  }
  Future<bool> createDevice(
      id,
      primaryThumbprint,
      secondaryThumbprint) async{
    return false;
  }
}