import debug from 'debug';
import reqwest from 'reqwest';
import { defaults } from 'lodash';
import { Map as makeMap, List } from 'immutable';
import stableStringify from 'json-stable-stringify';

import {
  receiveError,
  receiveNodeDetails,
  receiveNotFound, receiveNodesForTopology, receiveNodes,
} from '../actions/app-actions';

import { getCurrentTopologyUrl } from './topology-utils';
import { layersTopologyIdsSelector } from '../selectors/resource-view/layout';
import { activeTopologyOptionsSelector } from '../selectors/topology';
import getToken from './get-token'
const log = debug('scope:web-api-utils');
var apiRoot = process.env.API_ROOT;  //线上打开


//本地测试时候用；
// var apiRoot = 'http://observe-server.cestong.com.cn'

// 测试环境的时候
// var apiRoot='http://observe-front.cestong.com.cn/re'





const updateFrequency = '5s';
const csrfToken = (() => {
  // Check for token at window level or parent level (for iframe);
  /* eslint-disable no-underscore-dangle */
  const token = typeof window !== 'undefined'
    ? window.__WEAVEWORKS_CSRF_TOKEN || window.parent.__WEAVEWORKS_CSRF_TOKEN
    : null;
  /* eslint-enable no-underscore-dangle */
  if (!token || token === '$__CSRF_TOKEN_PLACEHOLDER__') {
    // Authfe did not replace the token in the static html.
    return null;
  }

  return token;
})();

export function buildUrlQuery(params = makeMap(), state = null) {
  // Attach the time travel timestamp to every request to the backend.
  if (state) {
    params = params.set('timestamp', state.get('pausedAt'));
  }

  // Ignore the entries with values `null` or `undefined`.
  return params.map((value, param) => {
    if (value === undefined || value === null) return null;
    if (List.isList(value)) {
      value = value.join(',');
    }
    return `${param}=${value}`;
  }).filter(s => s).join('&');
}

export function basePath(urlPath) {
  //
  // "/scope/terminal.html" -> "/scope"
  // "/scope/" -> "/scope"
  // "/scope" -> "/scope"
  // "/" -> ""
  //
  const parts = urlPath.split('/');
  // if the last item has a "." in it, e.g. foo.html...
  if (parts[parts.length - 1].indexOf('.') !== -1) {
    return parts.slice(0, -1).join('/');
  }
  return parts.join('/').replace(/\/$/, '');
}

export function basePathSlash(urlPath) {
  //
  // "/scope/terminal.html" -> "/scope/"
  // "/scope/" -> "/scope/"
  // "/scope" -> "/scope/"
  // "/" -> "/"
  //
  return `${basePath(urlPath)}/`;
}

// TODO: This helper should probably be passed by the 'user' of Scope,
// i.e. in this case Weave Cloud, rather than being hardcoded here.
export function getApiPath(pathname = window.location.pathname) {
  if (process.env.SCOPE_API_PREFIX) {
    // Extract the instance name (pathname in WC context is of format '/:instanceId/explore').
    const instanceId = pathname.split('/')[1];
    return basePath(`${process.env.SCOPE_API_PREFIX}/app/${instanceId}`);
  }

  return basePath(pathname);
}

export function getReportUrl(timestamp) {
  // return `${getApiPath()}/api/report?${buildUrlQuery(makeMap({ timestamp }))}`;  //上线用
  return `${apiRoot}/api/report?${buildUrlQuery(makeMap({ timestamp }))}`;   //本地用
}

export function topologiesUrl(state) {
  const activeTopologyOptions = activeTopologyOptionsSelector(state);
  const optionsQuery = buildUrlQuery(activeTopologyOptions, state);
  // return `${getApiPath()}/api/topology?${optionsQuery}`; //上线用
  return `${apiRoot}/api/topology?${optionsQuery}`; //本地用
}

//url 解析成对象
function parseQueryString(url){
  var json = {};
  var arr = url.substr(url.indexOf('?') + 1).split('&');
  arr.forEach(item=>{
      var tmp = item.split('=');
          json[tmp[0]] = tmp[1];
  });
  return json;
}
//获取拓扑数据传的参数
let queryData ={
  start_time:Math.round((new Date().getTime())/1000 - (1*60)),
  end_time:Math.round(new Date().getTime()/1000),
  app_alias:"opentelemetry-demo",
  live:false
}

