package com.remax.visualnovel.widget.ui

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.WidgetUserAvatarBinding
import com.remax.visualnovel.extension.glide.loadAndCircleCrop
import com.remax.visualnovel.extension.glide.loadCircleAvatar
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.extension.setSize
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.imageviewer.viewer.ViewerTransitionHelper
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.view.UITokenConstraintLayout
import com.dylanc.viewbinding.nonreflection.inflate


/**
 * Created by HJW on 2022/8/31
 *
 */
class UserAvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenConstraintLayout(context, attrs, defStyleAttr) {

    companion object {
        const val XXL = 0
        const val XL = 1
        const val L = 2
        const val M = 3
        const val S = 4

        const val BORDER_WHITE = 1
        const val BORDER_BLACK = 2
    }

    private var binding: WidgetUserAvatarBinding? = null

    private var avatarSize: Int = 0
    private var avatarFrameSize: Int = 0
    private var onlineStatusSize: Int = 0
    private var avatarSizeType: Int = 0

    private val sizeXXL = 128
    private val sizeFrameXXL = 160
    private val sizeXL = 80
    private val sizeFrameXL = 104
    private val sizeL = 64
    private val sizeFrameL = 88
    private val sizeM = 48
    private val sizeFrameM = 64
    private val sizeS = 32
    private val sizeFrameS = 40

    init {
        binding = inflate(WidgetUserAvatarBinding::inflate)
        val ta = context.obtainStyledAttributes(attrs, R.styleable.UserAvatarView)
        val avatarBorder = ta.getBoolean(R.styleable.UserAvatarView_avatarBorder, false)
        val avatarBorderType = ta.getInt(R.styleable.UserAvatarView_avatarBorderType, BORDER_WHITE)
        avatarSize = ta.getDimensionPixelSize(R.styleable.UserAvatarView_avatarSize, 0)
        avatarFrameSize = ta.getDimensionPixelSize(R.styleable.UserAvatarView_avatarFrameSize, 0)
        onlineStatusSize = ta.getDimensionPixelSize(R.styleable.UserAvatarView_onlineStatusSize, 0)
        avatarSizeType = ta.getInt(R.styleable.UserAvatarView_avatarSizeType, -1)
        ta.recycle()
        binding?.run {
            var avatarBorderPadding = 0.dp
            when (avatarSizeType) {
                XXL -> {
                    avatarSize = sizeXXL.dp
                    avatarFrameSize = sizeFrameXXL.dp
                    onlineStatusSize = 24.dp
                }

                XL -> {
                    avatarBorderPadding = 2.dp
                    avatarSize = sizeXL.dp
                    avatarFrameSize = sizeFrameXL.dp
                    onlineStatusSize = 16.dp
                }

                L -> {
                    avatarSize = sizeL.dp
                    avatarFrameSize = sizeFrameL.dp
                    onlineStatusSize = 16.dp
                }

                M -> {
                    avatarSize = sizeM.dp
                    avatarFrameSize = sizeFrameM.dp
                    onlineStatusSize = 8.dp
                }

                S -> {
                    avatarBorderPadding = 1.dp
                    avatarSize = sizeS.dp
                    avatarFrameSize = sizeFrameS.dp
                    onlineStatusSize = 8.dp
                }
            }
            if (avatarBorder) {
                userAvatarIv.setPadding(avatarBorderPadding, avatarBorderPadding, avatarBorderPadding, avatarBorderPadding)
                userAvatarIv.changeBackground {
                    when (avatarBorderType) {
                        BORDER_WHITE -> backgroundUIColorToken = context.getString(R.string.color_surface_white_normal)
                        BORDER_BLACK -> backgroundUIColorToken = context.getString(R.string.color_background_default)
                    }
                }
            }
            userAvatarIv.setSize(avatarSize, avatarSize)
            userAvatarFrameIv.setSize(avatarFrameSize, avatarFrameSize)
            userAvatarOnlineStatus.setSize(onlineStatusSize, onlineStatusSize)

            root.setSize(avatarSize, avatarSize)

            clipChildren = false
            clipToPadding = false
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(avatarSize, avatarSize)
        (parent as? ViewGroup)?.clipChildren = false
        (parent as? ViewGroup)?.clipToPadding = false
    }

    fun getAvatarImageView(): AppCompatImageView? = binding?.userAvatarIv

    /**
     * needViewer 能否点击放大
     */
    fun loadAvatar(avatarUrl: String?, clickInvoke: ((View) -> Unit)? = null) {
        binding?.userAvatarIv?.let {
            if (avatarUrl?.startsWith("http") == true){
                it.loadCircleAvatar(avatarUrl, avatarSize)
            }else{
                it.loadAndCircleCrop(avatarUrl)
            }
            if (clickInvoke != null) {
                ViewerTransitionHelper.put(it)
                setOnClick(it) {
                    clickInvoke.invoke(it)
                }
            }
        }
    }

    fun loadImageResourceAvatar(resId: Int) {
        binding?.userAvatarIv?.setImageResource(resId)
    }

    fun setAvatarViewPadding(padding: Int) {
        binding?.userAvatarIv?.setPadding(padding,padding,padding,padding)
    }

//    /**
//     * 隐藏/显示在线状态
//     */
//    fun onlineStatusIsShow(isShow: Boolean) {
//        binding?.userAvatarOnlineStatus?.isVisible = isShow
//    }

//    /**
//     * 展示头像框
//     */
//    fun showAvatarFrame(avatarFrame: String?, withHost: Boolean = true) {
//        binding?.userAvatarFrameIv?.showVipAvatar(avatarFrame, withHost)
//    }

}