package com.njcn.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URLDecoder;

public abstract class JnaCallDll {
    private static final Logger logger = LoggerFactory.getLogger(JnaCallDll.class);
    private String path = "";
    private String pathDll = "";
    private String nameDll;
    public static String jarPath = "";

    public JnaCallDll(String name) {
        super();
        this.nameDll = name;

        try {
            String os = System.getProperty("os.name");  // 获取当前操作系统的类型
            int beginIndex = os != null && os.startsWith("Windows") ? 1 : 0;// windows操作系统为1 否则为0
            if(beginIndex == 0){
                //linux操作系统
                this.path = URLDecoder.decode(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
            }else {
                this.path = URLDecoder.decode(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replaceFirst("/", ""), "UTF-8");
            }

        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }
        if (JnaCallDll.jarPath.equals("")) {
            JnaCallDll.jarPath = this.path.substring(0, this.path.lastIndexOf('/'));
        }
    }

    public String packagePath(String path) {
        return path + "/" + this.nameDll;
    }

    public boolean judgeFileType() { //判断打包方式是否为jar方式
        String pathPackage = this.getClass().getPackage().toString().replaceAll("package ", "/");
        pathPackage = pathPackage.replace('.', '/');
        this.pathDll = packagePath(path + pathPackage);
        this.pathDll = path + pathPackage + "/" + this.nameDll;

        return path.endsWith(".jar");//如果dll在jar包内部返回true，否则返回true
    }

    public boolean exitFile() {//判断dll文件是否存在
        File file = new File(packagePath(JnaCallDll.jarPath));
        return file.exists() || file.isFile();
    }

    public void copyDll2Path() {//复制jar包中的dll到指定位置
        InputStream is = this.getClass().getResourceAsStream(nameDll);
        File file = new File(packagePath(JnaCallDll.jarPath));
        byte[] bytes = new byte[1024];
        int readBytes;

        try {
            OutputStream os = new FileOutputStream(file);
            while ((readBytes = is.read(bytes)) != -1) {
                os.write(bytes, 0, readBytes);
            }
            os.close();
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
    }

    public String getStrpath() {
        if (judgeFileType()) {
            if (!exitFile()) {
                copyDll2Path();
            }

            return packagePath(JnaCallDll.jarPath);
        } else {
            return this.pathDll;
        }
    }

    public void setPath() {

    }
}
