import { isDev } from './NodeEnv';
import * as H from 'history';

const URL = {
  HOME: '/',
  EXPLORE: '/explore',
  ACTIVITY: '/activity',
  LOGIN: '/login',
  REGISTER: '/register',
  CREATE: '/create',
  RESALE: '/resale/:id',
  SALE: '/sale/:id',
  TOKEN: '/token/:id',
  PROFILE_COLLECTION: '/collection/:name',
  COLLECTION_EDIT: '/collection-edit/:name',
  PROFILE_USER: '/profile/:name',
  USER_EDIT: '/profile/edit',
  GUIDE: '/guide',
  GUIDE_DETAIL: '/guide/:id',
  LOGIN_CALLBACK: '/callback/:type',
  ERROR: '/error',
  RANKING: '/ranking',
};

const SEARCH_PARAM = {
  SEARCH_TEXT: 'searchText',
  RETURN_URL: 'returnUrl'
};

// router
const getSaleUrl = (id: number) => `/sale/${id}`;
const getResaleUrl = (id: number) => `/resale/${id}`;
const getTokenUrl = (id: number) => `/token/${id}`;
const getProfileCollectionUrl = (name: string) => `/collection/${name}`;
const getCollectionEditUrl = (name: string) => `/collection-edit/${name}`;
const getProfileUserUrl = (name: string) => `/profile/${name}`;
const getGuideDetailUrl = (id: string) => `/guide/${id}`;
const getSearchResultUrl = (keyword: string) => `${URL.EXPLORE}?${SEARCH_PARAM.SEARCH_TEXT}=${keyword}`;
const getLoginUrl = (returnUrl: string) => `${URL.LOGIN}?${SEARCH_PARAM.RETURN_URL}=${returnUrl}`;

const isTokenPage = (location?: H.Location) => (location || window.location).pathname.split('/')[1] === 'token';
const isSalePage = (location?: H.Location) => (location || window.location).pathname.split('/')[1] === 'sale';
const isResalePage = (location?: H.Location) => (location || window.location).pathname.split('/')[1] === 'resale';
const isProfileCollection = (location?: H.Location) => (location || window.location).pathname.split('/')[1] === 'collection';
const isProfileCollectionEdit = (location?: H.Location) => (location || window.location).pathname.split('/')[1] === 'collection-edit';
const isProfileUserPage = (location?: H.Location) => (location || window.location).pathname.split('/')[1] === 'profile';
const isExplorePage = (location?: H.Location) => (location || window.location).pathname === URL.EXPLORE;
const isActivityPage = (location?: H.Location) => (location || window.location).pathname === URL.ACTIVITY;
const isGuidePage = (location?: H.Location) => (location || window.location).pathname === URL.GUIDE;

const getId = () => Number(window.location.pathname.split('/')[2]);

// 외부링크
const getTransactionUrl = (hash: string) => {
  const url = isDev ? 'https://rinkeby.etherscan.io/tx' : 'https://etherscan.io/tx';
  return `${url}/${hash}`;
};

const registerArtistUrl = 'https://docs.google.com/forms/d/1fFzSS6n6OyTZofgnb4gMKj4cDbG2YR9PbNXgJr1fJgk/viewform?edit_requested=true';

export default {
  ...URL,
  SEARCH_PARAM,
  getSaleUrl,
  getResaleUrl,
  getTokenUrl,
  getProfileCollectionUrl,
  getCollectionEditUrl,
  getProfileUserUrl,
  getGuideDetailUrl,
  getSearchResultUrl,
  getLoginUrl,

  isTokenPage,
  isSalePage,
  isResalePage,
  isProfileCollection,
  isProfileCollectionEdit,
  isProfileUserPage,
  isExplorePage,
  isActivityPage,
  isGuidePage,

  getId,

  getTransactionUrl,
  registerArtistUrl,
};
