import { jsonp, request } from '@wisdom-utils/utils';
import * as constants from '../../constants';
/**
 *  兼容老接口
 */
const transformGateWay = url => {
  if (!/^(cityinterface|CityInterface|CityServer)/) {
    return url.replace(/GeteWay/, '');
  }
};

export const API = {
  AUTHORIZATION_TOKEN: '/PandaCore/Identity/AuthorizationToken', // 授权验证，不需要走网关加前缀
  GET_GATEWAY_CONFIG: '/PandaOMS/OMS/HostManager/GetGateWay',
  GET_CONFIG: '/CityInterface/rest/services.svc/GetConfig',
  GENERATE_TOKEN: '/cityinterface/rest/services.svc/generatetoken',
  GENERATE_GATEWAT_TOKEN: '/PandaCore/GCK/Basis/GenerateToken',
  GENERATE_IOT_TOKEN: 'cityinterface/rest/services.svc/generateGCKToken',
  GENERATE_QRCODE: 'cityinterface/rest/services.svc/generatetokenByqrcode',
  GENERATE_TOKEN_CHANGE: '/cityinterface/rest/services.svc/generatetokenquick',
  GET_WEB_SITE_CONFIG: '/CityInterface/rest/services.svc/GetWebSiteConfig',
  GET_WEB_SITE_CONFIG_GEWAY: '/PandaCore/GCK/Basis/GetWebSiteConfig',
  GET_USER_INFO: '/CityInterface/rest/services.svc/getUserInfo',
  GET_GATEWAY_USER_INFO: '/PandaCore/GCK/Basis/GetUserInfo',
  GET_OA: '/CityInterface/rest/services/OA.svc/getLoginInfo',
  GET_LOGS: '/CityInterface/rest/services/portal.svc/OMMonitor/SaveLoginInfo',
  GET_CITY:
    process.env.NODE_ENV !== 'production'
      ? '/cityjson?ie=utf-8'
      : 'https://pv.sohu.com',
  GET_ALL_GROUPS_INFO_FORUSER:
    '/CityInterface/rest/Services/Portal.svc/AuthorityManage/GetAllGroupsInfoForUser',
  GET_WEATHER: '/CityInterface/rest/services/CountyProduct.svc/GetWeather',
  SEND_MESSAGE_CODE:
    'CityInterface/rest/services/portal.svc/SendMessVerificationCode',
  CHANGE_PASSWORD: 'cityinterface/rest/services.svc/changepassword',
  IOT_CHANGE_PASSWORD:
    'CityInterface/rest/services/OMS.svc/U_UpdatePasswordQuickGCK',
  FILE_DOWNLOAD: '/cityinterface/rest/services/filedownload.svc/download',
  GET_VERSION: '/CityInterface/rest/services/OMs.svc/U_GetVersion',
  UPDATE_AVATAR: '/CityInterface/rest/services/OMs.svc/U_EditUser',
  UPLOAD_FILE_URL:
    '/cityinterface/rest/services/filedownload.svc/uploadfile/个人信息/{path}/{filename}',
  AVATAR_FILE_URL: '/cityinterface/rest/services/filedownload.svc/download',
  GET_SENSOR_TYPE: '/PandaCore/GCK/Sensor/GetSensorType', // 获取传感器类型
  GET_SYSTEM_CONFIGURATION: '/PandaOMS/OMS/SysConfiguration', // 获取系统配置
  GET_INFO_QYWX: '/CityInterface/rest/services/OA.svc/WeChatScanQRCode',
  PV_LOGS: '/CityInterface/rest/services/portal.svc/OMMonitor/SavePVLogInfo',
  LOGIN_LOGS:
    'CityInterface/rest/Services/IOTPlatform.svc/WisdomOMS/SaveLoginInfo',
  GET_PAGE_PART_INFO:
    '/CityInterface/rest/services/CountyProduct.svc/AccountManage/GetHomePagePartInfo',
  SAVE_Page_PART_INFO:
    '/CityInterface/rest/services/CountyProduct.svc/AccountManage/SaveHomePage',
};

