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

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

class CloudServiceAPI {
  static late final Map<String, dynamic> credentials;
  static late Future loadJson;
  static late String basicAuth;
  static late String address;
  static late String username;
  static late String password;
  static late Map<String, String> headers;
  static bool initState = false;

  CloudServiceAPI(){
    if(!initState) {
      loadJson = loadConfig();
      initState = true;
    }
  }

  Future loadConfig() async{
    credentials = await readJson();
    username = credentials['username'];
    password = credentials['password'];
    address = credentials['address'];
    basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}';
    headers = {
      'authorization': basicAuth,
      'content-type': 'application/json',
      'accept': 'application/json',
    };
    log("init Config $headers");
  }
  void reloadConfig() {
    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{
    var url = Uri.https(address, '/api/devices');
    Response r = await post(
        url,
        headers: headers,
        body: jsonEncode(<String, String>{
          'id': id,
          'primaryThumbprint' : primaryThumbprint,
          'secondaryThumbprint' : secondaryThumbprint
        })
    );
    if (r.statusCode == 200){
      return true;
    }
    debugPrint('Error createDevice: ${r.statusCode.toString()}');
    return false;
  }
  String getAddress(){
    return address;
  }
  String getUsername(){
    return username;
  }
  String getPassword(){
    return password;
  }
  void setAddress(String input){
    address = input;
    reloadConfig();
  }
  void setUsername(String input){
    username = input;
    reloadConfig();
  }
  void setPassword(String input){
    password = input;
    reloadConfig();
  }
}