package com.gamedog.vididin.main.fragments.task.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.ama.core.architecture.util.ResUtil
import com.ama.core.architecture.util.setOnClickBatch
import com.gamedog.vididin.databinding.DailyTaskItemViewBinding


class DailyTaskItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var mRewardNum: Int = 0
    private var mNeedWatchTotal: Int = 0
    private lateinit var mAction: () -> Unit
    private var mBinding: DailyTaskItemViewBinding


    init {
        mBinding = DailyTaskItemViewBinding.inflate(LayoutInflater.from(context), this, true)
        mBinding.run {
            setOnClickBatch(tvAction) {
                when (this) {
                    tvAction-> {
                        mAction.invoke()
                    }
                }
            }
        }
    }



    fun initUI(titleIconRes: Int, titleRes: Int, videosNum: Int, goldReward: Int, actionRes: Int) {
        mRewardNum = goldReward
        mNeedWatchTotal = videosNum

        mBinding.ivItemIcon.setImageResource(titleIconRes)
        mBinding.tvItemTitle.text = String.format(ResUtil.getString(titleRes), videosNum)
        mBinding.tvAction.setText(actionRes)
        mBinding.tvRewardNum.text = "+$goldReward"
        mBinding.tvItemProgress.text = "(0/$videosNum)"
    }

    fun updateProgress(finishVideoNum: Int) {
        val showWatchedNum = if (finishVideoNum > mNeedWatchTotal) mNeedWatchTotal else finishVideoNum
        mBinding.tvItemProgress.text = "($showWatchedNum/$mNeedWatchTotal)"
        mBinding.itemProgress.setProgress(showWatchedNum/mNeedWatchTotal)
    }


    fun setActionFun(action: ()->Unit) {
        mAction = action
    }




}