import { nowTime } from './my'
import { transformMD5 } from './transformMD5'
import CryptoJS from 'crypto-js'

const createAK = () => {
  const timeStamp8 = String(Date.now()).substring(0, 8).split('')
  const timeObj = nowTime()
  const timeStr8 = `${timeObj.year}${timeObj.month}${timeObj.day}`.split('')
  const arr18 = []
  timeStamp8.forEach((item, index) => {
    arr18.push('' + item + timeStr8[index])
  })
  const str18 = arr18.join('')
  const bigMD5 = transformMD5(str18).toUpperCase()
  const zhuArr = bigMD5.substring(16).split('')
  const fuArr = bigMD5.substring(0, 16).split('')
  // console.log('zhu', zhuArr)
  // console.log('fu', fuArr)
  const resArr = []
  zhuArr.forEach((item, index) => {
    const flag = Number(item) + Number(fuArr[index])
    if (flag > 0 || flag === 0) {
      resArr.push(timeObj.week)
    } else {
      resArr.push(item)
    }
  })
  return resArr.join('')
}

const iv = ''
const SK = '2ba7c765-b1fb-11eb-8e3a-0cda411d268e'

const parse = string => CryptoJS.enc.Utf8.parse(string)

export const encrypt = () => {
  const AK = createAK()
  console.log('AK', AK)
  return CryptoJS.AES.encrypt(parse(SK), parse(AK), {
    iv: parse(iv),
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }).toString()
}

export const decrypt = (code) => {
  const AK = createAK()
  return CryptoJS.AES.decrypt(code, parse(AK), {
    iv: parse(iv),
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }).toString(CryptoJS.enc.Utf8)
}
