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.R
import com.gamedog.vididin.databinding.DailyTaskItemViewBinding
import com.gamedog.vididin.manager.taskbeans.BaseTaskState.Companion.STATE_CLAIMED
import com.gamedog.vididin.manager.taskbeans.BaseTaskState.Companion.STATE_FINISH
import com.gamedog.vididin.manager.taskbeans.BaseTaskState.Companion.STATE_ONGOING


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) {
        mRewardNum = goldReward
        mNeedWatchTotal = videosNum

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

    fun updateProgress(watchedCount: Int, subTaskState: Int) {
        doUpdateProgress(watchedCount)

        with(mBinding.tvAction) {
            when(subTaskState) {
                STATE_ONGOING -> {
                    setText(R.string.go_and_do)
                    setBackgroundResource(R.drawable.bg_sub_task_normal)
                    isClickable = true
                    alpha = 1F
                }

                STATE_FINISH -> {
                    setText(R.string.resgatar)
                    setBackgroundResource(R.drawable.bg_sub_task_normal)
                    isClickable = true
                    alpha = 1F
                }

                STATE_CLAIMED -> {
                    setText(R.string.Resgatado)
                    setBackgroundResource(R.drawable.bg_sub_task_disable)
                    isClickable = false
                    alpha = 0.4F
                }
            }
        }
    }

    private fun doUpdateProgress(finishNum: Int) {
        val properShowNumForCurSubTask = if (finishNum > mNeedWatchTotal) mNeedWatchTotal else finishNum
        mBinding.tvItemProgress.text = "($properShowNumForCurSubTask/$mNeedWatchTotal)"
        mBinding.itemProgress.setProgress(100*properShowNumForCurSubTask/mNeedWatchTotal)
    }

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




}