package com.lukouguoji.module_base.bean

import androidx.databinding.ObservableBoolean
import com.lukouguoji.module_base.cache.ImageCacheModel
import com.lukouguoji.module_base.http.net.Api
import com.lukouguoji.module_base.util.HandlerUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File

class FileBean(
    // 本地地址
    var path: String = "",
    // 缩略图地址
    var url: String = "",
    // 原图地址
    var originalPic: String = ""
) {

    val canDelete = ObservableBoolean(false)

    fun download(callBack: (file: File) -> Unit) {
        if (isOnlineResource()) {
            GlobalScope.launch {
                ImageCacheModel.saveFile(path, onSuccess = {
                    HandlerUtils.postRunnable {
                        path = it.path
                        callBack(it)
                    }
                })
            }
        } else {
            callBack(File(path))
        }
    }

    fun isOnlineResource() = path.contains(Api.BASE_URL)

}