package com.remax.visualnovel.widget.ui

import android.animation.Animator
import android.content.Context
import android.util.AttributeSet
import androidx.core.content.withStyledAttributes
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.WidgetItemLikeBinding
import com.remax.visualnovel.extension.changeLikedStatus
import com.remax.visualnovel.utils.EpalUtils
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 LikeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenConstraintLayout(context, attrs, defStyleAttr) {

    var binding: WidgetItemLikeBinding? = null

    companion object {
        private const val DARK = 1
        private const val TRANSPARENT = 2
    }

    init {
        binding = inflate(WidgetItemLikeBinding::inflate)
        context.withStyledAttributes(attrs, R.styleable.LikeView) {
            val bgType = getInt(R.styleable.LikeView_likeViewBg, DARK)
            binding?.run {
                root.changeBackground {
                    backgroundUIColorToken = context.getString(
                        when (bgType) {
                            TRANSPARENT -> {
                                R.string.color_transparent
                            }

                            else -> {
                                R.string.color_surface_element_dark_normal
                            }
                        }
                    )

                }
            }
        }
    }

    fun changeLike(isLike: Boolean, isAnim: Boolean = false) {
        binding?.run {
            when {
                !isLike -> {
                    widgetItemLottieLike.isVisible = false
                    widgetItemLikeIcon.changeLikedStatus(false)
                }

                isAnim -> {
                    widgetItemLikeIcon.isGone = true
                    widgetItemLottieLike.run {
                        isVisible = true
                        progress = 0f
                        addAnimatorListener(object : Animator.AnimatorListener {
                            override fun onAnimationStart(p0: Animator) {
                                isEnabled = false
                            }

                            override fun onAnimationEnd(p0: Animator) {
                                isEnabled = true
                                progress = 1f
                            }

                            override fun onAnimationCancel(p0: Animator) {

                            }

                            override fun onAnimationRepeat(p0: Animator) {

                            }
                        })
                        playAnimation()
                    }
                }

                else -> {
                    widgetItemLottieLike.isVisible = false
                    widgetItemLikeIcon.changeLikedStatus(true)
                }
            }
        }
    }

    fun setLikeNum(likedCount: Int?) {
        setLikeNum(likedCount?.toLong())
    }

    fun setLikeNum(likedCount: Long?) {
        binding?.widgetItemAlbumLikeNum?.text = EpalUtils.formatNumberAutoSize(likedCount ?: 0)
    }

}