import { App, HsdsProvider  } from '../../../packages/app';
import { useLocation } from 'react-router-dom';
import CryptoJS from "crypto-js";


// 加密密钥（长度必须是 16 的整数倍，此处为 32 位）
const secretKey =  CryptoJS.enc.Utf8.parse("5405778e38fe5a12b4ce123443215678");
// 偏移量
const secretiv = CryptoJS.enc.Utf8.parse("5405778e38fe5a12b4ce123443215678");

function encryption(data:any) {
  let key = CryptoJS.enc.Utf8.parse(secretKey); // 加密秘钥
  let iv = CryptoJS.enc.Utf8.parse(secretiv);  //  矢量
  let encryptResult = CryptoJS.AES.encrypt(data,key, {   //  AES加密
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7  // 后台用的是pad.Pkcs5,前台对应为Pkcs7
  });
  return encodeURIComponent(CryptoJS.enc.Base64.stringify(encryptResult.ciphertext));  // Base64加密encode;
}

function decryption(data:any) {
  let key = CryptoJS.enc.Utf8.parse(secretKey);  // 加密秘钥
  let iv = CryptoJS.enc.Utf8.parse(secretiv);   //  矢量
  let baseResult=CryptoJS.enc.Base64.parse(data);   // Base64解密
  let ciphertext=CryptoJS.enc.Base64.stringify(baseResult);     // Base64解密
  let decryptResult = CryptoJS.AES.decrypt(ciphertext,key, {    //  AES解密
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
  });
  return CryptoJS.enc.Utf8.stringify(decryptResult);
}



  function StratApp() {
    const query = new URLSearchParams(useLocation().search);
    //获取并解析页面请求文件路径和文件类型
    const fileext = `${query.get('fileExt') }`
    // const fileExt = aesDecryptParams(JSON.stringify(fileext))
    const fileExt = decryption(fileext)
    console.log(fileExt)
    const FileExtArr = fileExt.split('.')
    console.log(FileExtArr)
    const path = `${query.get('file') }`;
    // const filePath = aesDecryptParams(path) as any
    const filePath = decryption(path)
    const FileArr = filePath.split('.heps')
    console.log(FileArr)

    //完善获取的路径数组
    var storFilePath = [];
    for (var i = 0;i<FileArr.length-1;i++){
      var temp = FileArr[i] + '.heps'
      storFilePath.push(temp)
    }
    console.log(storFilePath)
    //完善获取的文件后缀
    var storFileExt = [];
    for (var i = 0;i<FileArr.length-1;i++){
      var temp ='.'+ FileExtArr[i]
      storFileExt.push(temp)
    }
    console.log(storFileExt)




 return (
  <HsdsProvider
  url={`http://localhost:5000`}
  username=''
  password=''
  filepath={storFilePath[storFilePath.length-1]}
  fileExt={storFileExt[storFilePath.length-1]}

  >
      <App explorerOpen={true}   url={`http://localhost:5000`}
    username=""
    password=""
    filepath={storFilePath}
    fileExt={storFileExt}
    fileIndex={storFilePath.length-1}
    />

   </HsdsProvider>
  );
}

export default StratApp;
