package com.swago.baseswago.cusview

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import com.swago.baseswago.databinding.ViewAvatarBinding
import com.swago.loadUrl

class SwagoImageView : FrameLayout {

    private var binding : ViewAvatarBinding? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        initView()
    }

    private fun initView() {
        binding = ViewAvatarBinding.inflate(LayoutInflater.from(context),this,true)
    }

    fun loadImage(avatarUrl:String,picFrameUrl:String=""){
        binding?.apply {
            ivAvatar.loadUrl(context,avatarUrl)
            if (picFrameUrl.isNotEmpty()){
                ivFrame.loadUrl(context,picFrameUrl)
            }
        }
    }

    fun loadImage(@DrawableRes resourceId: Int, picFrameUrl:String=""){
        binding?.apply {
            ivAvatar.loadUrl(context,resourceId)
            if (picFrameUrl.isNotEmpty()){
                ivFrame.loadUrl(context,picFrameUrl)
            }
        }
    }

}