package com.cr.common

import android.content.Context
import com.cr.cruav.R
import com.cr.data.CrUtil
import java.io.*
import java.text.DecimalFormat

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/31 09:58
 * 描述：文件管理器
 */
class FileManager {
    companion object{

        /**
         * 判断文件是否存在
         * @param filePath String? 文件路径
         * @return Boolean
         */
        fun isExists(filePath: String?): Boolean {
            val file = File(filePath)
            return file.exists()
        }

        /**
         * 创建目录
         * @param folderPath String? 目录路径
         * @return Boolean
         */
        fun createFolder(folderPath:String?):Boolean{
            val file = File(folderPath)
            return file.mkdir()
        }

        /**
         * 将文件大小转为换含大小单位的字符串
         * @param fileSize
         * @return
         */
        fun FileSizeToString(fileSize: Long): String? {
            return if (fileSize < 1000) {
                String.format("%s b", fileSize)
            } else if (fileSize < 1000 * 1000) {
                String.format(
                    "%s kb",
                    DecimalFormat("0.00").format(java.lang.Double.valueOf(fileSize.toDouble()) / 1000.0)
                )
            } else {
                String.format(
                    "%s mb",
                    DecimalFormat("0.00").format(java.lang.Double.valueOf(fileSize.toDouble()) / (1000.0 * 1000.0))
                )
            }
        }

        /**
         * 截取文件名称含扩展名
         * @param fileAllPath 文件全路径
         * @return 含扩展名的文件名
         */
        fun getFileName(fileAllPath: String): String? {
            val start = fileAllPath.lastIndexOf("/")
            val end = fileAllPath.length
            return if (start != -1 && end != -1) {
                fileAllPath.substring(start + 1, end)
            } else {
                null
            }
        }

        /**
         * 截取文件的路径
         * @param fileAllPath 文件全路径
         * @return 文件路径不含文件名及扩展名
         */
        fun getFilePath(fileAllPath: String): String? {
            val start = fileAllPath.lastIndexOf("/")
            return if (start != -1) {
                fileAllPath.substring(0, start + 1)
            } else {
                null
            }
        }

        /**
         * 拷贝文件
         * @param oldFilePath 源文件全路径
         * @param newFilePath 新文件全路径
         * @return 是否拷贝成功
         */
        fun copyFile(oldFilePath: String?, newFilePath: String?): Boolean {
            val file = File(oldFilePath)
            if (!isExists(oldFilePath)) return false
            return if (isExists(newFilePath)) true else try {
                val inputStream = FileInputStream(oldFilePath)
                val outputStream = FileOutputStream(newFilePath)
                val buffer = ByteArray(1024)
                var byteRead: Int
                while (-1 != inputStream.read(buffer).also { byteRead = it }) {
                    outputStream.write(buffer, 0, byteRead)
                }
                inputStream.close()
                outputStream.flush()
                outputStream.close()
                true
            } catch (e: FileNotFoundException) {
                false
            } catch (e: IOException) {
                false
            }
        }

        /**
         * 删除文件
         * @param filePath 文件全路径
         * @return 删除是否成功
         */
        fun deleteFile(filePath: String?): Boolean {
            if (!isExists(filePath)) {
                return false
            }
            val file = File(filePath)
            return file.delete()
        }

        /**
         * 移植数据库
         * @param context
         */
        fun MoveDataBase(context: Context): String? {
            val dbPath = CrUtil.DATABASE_PATH
            val dbFile = File(dbPath)
            if (dbFile.exists()) {
                return dbPath
            }
            var fout: FileOutputStream? = null //VARIABLE: 输出流
            var inputStream: InputStream? = null //VARIABLE: 输入流
            return try {
                inputStream = context.resources.openRawResource(R.raw.config)
                fout = FileOutputStream(dbFile)
                val buffer = ByteArray(128)
                var len: Int
                while (inputStream.read(buffer).also { len = it } != -1) {
                    fout.write(buffer, 0, len)
                }
                dbPath
            } catch (ex: FileNotFoundException) {
                ex.printStackTrace()
                null
            } catch (ex: IOException) {
                ex.printStackTrace()
                null
            } finally {
                if (fout != null) {
                    try {
                        fout.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
                if (inputStream != null) {
                    try {
                        inputStream.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
        }
    }
}