const services = {
  authorizationToken: {
    url: API.AUTHORIZATION_TOKEN,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  getWateWayConfig: {
    url: API.GET_GATEWAY_CONFIG,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  queryConfig: {
    url: API.GET_CONFIG,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  generatetokenByqrcode: {
    url: API.GENERATE_QRCODE,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  getWebSiteConfig: {
    url: () =>
      window.globalConfig &&
      window.globalConfig &&
      window.globalConfig.hasGateWay
        ? API.GET_WEB_SITE_CONFIG_GEWAY
        : API.GET_WEB_SITE_CONFIG,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  getUserInfo: {
    url: () =>
      window.globalConfig && window.globalConfig.hasGateWay
        ? API.GET_GATEWAY_USER_INFO
        : API.GET_USER_INFO,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  getOA: {
    url: API.GET_OA,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  getInfo: {
    url: API.GET_INFO_QYWX,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  writeLogs: {
    url: API.GET_LOGS,
    method: constants.REQUEST_METHOD_POST,
    type: constants.REQUEST_HTTP,
  },

  getCity: {
    url: API.GET_CITY,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  generateTokenQuick: {
    url: API.GENERATE_TOKEN_CHANGE,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  getAllGroupsInfoForUser: {
    url: API.GET_ALL_GROUPS_INFO_FORUSER,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  getWeather: {
    url: API.GET_WEATHER,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },

  sendMessVerificationCode: {
    url: API.SEND_MESSAGE_CODE,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  getVersion: {
    url: API.GET_VERSION,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  updateAvatar: {
    url: API.UPDATE_AVATAR,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  generateToken: {
    url: () =>
      window.globalConfig &&
      (window.globalConfig.loginTemplate === 'Dark - IOTMultiLogin.html' ||
        window.globalConfig.loginTemplate === '新春 - 智联.html')
        ? API.GENERATE_IOT_TOKEN
        : API.GENERATE_TOKEN,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  changePassword: {
    url: () =>
      window.globalConfig &&
      (window.globalConfig.loginTemplate === 'Dark - IOTMultiLogin.html' ||
        window.globalConfig.loginTemplate === '新春 - 智联.html')
        ? API.IOT_CHANGE_PASSWORD
        : API.CHANGE_PASSWORD,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  // Sensor 指标管理
  getSensorType: {
    url: API.GET_SENSOR_TYPE,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  sysConfiguration: {
    url: API.GET_SYSTEM_CONFIGURATION,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
  pvLogs: {
    url: API.PV_LOGS,
    method: constants.REQUEST_METHOD_POST,
    type: constants.REQUEST_HTTP,
  },
  loginLogs: {
    url: API.LOGIN_LOGS,
    method: constants.REQUEST_METHOD_POST,
    type: constants.REQUEST_HTTP,
  },
  getPagePartInfo: {
    url: API.GET_PAGE_PART_INFO,
    method: constants.REQUEST_METHOD_GET,
    type: constants.REQUEST_HTTP,
  },
};

export const searchAutoCity = keywords => {
  const url = 'https://restapi.amap.com/v3/assistant/inputtips';
  const options = {
    callback: `jsonp_${new Date().getTime()}`,
  };
  const params = Object.assign(
    {},
    {
      s: 'rsv3',
      key: 'e83f64300a2a55a33fa8e4ab9a46bca6',
      platform: 'JS',
      logversion: '2.0',
      csid: '5A098355-8164-41D5-A2DC-78E1471CCD28',
      appname: window.location.href,
      sdkversion: '1.4.15',
      keywords,
    },
  );
  return jsonp(url, params, options);
};

export const getUserInfo = params => {
  const url =
    window.globalConfig && window.globalConfig.hasGateWay
      ? API.GET_GATEWAY_USER_INFO
      : API.GET_USER_INFO;
  return request({
    url,
    method: constants.REQUEST_METHOD_GET,
    params,
    // params: param.query,
  });
};

export const getWebSiteConfig = params => {
  const url =
    window.globalConfig && window.globalConfig && window.globalConfig.hasGateWay
      ? API.GET_WEB_SITE_CONFIG_GEWAY
      : API.GET_WEB_SITE_CONFIG;
  return request({
    url,
    method: constants.REQUEST_METHOD_GET,
    params,
    // params: param.query,
  });
};

// 更新常用页
export const savePagePartInfo = param =>
  request({
    url: API.SAVE_Page_PART_INFO,
    method: constants.REQUEST_METHOD_POST,
    data: param.data,
    params: param.query,
  });

export default services;
