package com.input.board.appskey.keyboard.fileTools;

import com.input.board.appskey.keyboard.KeyBoardApp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DesFile {
    private static String mAlgorithm = "AES";

    private static String transFormation = mAlgorithm + "/CBC/PKCS5Padding";
    private static IvParameterSpec mIv = new IvParameterSpec("109jksue6yw12rfd".getBytes());

    private static Key getFileKey() {
        return new SecretKeySpec("keyli123879dfgft".getBytes(), mAlgorithm);
    }


    private static byte[] DECRYPT(byte[] byteData) {
        Cipher mCipher = null;
        try {
            mCipher = Cipher.getInstance(transFormation);
            mCipher.init(Cipher.DECRYPT_MODE, getFileKey(), mIv);
            return mCipher.doFinal(byteData);
        } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException |
                 IllegalBlockSizeException | BadPaddingException |
                 InvalidAlgorithmParameterException exception) {
            return null;
        }

    }

    public static String deFile(InputStream inputStream) {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        int byteRead = 0;
        byte[] bytesBuffer = new byte[4096];
        try {
            while ((byteRead = inputStream.read(bytesBuffer)) != -1) {
                arrayOutputStream.write(bytesBuffer, 0, byteRead);
            }
            byte[] bytesContent = arrayOutputStream.toByteArray();

            String deFilePath = KeyBoardApp.appInstance.getCacheDir() + "/decrypt.json";
            File fileDec = new File(deFilePath);
            judeFileExist(fileDec);
            FileOutputStream fileOutputStream = new FileOutputStream(fileDec);
            byte[] decryptContent = DECRYPT(bytesContent);
            fileOutputStream.write(decryptContent);
            fileOutputStream.close();



            return deFilePath;

        } catch (Exception exception) {

            return "";
        }

    }



    private static void judeFileExist(File file) {
        try {
            if(file.exists()){
                file.delete();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

        } catch (Exception ex) {

        }
    }

    public static String getJsonString(InputStream fileInputStream)  {
        try {
//            FileInputStream fileInputStream = new FileInputStream(path);
            char[] charArray = new char[fileInputStream.available()];
            int readCount = 0;
            InputStreamReader streamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            StringWriter stringWriter = new StringWriter();

            while ((readCount = bufferedReader.read(charArray)) != -1) {
                stringWriter.write(charArray, 0, readCount);
            }
            String s = stringWriter.toString();


            return s;

        } catch (IOException e) {

         return "";
        }

    }

}
