package com.hongyegroup.cpt_rewards.ui

import android.content.Intent
import android.os.Bundle
import androidx.databinding.ViewDataBinding
import com.guadou.lib_baselib.base.activity.BaseVDBActivity
import com.guadou.lib_baselib.base.vm.BaseViewModel
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.view.MyScrollView
import com.hongyegroup.cpt_rewards.BR
import com.hongyegroup.cpt_rewards.R
import com.hongyegroup.cpt_rewards.databinding.ActivityRewardsDetailBinding

class RewardsDetailActivity : BaseVDBActivity<BaseViewModel, ActivityRewardsDetailBinding>() {

    var mHeight = 0f
    val mClickProxy by lazy { ClickProxy() }

    companion object {
        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, RewardsDetailActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun init(savedInstanceState: Bundle?) {

        initView()
        initListener()

    }


    override fun getDataBindingConfig(): DataBindingConfig {

        return DataBindingConfig(R.layout.activity_rewards_detail)
            .addBindingParams(BR.click, mClickProxy)

    }

    private fun initView() {

        CommUtils.getHandler().postDelayed(Runnable {

            mHeight = mBinding.clPromotionRewardsDetails.height.toFloat()

        }, 100)

    }

    private fun initListener() {

        mBinding.scrollViewPromotionRewardsDetail.setOnScrollListener(MyScrollView.OnScrollListener {

            if (it >= 0 && it <= mHeight) {
                var percent = 0f
                if (mHeight != 0f) {
                    percent = it / mHeight
                }
                mBinding.llPromotionRewardsDetailTitle.setBackgroundColor(evaluate(percent, CommUtils.getColor(R.color.transparent), CommUtils.getColor(R.color.white)))

            } else {
                mBinding.llPromotionRewardsDetailTitle.setBackgroundColor(CommUtils.getColor(R.color.white))
            }


        })

    }

    /**
     * 根据百分比和起始颜色和结束颜色 计算当前的RGBA颜色
     */
    fun evaluate(fraction: Float, startValue: Int, endValue: Int): Int {
        val startA = startValue shr 24 and 0xff
        val startR = startValue shr 16 and 0xff
        val startG = startValue shr 8 and 0xff
        val startB = startValue and 0xff
        val endA = endValue shr 24 and 0xff
        val endR = endValue shr 16 and 0xff
        val endG = endValue shr 8 and 0xff
        val endB = endValue and 0xff
        return startA + (fraction * (endA - startA)).toInt() shl 24 or
                (startR + (fraction * (endR - startR)).toInt() shl 16) or
                (startG + (fraction * (endG - startG)).toInt() shl 8) or
                startB + (fraction * (endB - startB)).toInt()
    }


    override fun startObserve() {

    }

    inner class ClickProxy {

        //跳转到兑换确认界面
        fun gotoRewardsConfirmActivity() {

            RewardsConfirmActivity.startInstance()

        }

    }

}