package com.abbidot.tracker.widget

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.FrameLayout
import android.widget.RelativeLayout
import coil.load
import com.abbidot.tracker.R
import com.abbidot.tracker.databinding.ViewCreditCardLayoutBinding
import com.abbidot.tracker.util.Util


/**
 *Created  by .yzq on 2022/4/6/006.
 * @link
 * @description:
 */
class CreditCardView : FrameLayout {

    private lateinit var mViewBinding: ViewCreditCardLayoutBinding

    //信用卡前面的view
    private lateinit var mFrontView: RelativeLayout

    //信用卡后面的view
    private lateinit var mBehindView: RelativeLayout

    //信用卡类型图片
    private var mCardLogoImageId = 0

    //动画执行时间
    private val mDuration = 350L

    constructor(context: Context) : super(context) {
        init(context)
    }


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

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


    private fun init(context: Context) {
//        val inflater =
//            getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        val view = inflate(context, R.layout.view_credit_card_layout, this)
        mViewBinding = ViewCreditCardLayoutBinding.bind(view)

        mBehindView = mViewBinding.layoutBehindCreditCard.root
        mFrontView = mViewBinding.layoutFrontCreditCard.root
    }


    fun setCardNumber(numberStr: String) {
        val formatCardNumber = Util.formatCardNumber(numberStr)
        mCardLogoImageId = Util.getCreditCardType(numberStr)

        mViewBinding.layoutFrontCreditCard.apply {
            creditCardNumberText.text = formatCardNumber
            creditCardLogoImg.load(mCardLogoImageId)
        }
    }

    fun setCardholderName(cardholderName: String) {
        mViewBinding.layoutFrontCreditCard.frontCardHolderName.text = cardholderName
    }

    fun setCardExpiry(cardExpiry: String) {
        mViewBinding.layoutFrontCreditCard.frontCardExpiry.text = cardExpiry
    }

    fun setCVV(cvv: String) {
        mViewBinding.layoutBehindCreditCard.apply {
            backCardCvv.text = cvv
            backCardLogoImg.load(mCardLogoImageId)
        }
    }


    /**
     * 翻转背面动画
     */
    fun showBehindAnimation() {
        if (mBehindView.visibility == View.VISIBLE) return
        val animatorStart = ObjectAnimator.ofFloat(this, "rotationY", 0f, -90f)

        val animatorEnd = ObjectAnimator.ofFloat(this, "rotationY", 90f, 0f)
        animatorEnd.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
                mFrontView.visibility = View.GONE
                mBehindView.visibility = View.VISIBLE
                mViewBinding.layoutBehindCreditCard.backCardLogoImg.load(mCardLogoImageId)
            }

            override fun onAnimationEnd(animation: Animator) {

            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }
        })

        startAnimator(animatorStart, animatorEnd)
    }

    /**
     * 翻转正面动画
     */
    fun showFrontAnimation() {
        if (mFrontView.visibility == View.VISIBLE) return
        val animatorStart = ObjectAnimator.ofFloat(this, "rotationY", 0f, 90f)

        val animatorEnd = ObjectAnimator.ofFloat(this, "rotationY", -90f, 0f)
        animatorEnd.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
                mFrontView.visibility = View.VISIBLE
                mBehindView.visibility = View.GONE
            }

            override fun onAnimationEnd(animation: Animator) {
            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }
        })

        startAnimator(animatorStart, animatorEnd)
    }

    private fun startAnimator(animatorStart: Animator, animatorEnd: Animator) {
        val animatorSet = AnimatorSet()
        animatorSet.play(animatorStart).before(animatorEnd)
        animatorSet.duration = mDuration
        animatorSet.interpolator = AccelerateDecelerateInterpolator()
        animatorSet.start()
    }
}