const isEmail = (email)=>{
    let  reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
    let result = reg.test(email)
    return result
}

const getUploadUrl = () =>{
    // let url = process.env.VUE_APP_BASE_URL || ''
    let url = "http://18.167.251.121:10086"
    return url
}	
 
function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(',');
    var mime = arr[0].match(/:(.*?);/)[1];
    var bstr =atob(arr[1]);    
    var n = bstr.length;     
    var u8arr =new Uint8Array(n); 
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type: mime });
}

function blobToFile(blob, fileName){
    blob.lastModifiedDate =new Date();
    blob.name = fileName;
    return blob;
}

function dataURLtoFile(dataurl, filename){
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    var blob = dataURLtoBlob(dataurl);
    return blobToFile(blob, filename);
}

const base64toFile = (dataurl, filename = 'file') => {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let suffix = mime.split('/')[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], `${filename}.${suffix}`, {
        type: mime
    })

}

function rgbToHsv([R, G, B]) {
    R /= 255
    G /= 255
    B /= 255
    const max = Math.max(R, G, B)
    const min = Math.min(R, G, B)
    const delta = max - min
	var H,S,V
	if (delta === 0) {
	  H = 0;
	} else if (max === R) {
	  H = ((G - B) / delta) % 6;
	} else if (max === G) {
	  H = (B - R) / delta + 2;
	} else { // max === B
	  H = (R - G) / delta + 4;
	}
	H = Math.round(H * 60); // 范围为 0-360
	if(H<0){
		H = 360+H
	}
	if (max === 0) {
	  S = 0;
	} else {
	  S = delta / max;
	}
	S = Math.round(S*100); // 范围为 0-100
	V = Math.round(max*100); // 范围为 0-100
    return [H, S, V]
  }

  const formatTime = (timestamp, fmt) =>{
    // date = new Date(), fmt = 'MM/dd/yyyy';
    let date = new Date();
    date.setTime(timestamp * 1000);

    if (!fmt) {
        formatRule ? (fmt = formatRule) : (fmt = "YYYY-MM-DD hh:mm:ss");
    }
    // console.log(formatRule)
    let o = {
        'M+': date.getMonth() + 1, // 月份
        'D+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S+': date.getMilliseconds(), // 毫秒
        'a': date.getHours() > 12
            ? 'PM'
            : 'AM' // 上午还是下午
    };
    if (/(Y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    if(/(a)/.test(fmt)&&o['h+']>12){
        o['h+'] = o['h+'] - 12
    }
    for (let k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1)
                ? (o[k])
                : (('00' + o[k]).substr(('' + o[k]).length)));
        }
    }

    return fmt;
}

const isMoible = () => {
    let is_mobile = navigator.userAgent.toLowerCase().match(/(ipad|ipod|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i) != null;
    if(is_mobile){
        return true
    }else{
        return false
    }
}



export{
    isEmail,
    getUploadUrl,
    dataURLtoFile,
    blobToFile,
    base64toFile,
    rgbToHsv,
    formatTime,
    dataURLtoBlob,
    isMoible,
}