import ThkWeb3 from "thinkium-web3js";
import { BigNumber } from 'bignumber.js';

const thkWeb3 = new ThkWeb3();



export function compressImage(base64String, width, quality) {
    var getMimeType = function (urlData) {
      var arr = urlData.split(',');
      var mime = arr[0].match(/:(.*?);/)[1];
      // return mime.replace("image/", "");
      return mime;
    };
  
    return new Promise((resolve, reject) => {
      var oldimg = new Image();
      oldimg.src = base64String;
      var canvas = document.createElement('canvas');
      var context = canvas.getContext('2d');
  
      oldimg.onload = function() {
        var height = width / oldimg.width * oldimg.height;
        console.log('原始宽：' + oldimg.width + "原始高：" + oldimg.height);
        canvas.width = width;
        canvas.height = height;
        // 绘制
        context.drawImage(oldimg, 0, 0, width, height);
        // 新的base64数据（压缩后的数据）
        var newBase64 = canvas.toDataURL(getMimeType(base64String), quality);
        console.log('compressBase64Compress', newBase64);
        resolve(newBase64);
      }
    })
  }

  export function toBig(value) {
    if(isNaN(value) || !value){
       return '0';
    }
    return new BigNumber(value).multipliedBy('1e+18').toString(10)
  }
  

  export function isAddressTH(address) {
    return thkWeb3.Iban.isValid(address)
  }
  
  export function toAddressTH(address) {
    if (!address) {
      console.log('传入的address为空');
      return;
    }
    address = address.toLowerCase();
    if (address.substring(0, 2) !== '0x') {
      console.log('转换为TH时，传入的地址不正确');
      return '';
    }
    return thkWeb3.Iban.toIban(address)
  }
  
  export function toAddress0x(address) {
    if (!address) {
      console.log('传入的address为空');
      return '';
    }
    address = address.toUpperCase();
    if (!thkWeb3.Iban.isValid(address)) {
      console.log('传入的地址不正确');
      return address;
    }
    return thkWeb3.Iban.toAddress(address).toLowerCase();
  }

  export function hideMoreText(text, front, end = 0) {
    if (!text) {
      return '';
    }
    text = text.trim();
    if (text.length <= front + end) {
      return text
    } else {
      return text.substr(0, front) + "..." + text.substr(text.length - end);
    }
  }


  export function _trimAll(data){
    if(typeof data == 'string'){
       return data.trim();
    }else if(typeof data == 'object'){
       Object.keys(data).map(key => {
          data[key] = _trimAll(data[key]);
       })
       return data;
    }else{
      return data;
    }
  }


  export function toRegularNumber(number, type = 'floor', n = 6){
    if(!number){
	    return 0;
	  }
    if(number instanceof Array){
      return 0;
    }
    number = number.toString();
    let regularNumber = new BigNumber(number).div("1e+18").toString(10);

    if (type === 'origin') {

    }
    if (type === 'auto') {
      regularNumber =  parseFloat((regularNumber - 0).toFixed(n))
    }


    if (type === 'ceil') {
      regularNumber *= Math.pow(10, n);
      regularNumber = Math.ceil(regularNumber);
      regularNumber =  parseFloat(regularNumber / Math.pow(10, n))
    }
    if (type === 'floor') {
      regularNumber *= Math.pow(10, n);
      regularNumber = Math.floor(regularNumber);
      regularNumber =  parseFloat(regularNumber / Math.pow(10, n))
    }

    if(regularNumber - 999999999 > 0){
      regularNumber = regularNumber.toExponential(n);
    }

    console.log('---regularNumber', regularNumber);
    return regularNumber;
  }

// 比较大数
export function AIsBiggerThanB(numberA, numberB) {
  let a = new BigNumber(numberA);
  let b = new BigNumber(numberB);
  return a.gt(b);
}


export function clearNumber(number) {
  number += '';
  number = number.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
  number = number.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
  number = number.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
  // number = number.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
  if (number.indexOf(".") < 0 && number != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
    number = parseFloat(number);
  }
  return number || '';
}

export function _toFixed(num, n) {
  let [int, decimal] = (num).toString().split('.');
  decimal = decimal ? decimal.toString().substr(0, n) : '';
  num = parseFloat(int + '.' + decimal);
  return num;
}