package com.quiescent.wallpaper.collection.test.tool

import com.quiescent.wallpaper.collection.test.ui.applicationContext
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream


object Utils {

    fun unitCovert(dpV: Float): Int {
       applicationContext.resources.displayMetrics.density.let {
           val value = dpV * it + 0.5f
           return value.toInt()
       }
    }

    fun fileToCache(sourceFilepath: String?, newPath: String?): Boolean {
        return try {
            val oldFile = File(sourceFilepath)
            if (!oldFile.exists()) {
                return false
            } else if (!oldFile.isFile) {
                return false
            } else if (!oldFile.canRead()) {
                return false
            }
            val input = FileInputStream(sourceFilepath)
            val output = FileOutputStream(newPath)
            val buffer = ByteArray(1024)
            var byteRead: Int
            while (-1 != input.read(buffer).also { byteRead = it }) {
                output.write(buffer, 0, byteRead)
            }
            input.close()
            output.flush()
            output.close()
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

}