import isEmpty from 'lodash/isEmpty';
import { AuthProvider, OnErrorResponse } from "@refinedev/core";
import { IUser } from "../interfaces";

const API_URL = "/api/v1";
const LOCAL_STORAGE_USER_KEY = "rpk-gui-current-user";
const GOOGLE_SCOPES = { "scopes": "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email" };
const DISCORD_SCOPES = { "scopes": "identify email" }

export const authProvider: AuthProvider = {
  login: async ({ providerName, email, password }) => {
    const to_param = findGetParameter("to");
    if (providerName) {
      let scope = {};
      if (providerName === "google") {
        scope = GOOGLE_SCOPES;
      } else if (providerName === "discord") {
        scope = DISCORD_SCOPES;
      }
      const params = new URLSearchParams(scope);
      const url = `${API_URL}/auth/${providerName}/authorize?${params.toString()}`;
      const response = await fetch(url, { method: "GET", },);
      const body = await response.json();

      if (to_param) {
        localStorage.setItem("redirect_after_login", to_param);
      }
      window.location.href = body.authorization_url;
      return { success: true };
    } else if (email !== undefined && password !== undefined) {
      const params = new URLSearchParams({"grant_type": "password", "username": email, "password": password});
      const response = await fetch(
        `${API_URL}/auth/login`,
        {
          method: "POST",
          body: params.toString(),
          headers: { "Content-Type": "application/x-www-form-urlencoded", },
        },
      );
      if (response.status >= 200 && response.status < 300) {
        const response = await fetch(`${API_URL}/users/me`);
        const user = await response.json();
        store_user(user);

        return { success: true };
      }
    }

    return { success: false };
  },
  logout: async () => {
    const response = await fetch(`${API_URL}/auth/logout`, { method: "POST" });
    if (response.status == 204 || response.status == 401) {
      forget_user();
      return { success: true };
    }
    return { success: false };
  },
  check: async () => {
    if (get_user() == null) {
      return {
        authenticated: false,
        redirectTo: "/login",
        logout: true
      }
    }
    return { authenticated: true };
  },
  getIdentity: async (): Promise<IUser> => {
    const user = get_user();
    if (user !== null && !isEmpty(user)) {
      return user;
    }

    const response = await fetch(`${API_URL}/users/me`);
    if (response.status < 200 || response.status > 299) {
      return null;
    }
    const user_data = await response.json();
    store_user(user_data)

    return user_data;
  },
  register: async (params) => {
    const response = await fetch(`${API_URL}/register`, {
      method: "POST",
      body: JSON.stringify(params),
      headers: {
        "Content-Type": "application/json",
      },
    });

    if (response.status == 201) {
      return { success: true, redirectTo: "/", };
    }

    return {
      success: false,
      error: {
        message: "Register failed",
        name: "Invalid email or password",
      },
    };
  },
  forgotPassword: async (params) => {
    const response = await fetch(`${API_URL}/users/forgot-password`, {
      method: "POST",
      body: JSON.stringify(params),
      headers: {
        "Content-Type": "application/json",
      },
    });

    if (response.status == 202) {
      return { success: true, redirectTo: "/", };
    }

    return { success: false, };
  },
  updatePassword: async (params) => {
    if (params.token !== undefined) {
      const response = await fetch(`${API_URL}/users/reset-password`, {
        method: "POST",
        body: JSON.stringify({
          password: params.password,
          token: params.token,
        }),
        headers: {
          "Content-Type": "application/json",
        },
      });

      if (response.status == 200) {
        return { success: true, redirectTo: "/", };
      }
    }

    return { success: false, };
  },
  getPermissions: async () => { throw new Error("Not implemented"); },
  onError: async (error) => {
    if (error?.status === 401) {
      forget_user();
      return {
        redirectTo: "/login",
        logout: true,
        error: { message: "Authentication required" },
      } as OnErrorResponse;
    }
    else if (error?.status === 403) {
      return {
        error: { message: "Insufficient credentials" },
      } as OnErrorResponse;
    }
    return {
        error: { message: "Unexpected authentication error" },
    } as OnErrorResponse;
  },
};

function store_user(user: any) {
  localStorage.setItem(LOCAL_STORAGE_USER_KEY, JSON.stringify(user));
}

function get_user() {
  const user_string = localStorage.getItem(LOCAL_STORAGE_USER_KEY)
  if (user_string == null) {
    return null
  }
  return JSON.parse(user_string);
}

function forget_user() {
  localStorage.removeItem(LOCAL_STORAGE_USER_KEY);
}

export function empty_user() {
  store_user({})
}

function findGetParameter(parameterName: string) {
    let result = null, tmp = [];
    location.search.substr(1).split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}
