import { AuthBindings } from '@refinedev/core';
import { getApiUrl } from 'helpers/utils';
import { requestHeaders } from 'helpers/login';
import memoize from 'lodash.memoize';

const getUserInfoOnce = memoize(async () => {
  const res = await fetch(`${getApiUrl()}/userinfo`, { headers: requestHeaders() });
  const user = await res.json();
  return user;
});

export const authProvider: AuthBindings = {
  login: async ({ providerName, email }) => {
    if (providerName === 'google') {
      window.location.href = 'https://accounts.google.com/o/oauth2/v2/auth';
      return {
        success: true,
      };
    }

    if (providerName === 'github') {
      window.location.href = 'https://github.com/login/oauth/authorize';
      return {
        success: true,
      };
    }

    if (email) {
      localStorage.setItem('email', email);
      return {
        success: true,
        redirectTo: '/',
      };
    }

    return {
      success: false,
      error: {
        message: 'Login failed',
        name: 'Invalid email or password',
      },
    };
  },
  register: async ({ email, password }) => {
    if (email && password) {
      return {
        success: true,
        redirectTo: '/',
      };
    }
    return {
      success: false,
      error: {
        message: 'Register failed',
        name: 'Invalid email or password',
      },
    };
  },
  updatePassword: async ({ password }) => {
    if (password) {
      //we can update password here
      return {
        success: true,
        redirectTo: '/login',
      };
    }
    return {
      success: false,
      error: {
        message: 'Update password failed',
        name: 'Invalid password',
      },
    };
  },
  forgotPassword: async ({ email }) => {
    if (email) {
      //we can send email with forgot password link here
      return {
        success: true,
        redirectTo: '/login',
      };
    }
    return {
      success: false,
      error: {
        message: 'Forgot password failed',
        name: 'Invalid email',
      },
    };
  },
  logout: async () => {
    localStorage.removeItem('email');
    return {
      success: true,
      redirectTo: '/',
    };
  },
  onError: async (error) => {
    console.error(error);
    return { error };
  },
  check: async () => {
    return localStorage.getItem('email')
      ? { authenticated: true }
      : {
          authenticated: false,
          redirectTo: '/login',
          error: {
            message: 'Check failed',
            name: 'Not authenticated',
          },
        };
  },
  getPermissions: async () => ['admin'],
  getIdentity: async () => {
    const user = await getUserInfoOnce();
    return {
      id: user.userid,
      name: user.username,
      avatar: `https://dayu.woa.com/avatars/${user.username}/avatar.jpg`,
      ...user,
    };
  },
};
