package com.duolebo.blyrobot.util

import android.content.Context
import android.text.TextUtils
import java.net.Inet4Address
import java.net.NetworkInterface
import java.util.*
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.*


object AppUtil {

    fun deleteFile(file: File?, fileFilter: FileFilter) {
        if (file == null) {
            return
        }
        if (!fileFilter.accept(file)) {
            return
        }
        if (file.isFile) {
            file.delete()
            return
        }
        val files = file.listFiles()
        if (files == null) {
            file.delete()
            return
        }
        for (childFile in files) {
            deleteFile(childFile, fileFilter)
        }
        file.delete()
    }

    fun getIPAddress(useIPv4: Boolean, interfaceName: String?): String {
        try {
            val interfaces = Collections.list(NetworkInterface.getNetworkInterfaces())
            for (intf in interfaces) {
                if (!TextUtils.isEmpty(interfaceName) && intf.name != interfaceName) {
                    continue
                }
                val addrs = Collections.list(intf.inetAddresses)
                for (addr in addrs) {
                    if (!addr.isLoopbackAddress) {
                        val sAddr = addr.hostAddress.toUpperCase()
                        //                        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                        val isIPv4 = addr is Inet4Address //Since API-23 InetAddressUtils was removed
                        if (useIPv4) {
                            if (isIPv4)
                                return sAddr
                        } else {
                            if (!isIPv4) {
                                val delim = sAddr.indexOf('%') // drop ip6 port suffix
                                return if (delim < 0) sAddr else sAddr.substring(0, delim)
                            }
                        }
                    }
                }
            }
        } catch (ex: Exception) {
        }
        // for now eat exceptions
        return ""
    }

    fun pntToJpg(pngFilePath: String, jpgFilePath: String) {
        val bitmap = BitmapFactory.decodeFile(pngFilePath)
        try {
            BufferedOutputStream(FileOutputStream(jpgFilePath)).use { bos ->
                if (bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)) {
                    bos.flush()
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    fun readFromLocal(file: File): String {
        var res = ""
        if (file.isFile && file.exists()) {
            try {
                val fis = FileInputStream(file)
                res = fileRead(fis)
                fis.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

        return res
    }

    fun fileRead(fis: InputStream): String {
        val baos = ByteArrayOutputStream()
        val buffer = ByteArray(1024)
        var len: Int
        try {
            len = fis.read(buffer)
            while (len != -1) {
                baos.write(buffer, 0, len)
                len = fis.read(buffer)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }

        val result = String(baos.toByteArray())
        try {
            baos.close()
            fis.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return result
    }

    fun readFromAssert(context: Context, fileName: String): String {
        var res = ""
        try {
            val inputStream = context.assets.open(fileName)
            res = fileRead(inputStream)
            inputStream.close()
        } catch (e: IOException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }

        return res
    }
}