package com.zxhy.fastfilemanagerpro.kokodata

import android.content.Context
import android.net.Uri
import android.text.format.Formatter
import android.widget.ImageView
import com.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.toFile
import com.zxhy.fastfilemanagerpro.kokotools.GlideHelper.loadImageView
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

data class KoFileDatako(
    var name: String = "",
    var path: String = "",
    var uri: Uri = Uri.EMPTY,
    val timeE: String = "",
    var isSelect: Boolean = false,
    val sizeF: String = ""
) {
    companion object {

        fun KoFileDatako.loadImageIcon(context: Context, imageView: ImageView) {
            when {
                isEmpty() -> imageView.setImageResource(R.mipmap.qwe_889989111)
                isImage() or isVideo() -> imageView.loadImageView(context, toFile())
                isAudio() -> imageView.setImageResource(R.mipmap.zd_8871110)
                isLog() -> imageView.setImageResource(R.mipmap.zd_001123)
                isZip() -> imageView.setImageResource(R.mipmap.eqwe_878979)
                isApk() -> imageView.setImageResource(R.mipmap.eqwe_88599)
                isDocuments() -> imageView.setImageResource(R.mipmap.qwe_889989)
                toFile().isDirectory -> imageView.setImageResource(R.mipmap.dd_89896330)
                else -> imageView.setImageResource(R.mipmap.qwe_889989111)
            }
        }

        fun KoFileDatako.isEmpty(): Boolean {
            return toFile().length() == 0L
        }

        fun KoFileDatako.isImage(): Boolean {
            return name.contains(".jpg") or name.contains(".png") or name.contains(".gif")
        }

        fun KoFileDatako.isAudio(): Boolean {
            return name.contains(".mp3") or
                    name.contains(".aac") or
                    name.contains(".ogg") or
                    name.contains(".aac")
        }

        fun KoFileDatako.isLog(): Boolean {
            return name.contains("log") or name.contains("Log")
        }

        fun KoFileDatako.isZip(): Boolean {
            return name.contains(".zip") or name.contains(".7z")
        }

        fun KoFileDatako.isApk(): Boolean {
            return name.contains(".zip") or name.contains(".7z")
        }

        fun KoFileDatako.isDocuments(): Boolean {
            return name.contains(".doc") or
                    name.contains(".docx") or
                    name.contains(".ppt") or
                    name.contains(".pptx") or
                    name.contains(".xls") or
                    name.contains(".xlsx") or
                    name.contains(".txt") or
                    name.contains(".pdf")
        }

        fun KoFileDatako.isVideo(): Boolean {
            return name.contains(".mp4") or
                    name.contains(".avi") or
                    name.contains(".flv") or
                    name.contains(".rmvb") or
                    name.contains(".mkv")
        }


        fun KoFileDatako.toFile(): File {
            return File(path)
        }

        fun File.koFileDatako(context: Context): KoFileDatako {
            return KoFileDatako(
                name = name,
                timeE = timeE(),
                sizeF = sizeF(context),
                path = absolutePath
            )
        }

        fun File.timeE(): String {
            return SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(lastModified())
        }

        fun File.sizeF(context: Context): String {
            return Formatter.formatFileSize(context, length())
        }
    }

}