package com.yzx.escreen.utils

import android.app.Dialog
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.yzx.escreen.R
import com.yzx.escreen.config.Config
import java.text.SimpleDateFormat
import java.util.TimeZone

fun ImageView.setSrc(identity: String, key: String) {
    var url =
        "https://${identity.ifEmpty { "filea" }}.oa.qbjjyyun.net/edufile/imageView?uniqueKey=${key}"
    if ("cszxy" == Config.ENV_NAME || "kz" == Config.ENV_NAME) {
        url = "https://file.educlouddata.com/edufile/imageView?uniqueKey=${key}"
    }
    Glide.with(this)
        .load(url)
        .placeholder(R.color.gray_1)
        .into(this)
    this.setTag(R.id.image_src_tag, url)
}

fun String.getImgUrl(key: String): String {
    var url =
        "https://${this.ifEmpty { "filea" }}.oa.qbjjyyun.net/edufile/imageView?uniqueKey=${key}"
    if ("cszxy" == Config.ENV_NAME || "kz" == Config.ENV_NAME) {
        url = "https://file.educlouddata.com/edufile/imageView?uniqueKey=${key}"
    }
    return url
}


fun ImageView.setSrc(url: String) {
    Glide.with(this)
        .load(url)
        .into(this)
}

fun Int.toTime(format: String = "yyyy-MM-dd HH:mm"): String {
    val format = TimeUtils.getSafeDateFormat(format)
    format.timeZone = TimeZone.getTimeZone("GMT+8:00")
    return TimeUtils.millis2String(this * 1000L, format)
}

fun Int.toDay(): String {
    return toTime("yyyy-MM-dd")
}

fun Int.durationStr(): String {
    val value = this
    if (value <= 60) {
        return "${value}秒"
    } else if (value > 60 && value <= 60 * 60) {
        var min = value / 60
        if (min == 60) {
            return "1小时"
        }
        return "${min}分钟"
    } else if (value > 60 * 60 && value <= 24 * 60 * 60) {
        var hour = value / 3600.0

        if (hour >= 23.5f && hour < 25f) {
            return "1天"
        }

        return "${hour.toInt()}小时"
    } else {
        var day = value / (24 * 60 * 60)
        var sub = value % (24 * 60 * 60);
        var hour = sub / 3600f
        if (hour >= 23.5f || hour < 25f) {
            return "${day + 1}天"
        }
        if (hour < 1) {
            return "${day}天"
        }
        return "${day}天${hour.toInt()}小时"
    }
}

fun Dialog.hideNavigationBar() {
    val window = this.window
    window?.apply {
        val uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = uiOptions
        window.decorView.setOnSystemUiVisibilityChangeListener {
            window.decorView.systemUiVisibility = uiOptions
        }
    }
}

fun SimpleDateFormat.zhCn(): SimpleDateFormat {
    timeZone = TimeZone.getTimeZone("GMT+8:00")
    return this
}
























