package com.gh.gamecenter.common.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import com.facebook.drawee.generic.RoundingParams
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor


class AvatarBorderView : ConstraintLayout {

    var avatarView: SimpleDraweeView? = null
    var borderView: SimpleDraweeView? = null
    var badgeView: SimpleDraweeView? = null
    var avatarContainer: CardView? = null
    var avatarBorderView: View? = null
    private var mAvatarWidth = 100f
    private var mBadgeWidth = 30f
    private var mAvatarBorderColor = Color.parseColor("#ffffff")
    private var mAvatarBorderWidth = 2f.dip2px().toFloat()
    private var mRatio = 0f

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

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

    private fun initView(attrs: AttributeSet) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.AvatarBorderView)
        if (ta.hasValue(R.styleable.AvatarBorderView_avatar_width)) {
            mAvatarWidth = ta.getDimension(R.styleable.AvatarBorderView_avatar_width, mAvatarWidth)
        }
        if (ta.hasValue(R.styleable.AvatarBorderView_badge_width)) {
            mBadgeWidth = ta.getDimension(R.styleable.AvatarBorderView_badge_width, mBadgeWidth)
        }
        if (ta.hasValue(R.styleable.AvatarBorderView_border_width)) {
            mAvatarBorderWidth = ta.getDimension(R.styleable.AvatarBorderView_border_width, mAvatarBorderWidth)
        }
        if (ta.hasValue(R.styleable.AvatarBorderView_border_ratio)) {
            mRatio = ta.getFloat(R.styleable.AvatarBorderView_border_ratio, 0f)
        }
        mAvatarBorderColor = ta.getColor(R.styleable.AvatarBorderView_border_color, mAvatarBorderColor)
        ta.recycle()

        LayoutInflater.from(context).inflate(R.layout.view_avatar_border, this, true)
        avatarView = findViewById(R.id.avatarView)
        borderView = findViewById(R.id.borderView)
        badgeView = findViewById(R.id.badgeView)
        avatarContainer = findViewById(R.id.avatarContainer)
        avatarBorderView = findViewById(R.id.avatarBorderView)

        changeLayoutParams()
    }

    private fun changeLayoutParams() {
        avatarContainer?.apply {
            radius = mAvatarWidth / 2
            cardElevation = 0F
        }
        avatarView?.apply {
            val params = layoutParams
            params.width = mAvatarWidth.toInt()
            params.height = mAvatarWidth.toInt()
            layoutParams = params
            val roundingParams = RoundingParams.fromCornersRadius(mAvatarWidth)
            roundingParams.borderColor = mAvatarBorderColor
            roundingParams.borderWidth = mAvatarBorderWidth
            hierarchy?.roundingParams = roundingParams
        }
        avatarBorderView?.apply {
            val params = layoutParams
            params.width = mAvatarWidth.toInt()
            params.height = mAvatarWidth.toInt()
            layoutParams = params
            val drawable = GradientDrawable().apply {
                setColor(R.color.transparent.toColor())
                shape = GradientDrawable.OVAL
                setStroke(mAvatarBorderWidth.toInt(), mAvatarBorderColor)
            }
            background = drawable
        }
        borderView?.apply {
            val params = layoutParams
            if (mRatio > 0) {
                params.width = (mAvatarWidth * mRatio).toInt()
                params.height = (mAvatarWidth * mRatio).toInt()
            } else {
                params.width = (mAvatarWidth * 3 / 2).toInt()
                params.height = (mAvatarWidth * 3 / 2).toInt()
            }
            layoutParams = params
        }
        badgeView?.apply {
            val params = layoutParams
            params.width = mBadgeWidth.toInt()
            params.height = mBadgeWidth.toInt()
            layoutParams = params
        }
    }

    fun setAvatarWidth(width: Float) {
        mAvatarWidth = width
        changeLayoutParams()
    }

    fun setBadgeWidth(width: Float) {
        mBadgeWidth = width
        changeLayoutParams()
    }

    fun display(borderUrl: String? = "", avatarUrl: String? = "", badgeUrl: String? = "") {
        if (avatarUrl.isNullOrEmpty()) {
            avatarView?.setImageURI("")
        } else {
            ImageUtils.display(avatarView, avatarUrl)
        }
        displayBorder(borderUrl)
        displayUserBadge(badgeUrl)
    }

    fun displayAvatar(avatarUrl: String? = "") {
        if (avatarUrl.isNullOrEmpty()) {
            avatarView?.setImageURI("")
        } else {
            ImageUtils.display(avatarView, avatarUrl)
        }
    }

    fun displayBorder(borderUrl: String? = "") {
        if (!borderUrl.isNullOrEmpty()) {
            borderView?.visibility = View.VISIBLE
            ImageUtils.display(borderView, borderUrl)
        } else {
            borderView?.visibility = View.INVISIBLE
        }
    }

    private fun displayUserBadge(badgeUrl: String? = "") {
        if (badgeUrl.isNullOrEmpty()) {
            badgeView?.setImageURI("")
            badgeView?.visibility = View.GONE
        } else {
            badgeView?.visibility = View.VISIBLE
            ImageUtils.display(badgeView, badgeUrl)
        }
    }
}