import React from "react";
import md5 from 'md5';
import {Input} from "antd";
const { Search } = Input;

const $ = window.$;
const isDev = window.location.port == 3007;
const isdev2= window.location.hostname ==='www.educoder.net';
export const TEST_HOST = window.location.hostname ==="testforgeplus.educoder.net";

export function getImageUrl(path) {
	const local = 'https://testforgeplus.educoder.net';
	const normal = TEST_HOST ? "https://new-testali-cdn.educoder.net/images" : 'https://ali-cdn.educoder.net/images';
	const normalend = TEST_HOST ? 'https://new-testali-cdn.educoder.net' :'https://ali-cdn.educoder.net';
	if (isDev) {
		return `${local}/${path}`;
	}
	if(path.substr(0, 7) !== "/system"){
		if(path.substr(0, 7) !== "/images"){
    	return `${normal}${path}`;
		}else{
			return `${normalend}${path}`;
		}
	}
	return `${path}`;
}


export function openNewWindow(url) {
	let link = document.createElement('a')
	link.target = "_blank"
	document.body.appendChild(link)
	link.href = url
	let evt = document.createEvent("MouseEvents")
	evt.initEvent("click", false, false)
	link.dispatchEvent(evt)
	document.body.removeChild(link);
}

export function numFormat(num, digits){
	let d = digits || 1;
  var si = [
    { value: 1, symbol: "" },
    { value: 1E3, symbol: "k" },
    { value: 1E4, symbol: "W" }
  ];
  var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
  var i;
  for (i = si.length - 1; i > 0; i--) {
    if (num >= si[i].value) {
      break;
    }
  }
  return (num / si[i].value).toFixed(d).replace(rx, "$1") + si[i].symbol;
}

export function getImage(path) {
	const local = 'https://testforgeplus.trustie.net/';
	if(path.indexOf("http://")===-1){
		if (isDev) {
			return `${local}/images/${path}`
		}
		return `/${path}`;
	}else{
		return path;
	}
}

export function getcdnImageUrl(path) {
  const testlocal = 'https://testali-cdn.educoder.net'
  const  local='https://ali-cdn.educoder.net'
	let firstStr=path.substr(0,1);
	if (isdev2) {
		return `${local}/${path}`;
	}else{
		if(firstStr=="/"){
			return `${path}`
		}else{
			return `/${path}`
		}
	}
}

export function setcndImagesUrl(path){
	const  local='https://ali-cdn.educoder.net'
	let firstStr=path.substr(0,1);
	if(firstStr=="/"){
		if (!isdev2) {
			return `${path}`
		}
		return `${local}${path}`;
	}else{
		if (!isdev2) {
			return `/${path}`
		}
		return `${local}/${path}`;
	}
}



export function setImagesUrl(path){
	const testlocal = 'https://testali-cdn.educoder.net'
	 const  local='https://ali-cdn.educoder.net'
	let firstStr=path.substr(0,1);
	if(firstStr=="/"){
		if (!isdev2) {
			return `${path}`
		}
		return `${local}${path}`;
		// return isDev?`${testlocal}${path}`:`${path}`;
	}else{
		if (!isdev2) {
			return `/${path}`
		}
		return `${local}/${path}`;
		 //return isDev?`${testlocal}/${path}`:`/${path}`;
	}
}

export function getUrl(path, goTest) {
	const local = 'https://testforgeplus.trustie.net'
	if (isDev) {
		return `${local}${path?path:''}`
	}
	return `${path ? path: ''}`;
}

export function getUrlmys(path, goTest) {
	const local = 'https://test-jupyterweb.educoder.net'
	if (isDev) {
		return `${local}${path?path:''}`
	}
	return `${path ? path: ''}`;
}

export function getUrl2(path, goTest) {
  	const local = 'http://localhost:3000'
	if (isDev) {
		return `${local}${path?path:''}`
	}
	return `${path ? path: ''}`;
}
const newopens ="79e33abd4b6588941ab7622aed1e67e8";
let newtimestamp;


export function getmyUrl(geturl) {

	return `${getUrl()}${geturl}`;
}

export function getUploadActionUrl(path, goTest) {
	return `${getUrl()}/api/attachments.json`;
}

export function getUploadLogoActionUrl() {
	return `${getUrl()}/api/resumes/logo.json${isDev ?`?debug=${window._debugType || 'admin'}` : ""}`;
}

export function getUploadActionUrltwo(id) {
	return `${getUrlmys()}/api/shixuns/${id}/upload_data_sets.json${isDev ?`?debug=${window._debugType || 'admin'}` : ""}`
}

export function getUploadActionUrlthree() {
	return `${getUrlmys()}/api/jupyters/import_with_tpm.json${isDev ?`?debug=${window._debugType || 'admin'}` : ""}`
}

export function getupload_git_file(id) {
	return `${getUrlmys()}/api/shixuns/${id}/upload_git_file.json${isDev ?`?debug=${window._debugType || 'admin'}` : ""}`
}


export function getUploadActionUrlOfAuth(id) {
	return `${getUrl()}/api/users/accounts/${id}/auth_attachment.json${isDev ?`?debug=${window._debugType || 'admin'}` : ""}`
}

export function getRandomNumber(type) {
	let anewopens=md5(newopens+newtimestamp);
	return type===true?`randomcode=${newtimestamp}&client_key=${anewopens}`:`?randomcode=${newtimestamp}&client_key=${anewopens}`
}

export function test(path) {
	return `${path}`;
}

export function toPath(path) {
	window.open(path, '_blank');
}


export function getTaskUrlById(id) {
	return `/tasks/${id}`
}

export function getRandomcode(url) {
	return url;
}

export function htmlEncode(str) {
	var s = "";
	if (str.length === 0) {
		return "";
	}
	s = str.replace(/&/g, "&amp;");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	s = s.replace(/ /g, "&nbsp;");
	s = s.replace(/\'/g, "&#39;");//IE下不支持实体名称
	s = s.replace(/\"/g, "&quot;");
	return s;
}

export function publicSearchs(Placeholder,onSearch,onInputs,onChanges,loadings) {
	return(<Search
		placeholder= { Placeholder || "请输入内容进行搜索" }
		onSearch={onSearch}
		// value={searchValue}
		onInput={onInputs}
		onChange={onChanges}
		loading={loadings||false}
		allowClear={true}
	></Search>)
}



export function turnbar(str){
	let s = str;
  if(s && s.length>0){
		if(s.indexOf("%")>-1){
			s = s.replaceAll('%','_25');
		}
		if(s.indexOf("#")>-1){
			s = s.replaceAll('#','%23');
		}
		if(s.indexOf("/")>-1){
			s = s.replaceAll('/','%2F');
		}
  }
  return s;
}
export function returnbar(str){
	let s = str;
  if(s && s.length>0){
		if(str.indexOf("_25")>-1){
			s = s.replaceAll('_25','%');
		}
		if(s.indexOf("%23")>-1){
			s = s.replaceAll('%23','#');
		}
		if(s.indexOf("%2F")>-1){
			s = s.replaceAll('%2F','/');
		}
  }
  return s;
}