package com.rdiot.yx485.util

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.king.zxing.util.LogUtils
import com.rdiot.yx485.R
import com.rdiot.yx485.base.LocalData
import com.rdiot.yx485.net.DeviceCode

object RoomUtils {
    @JvmStatic
    fun getIcon(code: String?): Int {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> R.mipmap.water_master
            DeviceCode.WATER_SUB.typeCode -> R.mipmap.water_sub
            DeviceCode.WATER_INTELLIGENCE.typeCode -> R.mipmap.water_intelligence
            DeviceCode.FLUORINE_MASTER.typeCode -> R.mipmap.fluorine_master
            else -> R.mipmap.fluorine_sub
        }

        return result
    }

    @JvmStatic
    @BindingAdapter("imageResource")
    fun loadImage(view: ImageView, code: String?) {
        val context = view.context

        Glide.with(context)
            .load(getIcon(code))
            .into(view)
    }

    @JvmStatic
    @BindingAdapter("imageUrl")
    fun loadImageByUrl(view: ImageView, icon: String?) {
        val context = view.context
        val imgUrl = "${LocalData.apiHost.avatarHost}${icon}"
        Glide.with(context)
            .load(imgUrl).placeholder(R.mipmap.fluorine_sub)
            .into(view)
    }

//    @JvmStatic
//    fun getName(code: String?): Int {
//        val result = when (code) {
//            DeviceCode.WATER_MASTER.typeCode -> R.string.master
//            DeviceCode.WATER_SUB.typeCode -> R.string.sub
//            DeviceCode.WATER_INTELLIGENCE.typeCode -> R.string.intelligence
//            DeviceCode.FLUORINE_MASTER.typeCode -> R.string.master
//            else -> R.string.sub
//        }
//        return result
//    }

    @JvmStatic
    fun getDesc(code: String?): Int {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> R.string.master_en
            DeviceCode.WATER_SUB.typeCode -> R.string.sub_en
            DeviceCode.WATER_INTELLIGENCE.typeCode -> R.string.intelligence_en
            DeviceCode.FLUORINE_MASTER.typeCode -> R.string.master_en
            else -> R.string.sub_en
        }
        return result
    }

    @JvmStatic
    fun getIconColor(code: String?, context: Context): GradientDrawable {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> R.color.c_21B191
            DeviceCode.WATER_SUB.typeCode -> R.color.c_4AB0E4
            DeviceCode.WATER_INTELLIGENCE.typeCode -> R.color.c_60A736
            DeviceCode.FLUORINE_MASTER.typeCode -> R.color.c_5198FD
            else -> R.color.c_F0B756
        }
        val shape = GradientDrawable()
        shape.cornerRadius = 40f // 圆角半径
        shape.setColor(ContextCompat.getColor(context, result))
        return shape
    }

    @JvmStatic
    fun getTextColor(code: String?, context: Context): Int {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> R.color.c_558178
            DeviceCode.WATER_SUB.typeCode -> R.color.c_387084
            DeviceCode.WATER_INTELLIGENCE.typeCode -> R.color.c_517232
            DeviceCode.FLUORINE_MASTER.typeCode -> R.color.c_31517D
            else -> R.color.c_A79365
        }
        return ContextCompat.getColor(context, result)
    }

    @JvmStatic
    fun getBgColor(code: String?, context: Context): GradientDrawable {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> R.color.c_8BFAE1
            DeviceCode.WATER_SUB.typeCode -> R.color.c_55DCDF
            DeviceCode.WATER_INTELLIGENCE.typeCode -> R.color.c_B1EB5C
            DeviceCode.FLUORINE_MASTER.typeCode -> R.color.c_97C2FF
            else -> R.color.c_FAE06A
        }
        val shape = GradientDrawable()
        shape.cornerRadius = 20f // 圆角半径
        shape.setColor(ContextCompat.getColor(context, result))

        return shape
    }

    //是否是水机
    @JvmStatic
    fun isWater(code: String?): Boolean {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> true
            DeviceCode.WATER_SUB.typeCode -> true
            DeviceCode.WATER_INTELLIGENCE.typeCode -> true
            DeviceCode.FLUORINE_MASTER.typeCode -> false
            else -> false
        }
        return result
    }

    //是否是主控
    @JvmStatic
    fun isMaster(code: String?): Boolean {
        val result = when (code) {
            DeviceCode.WATER_MASTER.typeCode -> true
            DeviceCode.WATER_SUB.typeCode -> false
            DeviceCode.WATER_INTELLIGENCE.typeCode -> false
            DeviceCode.FLUORINE_MASTER.typeCode -> true
            else -> false
        }
        return result
    }

    //是否显示睡眠
    @JvmStatic
    fun isShowSleep(code: String?): Boolean {
        val result = when (code) {
            DeviceCode.FLUORINE_MASTER.typeCode -> false
            else -> true
        }
        return result
    }
}