import superagentPromise from 'superagent-promise';
import _superagent from 'superagent';
import promise from 'promise'
import configStore from '../../../kara-search/stores/configStore'

const Promise = promise;
const superagent = superagentPromise(_superagent, Promise);

const handleErrors = err => {
  if (err && err.response && err.response.status === 401) {
    // login out ?
  }
  return err;
};

const responseBody = res => (res.body || JSON.parse(res.text));

const tokenPlugin = req => {
  if (configStore.config.token) {
    req.set('authorization', `bearer ${configStore.config.token}`);
  }
};

const getUrl = (url, version) => {
  return `${configStore.config.apiRoot}/${version ? version : configStore.config.apiVersion}${url}`
}

const getLanguage = () => {
  return configStore.config.language === 'en' ? 'en' : 'cn'
}

//系统请求头
const sysHeader = req => {
  if (configStore.config.token) {
    req.set('authorization', `bearer ${configStore.config.token}`);
    req.set('language', getLanguage());
    req.set('platform', 'web');
  }
};

const requests = {
  get: (url, version) =>
      superagent
          .get(getUrl(url, version))
          .use(tokenPlugin)
          .end(handleErrors)
          .then(responseBody),
  post: (url, body, version) =>
      superagent
          .post(getUrl(url, version), body)
          .use(tokenPlugin)
          .end(handleErrors)
          .then(responseBody),
  search: (url, body, version) =>
      superagent
          .post(getUrl(url, version), body)
          .use(tokenPlugin)
          .end(handleErrors)
          .then(res => {
            return {...res.body, kw: body.keywords} || {...JSON.parse(res.text), kw: body.keywords}
          }),
  sys: (url, body, version) =>
      superagent
          .post(getUrl(url, version), body)
          .use(sysHeader)
          .end(handleErrors)
          .then(res => {
            return {...res.body, kw: body.keywords} || {...JSON.parse(res.text), kw: body.keywords}
          })
};

const search = {
  all001: (keywords,from,to, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/search/global/all', {keywords,from,to, language, isHighlight, platform}),
  msgGroup001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/search/global/channel/list', {keywords, pageInfo, language, isHighlight, platform}),
  msgChat001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/search/global/chatRecord/list', {keywords, pageInfo, language, isHighlight, platform}),
  msgFile001: (keywords, pageInfo, language = getLanguage(), isHighlight = false, platform = 'web') =>
      requests.search('/search/file/gatherFile', {keywords, language, pageInfo, isHighlight, platform}),
  address001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/search/mobileGlobal/user/list', {keywords, language, pageInfo, isHighlight, platform}),
  friendCircle001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/search/global/moment/list', {keywords, language, pageInfo, isHighlight, platform}),
  knowledge001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/ses/global/itsmFAQ/list', {keywords, language, pageInfo, isHighlight, platform}),
  news001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/search/global/information/list', {keywords, language, pageInfo, isHighlight, platform}),
  sys: (keywords, isHighlight = true) =>
      requests.sys('/webhook/apps.search', {keywords, isHighlight}),
  todo001: (keywords, pageInfo, language = getLanguage(), isHighlight = true, platform = 'web') =>
      requests.search('/ses/global/todoTask/list', {keywords, language, pageInfo, isHighlight, platform}),
  userRecord001: (keywords,currentUserSize,globalUserSize) =>
      requests.search('/ses/user/behavior/record', {keywords,currentUserSize,globalUserSize}),
  delRecord: (keywords) =>
      requests.search('/ses/user/behavior/remove', {keywords}),

};


export default {
  search,
};
