import axios from "axios";
import AsyncStorage from "@react-native-async-storage/async-storage";
import {
  ActivationParams,
  LoginParams,
  OnboardingParams,
  RegistrationParams,
} from "../../interfaces/Interfaces";

let debug = true;
export let backendURL = "";
if (debug) {
  backendURL = "http://10.0.10.8:8000";
} else {
  backendURL = "https://keannu125.pythonanywhere.com";
}

const instance = axios.create({
  baseURL: backendURL,
  timeout: 1000,
});

// App APIs

// Token Handling
export async function getAccessToken() {
  const accessToken = await AsyncStorage.getItem("access_token");
  return accessToken;
}

export async function getRefreshToken() {
  const refreshToken = await AsyncStorage.getItem("refresh_token");
  return refreshToken;
}

export async function setAccessToken(access: string) {
  await AsyncStorage.setItem("access_token", access);
  return true;
}

export async function setRefreshToken(refresh: string) {
  await AsyncStorage.setItem("refresh_token", refresh);
  return true;
}

// User APIs
export function UserRegister(register: RegistrationParams) {
  console.log(JSON.stringify(register));
  return instance
    .post("/api/v1/accounts/users/", register)
    .then(async (response) => {
      return [true, response.status];
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      return [false, error_message];
    });
}

export function UserLogin(user: LoginParams) {
  return instance
    .post("/api/v1/accounts/jwt/create/", user)
    .then(async (response) => {
      /*console.log(
        "Access Token:",
        response.data.access,
        "\nRefresh Token:",
        response.data.refresh
      );*/
      setAccessToken(response.data.access);
      setRefreshToken(response.data.refresh);
      return [true];
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      return [false, error_message];
    });
}

export async function TokenRefresh() {
  const refresh = await getRefreshToken();
  // console.log("Refresh token", refresh);
  return instance
    .post("/api/v1/accounts/jwt/refresh/", {
      refresh: refresh,
    })
    .then(async (response) => {
      setAccessToken(response.data.access);
      /*console.log(
        "Token refresh success! New Access Token",
        response.data.access
      );*/
      return true;
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      console.log("Token Refresh error:", error_message);
      return false;
    });
}
export async function UserInfo() {
  const accessToken = await getAccessToken();
  return instance
    .get("/api/v1/accounts/users/me/", {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    })
    .then((response) => {
      // console.log(JSON.stringify(response.data));
      return [true, response.data];
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      return [false, error_message];
    });
}

export function UserActivate(activation: ActivationParams) {
  return instance
    .post("/api/v1/accounts/users/activation/", activation)
    .then(async (response) => {
      return true;
    })
    .catch((error) => {
      return false;
    });
}

// App APIs

export async function GetCourses() {
  const accessToken = await getAccessToken();
  return instance
    .get("/api/v1/courses/", {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    })
    .then((response) => {
      // console.log(JSON.stringify(response.data));
      return response.data;
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      console.log("Error getting courses", error_message);
      return false;
    });
}

export async function GetSemesters() {
  const accessToken = await getAccessToken();
  return instance
    .get("/api/v1/semesters/", {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    })
    .then((response) => {
      // console.log(JSON.stringify(response.data));
      return response.data;
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      console.log("Error getting semesters", error_message);
      return false;
    });
}

export async function GetYearLevels() {
  const accessToken = await getAccessToken();
  return instance
    .get("/api/v1/year_levels/", {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    })
    .then((response) => {
      // console.log(JSON.stringify(response.data));
      return response.data;
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      console.log("Error getting year levels", error_message);
      return false;
    });
}

export async function OnboardingUpdateStudentInfo(info: OnboardingParams) {
  const accessToken = await getAccessToken();
  const headers = {
    Authorization: `Bearer ${accessToken}`,
  };
  return instance
    .patch("/api/v1/accounts/users/me/", info, { headers })
    .then((response) => {
      console.log(JSON.stringify(response.data));
      return [true, response.data];
    })
    .catch((error) => {
      let error_message = "";
      if (error.response) error_message = error.response.data;
      else error_message = "Unable to reach servers";
      console.log("Error updating onboarding info", error_message);
      return [false, error_message];
    });
}