//上线打开开始
function getParamsObject() {
  var strr = parent.location.href;   //上线做为iframe嵌套时使用
  let param = parseQueryString(strr);  //全链路需要的参数多，因此解析成对象形式
  if(parseInt(param.start_time)!=0 && param.start_time != 'NaN'){ //上线时打开
    queryData.start_time = parseInt(param.start_time);
  } else{
    queryData.start_time = Math.round((new Date().getTime())/1000 - (1*60))
  }

  if(parseInt(param.end_time) !=0 && param.end_time != 'NaN'){ //上线时打开web-api
    queryData.end_time=parseInt(param.end_time);
  } else{
    queryData.end_time=Math.round(new Date().getTime()/1000);
  }
  if(param.app_alias != undefined){
    queryData.app_alias=param.app_alias; //上线时打开
  } else{
    queryData.app_alias= 'OBSERVE'
  }

  if(param.live != undefined){
    queryData.live = param.live;
  } else{
    queryData.live = false;
  }
}
getParamsObject()
//上线打开结束


//自己公司新加的替代了topologiesUrl，假如要复原得全局搜索topologiesUrl
export function topologiesUrlApi(state) {
  const activeTopologyOptions = activeTopologyOptionsSelector(state);
  const optionsQuery = buildUrlQuery(activeTopologyOptions, state);
  let tempAlias = ''
  let tempStart = ''
  let tempEnd = ''
  // window.top == window  true 自己本身没有被嵌套
  if(window.top == window ){
    tempAlias = queryData.app_alias
    tempStart = queryData.start_time
    tempEnd = queryData.end_time

  } else { // false 被嵌套
    const appsItem = JSON.parse(parent.localStorage.appsItem)
    tempAlias = appsItem.alias
    const global_times = JSON.parse(parent.localStorage.topoGlobalTimes) || {}
    if (global_times) {
      tempStart = global_times.startTime
      tempEnd = global_times.endTime
    }
  }
  getParamsObject()
  return `${apiRoot}/api/v1/topology/${queryData.app_alias}?start_time=${tempStart}&end_time=${tempEnd}&live=${queryData.live}` //本地测试好的url地址
}


//wsUrl携带参数的例子
// const wsUrl = 'ws://observe-server.cestong.com.cn/api/v1/ws/topology/services/ws?t=5s&app_alias=opentelemetry-demo'
//本地环境用
// const wsUrl ='ws://observe-server.cestong.com.cn'
// 测试环境用
// const wsUrl='ws://observe-front.cestong.com.cn'

//上线时打开
const wsUrl = `ws://${parent.location.hostname}`


export function getWebsocketUrl(host = window.location.host, pathname = window.location.pathname) {
  const wsProto = window.location.protocol === 'https:' ? 'wss' : 'ws';
  return `${wsProto}://${host}${getApiPath(pathname)}`;
}

export function buildWebsocketUrl(topologyUrl, topologyOptions = makeMap(), state) {
  topologyOptions = topologyOptions.set('t', updateFrequency);
  const optionsQuery = buildUrlQuery(topologyOptions, state);
  // return `${getWebsocketUrl()}${topologyUrl}/ws?${optionsQuery}`;   //原来系统本身的
  let tempAlias = ''
  let tempStart = ''
  let tempEnd = ''
  // window.top == window  true 自己本身没有被嵌套
  if(window.top == window){
    tempAlias = queryData.app_alias
  } else { // false 被嵌套
    const appsItem = JSON.parse(parent.localStorage.appsItem)
    tempAlias = appsItem.alias
    const global_times = JSON.parse(parent.localStorage.topoGlobalTimes) || {}
    if (global_times) {
      tempStart = global_times.startTime
      tempEnd = global_times.endTime
    }
  }
  getParamsObject()
  return `${wsUrl}${topologyUrl}/ws?t=5s&app_alias=${queryData.app_alias}&start_time=${tempStart}&end_time=${tempEnd}&live=${queryData.live}`;
}

/**
  * XHR wrapper. Applies a CSRF token (if it exists) and content-type to all requests.
  * Any opts that get passed in will override the defaults.
  */
export function doRequest(opts) {

  const config = defaults(opts, {
    contentType: 'application/json',
    type: 'json'
  });
  if (csrfToken) {
    config.headers = Object.assign({}, config.headers, { 'X-CSRF-Token': csrfToken, 'Authorization': getToken });
  }
  if(getToken) {
    config.headers = Object.assign({}, config.headers, {'Authorization': getToken });
  }

  return reqwest(config);
}

