package com.gamedog.vididin.features.benefit.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.ama.core.architecture.util.setOnClickBatch
import com.gamedog.vididin.R
import com.gamedog.vididin.databinding.BenefitTaskItemViewBinding
import com.gamedog.vididin.manager.TaskManager
import com.gamedog.vididin.manager.taskbeans.TaskStateBoxSub


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


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

        with(mBinding.progressBar) {
            setMax(100)
            setProgress(0)
        }
    }

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


    fun updateUIByState(subTaskState: TaskStateBoxSub, boxIndex: Int, subTaskIndex: Int) {
        with (mBinding) {
            val finishNum = if (subTaskState.finishedNum > subTaskState.required_count) subTaskState.required_count else subTaskState.finishedNum
            val progressNum: Int = finishNum * 100 / subTaskState.required_count

            //ivItemIcon.setImageResource(R.mipmap.icon_cash_s)
            tvItemTitle.setText(TaskManager.instance().boxTaskStatus().getSubTaskHintStrRes(boxIndex, subTaskIndex))
            progressBar.setProgress(progressNum)
            tvProgressInfo.text = "($finishNum/${subTaskState.required_count})"

            with (tvAction) {
                if (progressNum < 100) {
                    isClickable = true
                    setBackgroundResource( R.drawable.bg_benefit_item_action_bg)
                    setText(R.string.go_and_do)
                } else {
                    isClickable = false
                    setBackgroundResource( R.drawable.bg_sub_task_disable)
                    setText(R.string.finished)
                }
            }
        }
    }


}