package com.zxhy.solarmasterace.data

import android.content.Context
import android.net.Uri
import android.text.format.Formatter
import android.util.Log
import android.widget.ImageView
import coil.dispose
import coil.load
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.quicktools.ColiVideoEx
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

data class FileBean(
    val id: Long = 0L,
    val name: String = "",
    val path: String = "",
    val uri: Uri = Uri.EMPTY,
    val size: Long = 0L,
    val time: String = "",
    var isSelect: Boolean = false
) {
    fun file(): File {
        return File(path)
    }

    fun isDir(): Boolean {
        val file = File(path)
        return file.exists() && file.isDirectory
    }

    fun isEmpty(): Boolean {
        return size == 0L
    }

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

    fun imageRes(): Int {
        return R.mipmap.as_230
    }

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

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

    fun isApk(): Boolean {
        return name.contains(".apk")
    }

    fun isZip(): Boolean {
        return name.contains(".zip") or name.contains(".rar")
    }

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


    fun isWord(): Boolean {
        return name.contains(".doc") or name.contains(".docx")
    }


    fun isExcel(): Boolean {
        return name.contains(".xls") or name.contains(".xlsx")
    }

    fun isPPt(): Boolean {
        return name.contains(".ppt") or name.contains(".pptx")
    }

    fun isPdf(): Boolean {
        return name.contains(".pdf")
    }

    fun isTxt(): Boolean {
        return name.contains(".txt")
    }

    fun isDocument(): Boolean {
        return isWord() or isExcel() or isPPt() or isPdf() or isTxt()
    }

    fun sizeFormat(context: Context): String? {
        return Formatter.formatFileSize(context, size)
    }

    fun bindIconImage(image: ImageView) {
        image.dispose()
        if (isDir()) {
            image.setImageResource(R.mipmap.dsjjd_698987)
        } else if (isImage()) {
            image.load(file())
        } else if (isVideo()) {
            image.load(file(), ColiVideoEx.videoLoader(image.context))
        } else if (isAudio()) {
            image.setImageResource(R.mipmap.fasd_87985)
        } else if (isApk()) {
            image.setImageResource(R.mipmap.faad_8795)
        } else if (isZip()) {
            image.setImageResource(R.mipmap.fas_87895)
        } else if (isLogFile()) {
            image.setImageResource(R.mipmap.fzzx_65987)
        } else if (isWord()) {
            image.setImageResource(R.mipmap.wrf_58997)
        } else if (isExcel()) {
            image.setImageResource(R.mipmap.ex_289897)
        } else if (isPPt()) {
            image.setImageResource(R.mipmap.fas_87895)
        } else if (isPdf()) {
            image.setImageResource(R.mipmap.zc_8979855556)
        } else if (isTxt()) {
            image.setImageResource(R.mipmap.fdg_87985)
        } else {
            image.setImageResource(R.mipmap.zczxc_699887)
        }
    }
}

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

fun File.isVideo(): Boolean {
    return name.contains(".mp4") or name.contains(".avi")
}

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

fun File.isApk(): Boolean {
    return name.contains(".apk")
}

fun File.isZip(): Boolean {
    return name.contains(".zip") or name.contains(".rar")
}

fun File.isLogFile(): Boolean {
    return name.contains("log") or name.contains("Log")
}

fun File.isWord(): Boolean {
    return name.contains(".doc") or name.contains(".docx")
}

fun File.isExcel(): Boolean {
    return name.contains(".xls") or name.contains(".xlsx")
}

fun File.isPPt(): Boolean {
    return name.contains(".ppt") or name.contains(".pptx")
}


fun File.isPdf(): Boolean {
    return name.contains(".pdf")
}

fun File.isTxt(): Boolean {
    return name.contains(".txt")
}

fun File.isDocument(): Boolean {
    return isWord() or isExcel() or isPPt() or isPdf() or isTxt()
}

fun File.toFileBean(id: Long = 0): FileBean {
    val timeE = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(lastModified())
    return FileBean(
        id = id,
        name = name,
        path = absolutePath,
        size = length(),
        time = timeE
    )
}

