import useJwt from 'auth/jwt/coreUseJwt';

/**
 * Return if user is logged in
 * This is completely up to you and how you want to store the token in your frontend application
 * e.g. If you are using cookies to store the application please update this function
 */
// eslint-disable-next-line arrow-body-style
export const hostname = 'localhost';
const hostPort = '8090';
export const hostPath = `http://${hostname}:${hostPort}`;
export const apiPath = window.location.href.match("localhost:3000")?`${hostPath}/api`:(window.location.href.match(":3000")?  "http://"+window.location.hostname+":8090/api":`/api`);
export const paymentPath = window.location.href.match("localhost:3000")?`${hostPath}/payment`:`/payment`;

/**
 * Testing:
 *    Domain: apigw-isit.staging-eid.gov.hk
 *    URL: hk.gov.iamsmart.testapp://
 * 
 * Production
 *    Domain: apigw.iamsmart.gov.hk
 *    URL: hk.gov.iamsmart://
 */
export const iAmSmartPath = `https://apigw-isit.staging-eid.gov.hk`;
export const iAmSmartAppPath = `hk.gov.iamsmart.testapp://`;
export const clientId = "cf61fa7c121e4869966f69c8694b1cd2";

export const iAmSmartCallbackPath = () => {
  let hostname = window.location.hostname;
  if (hostname.match("pnspsuat")) {
    hostname = "pnspsuat.gld.gov.hk";
  }else{
    hostname = "pnspsdev.gld.gov.hk";
  }
  return hostname;
};

export const getNonce = () => {
  let hostname = window.location.hostname;
  if (hostname.match("localhost")) {
    hostname = "pnspsuat.gld.gov.hk";
  }
  return hostname;
};

export const getBowserType = () => {
  // console.log(navigator.userAgent)
  // const regex = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Mi|huawei|Opera Mini|SAMSUNG|Samsung|SGH-[I|N|T]|GT-[I|N]|SM-[A|N|P|T|Z]|SHV-E|SCH-[I|J|R|S]|SPH-L/i;
  // if(!regex.test(navigator.userAgent)) 
  if (navigator.userAgent.indexOf("Edg") != -1) {
    if (navigator.userAgent.match(/Android/i)) return "Android_Edge"
    if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Edge"
    return "PC_Browser"
  } else if (navigator.userAgent.indexOf("Chrome") != -1) {
    if (navigator.userAgent.match(/Android/i)) return "Android_Chrome"
    if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Chrome"
    return "PC_Browser"
  } else if (navigator.userAgent.indexOf("Safari") != -1) {
    if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Safari"
    return "PC_Browser"
  } else if (navigator.userAgent.indexOf("Firefox") != -1) {
    if (navigator.userAgent.match(/Android/i)) return "Android_Firefox"
    if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Firefox"
    return "PC_Browser"
  } else if (navigator.userAgent.match(/SAMSUNG|Samsung|SGH-[I|N|T]|GT-[I|N]|SM-[A|N|P|T|Z]|SHV-E|SCH-[I|J|R|S]|SPH-L/i)) {
    return "Android_Samsung"
  } else if (navigator.userAgent.match(/huawei/i)) {
    return "Android_Huawei"
  } else if (navigator.userAgent.match(/Mi/i)) {
    return "Android_Xiaomi"
  }
  return "PC_Browser";
}

export const isAppBowser = () => {
  return "PC_Browser" != getBowserType();
}


export const isUserLoggedIn = () => {
  return localStorage.getItem('userData') && localStorage.getItem(useJwt.jwtConfig.storageTokenKeyName);
};

export const getUserData = () => JSON.parse(localStorage.getItem('userData'));

/**
 * This function is used for demo purpose route navigation
 * In real app you won't need this function because your app will navigate to same route for each users regardless of ability
 * Please note role field is just for showing purpose it's not used by anything in frontend
 * We are checking role just for ease
 * NOTE: If you have different pages to navigate based on user ability then this function can be useful. However, you need to update it.
 * @param {String} userRole Role of user
 */
export const getHomeRouteForLoggedInUser = (userRole) => {
  if (userRole === 'admin') return '/';
  if (userRole === 'user') return '/';
  if (userRole === 'client') return { name: 'access-control' };
  return { name: 'auth-login' };
};
