import 'dart:convert';

import 'package:fhub/backend/DataManager.dart';
import 'package:fhub/backend/DebugHelper.dart';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class LoginManager {

  static Future<int> AutoLogin() async{
    final prefs = await SharedPreferences.getInstance();
    String username = "";
    String password= "";
    try{
      if(prefs.containsKey("googleLogin")){
        if(prefs.getBool("googleLogin") ?? false){

        }
      }

      username = prefs.getString("username")!;
      password = prefs.getString("password")!;
    }catch(e){
      Debug.LogError(e);
    }

    if(username.length > 3 && password.length >5){
      return await Login(username, password);
    }

    return 1;
  }

  static Future<int> Register (String username, String password) async{
    Debug.Log("Starting registration");
    final prefs = await SharedPreferences.getInstance();

    var loginResponse = null;
    try {
      loginResponse = (await http.post(
          Uri.parse('${DataManager.API_ENDPOINT}register.php'),
          body: <String, String>{
            "username": username,
            "password": password
          }));
      Debug.Log("Reg response: " + loginResponse.body.toString());


      if (loginResponse.body.toLowerCase() == "0") {
        prefs.setString("username", username);
        prefs.setString("password", password);

        return 0;

      }else{
        return 5;
      }
    } catch (e) {
      Debug.LogError("Error while login $e");
    }

    return 1;
  }

  static Future<int> Login (String username, String password) async{
    Debug.Log("Logging in");
    final prefs = await SharedPreferences.getInstance();

    var loginResponse = null;
    try {
      loginResponse = (await http.post(
          Uri.parse('${DataManager.API_ENDPOINT}login.php'),
          body: <String, String>{
            "username": username,
            "password": password
          }));
      Debug.LogError(loginResponse.body.toString());

      try{
        DataManager.UserJson = jsonDecode(loginResponse.body.toString());
        await DataManager.GetGamesProgress();
        Debug.LogResponse(DataManager.UserJson);
        prefs.setString("username", username);
        prefs.setString("password", password);
        return 0;
      }catch(e){
        return 5;
      }
    } catch (e) {
      Debug.LogError("Error while login $e");
    }

    return 1;
  }

  static Future<int> GoogleLogin (String email) async{
    Debug.Log("Logging in");
    final prefs = await SharedPreferences.getInstance();

    var loginResponse = null;
    try {
      var username = email + "#0";
      loginResponse = (await http.post(
          Uri.parse('${DataManager.API_ENDPOINT}google_login.php'),
          body: <String, String>{
            "username": username
          }));
      Debug.LogError(loginResponse.body.toString());

      try{
        DataManager.UserJson = jsonDecode(loginResponse.body.toString());
        await DataManager.GetGamesProgress();
        Debug.LogResponse(DataManager.UserJson);
        prefs.setString("username", username);
        prefs.setString("password", username);
        prefs.setBool("googleLogin", true);
        return 0;
      }catch(e){
        return 5;
      }
    } catch (e) {
      Debug.LogError("Error while login $e");
    }

    return 1;
  }
}