/**
 * Does a one-time fetch of all the nodes for a custom list of topologies.
 */
function getNodesForTopologies(state, dispatch, topologyIds, topologyOptions = makeMap()) {
  // fetch sequentially
  state.get('topologyUrlsById')
    .filter((_, topologyId) => topologyIds.contains(topologyId))
    .reduce(
      (sequence, topologyUrl, topologyId) => sequence
        .then(() => {
          const optionsQuery = buildUrlQuery(topologyOptions.get(topologyId), state);
          return doRequest({ url: `${getApiPath()}${topologyUrl}?${optionsQuery}` });
        })
        .then(json => dispatch(receiveNodesForTopology(json.nodes, topologyId))),
      Promise.resolve()
    );
}

export function getNodesOnce(getState, dispatch) {
  const state = getState();
  const topologyUrl = getCurrentTopologyUrl(state);
  const topologyOptions = activeTopologyOptionsSelector(state);
  const optionsQuery = buildUrlQuery(topologyOptions, state);
  const url = `${getApiPath()}${topologyUrl}?${optionsQuery}`;
  doRequest({
    error: (req) => {
      log(`Error in nodes request: ${req.responseText}`);
      dispatch(receiveError(url));
    },
    success: (res) => {
      dispatch(receiveNodes(res.nodes));
    },
    url
  });
}

/**
 * Gets nodes for all topologies (for search).
 */
export function getAllNodes(state, dispatch) {
  const topologyOptions = state.get('topologyOptions');
  const topologyIds = state.get('topologyUrlsById').keySeq();
  getNodesForTopologies(state, dispatch, topologyIds, topologyOptions);
}

/**
 * One-time update of all the nodes of topologies that appear in the current resource view.
 * TODO: Replace the one-time snapshot with periodic polling.
 */
export function getResourceViewNodesSnapshot(state, dispatch) {
  const topologyIds = layersTopologyIdsSelector(state);
  getNodesForTopologies(state, dispatch, topologyIds);
}

export function getNodeDetails(getState, dispatch) {
  const state = getState();
  const nodeMap = state.get('nodeDetails');
  const topologyUrlsById = state.get('topologyUrlsById');
  const currentTopologyId = state.get('currentTopologyId');
  const requestTimestamp = state.get('pausedAt');

  // get details for all opened nodes
  const obj = nodeMap.last();
  if (obj && topologyUrlsById.has(obj.topologyId)) {
    const topologyUrl = topologyUrlsById.get(obj.topologyId);
    let urlComponents = [getApiPath(), topologyUrl, '/', encodeURIComponent(obj.id)];

    // Only forward filters for nodes in the current topology.
    const topologyOptions = currentTopologyId === obj.topologyId
      ? activeTopologyOptionsSelector(state) : makeMap();

    const query = buildUrlQuery(topologyOptions, state);
    if (query) {
      urlComponents = urlComponents.concat(['?', query]);
    }
    const url = urlComponents.join('');

    doRequest({
      error: (err) => {
        log(`Error in node details request: ${err.responseText}`);
        // dont treat missing node as error
        if (err.status === 404) {
          dispatch(receiveNotFound(obj.id, requestTimestamp));
        } else {
          dispatch(receiveError(topologyUrl));
        }
      },
      success: (res) => {
        // make sure node is still selected
        if (nodeMap.has(res.node.id)) {
          dispatch(receiveNodeDetails(res.node, requestTimestamp));
        }
      },
      url
    });
  } else if (obj) {
    log('No details or url found for ', obj);
  }
}

export function doResizeTty(pipeId, control, cols, rows) {
  const url = `${getApiPath()}/api/control/${encodeURIComponent(control.probeId)}/`
    + `${encodeURIComponent(control.nodeId)}/${control.id}`;

  return doRequest({
    data: stableStringify({ height: rows.toString(), pipeID: pipeId, width: cols.toString() }),
    method: 'POST',
    url,
  })
    .fail((err) => {
      log(`Error resizing pipe: ${err}`);
    });
}


export function deletePipe(pipeId, dispatch) {
  const url = `${getApiPath()}/api/pipe/${encodeURIComponent(pipeId)}`;
  doRequest({
    error: (err) => {
      log(`Error closing pipe:${err}`);
      dispatch(receiveError(url));
    },
    method: 'DELETE',
    success: () => {
      log('Closed the pipe!');
    },
    url
  });
}
