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.setOnClickBatch
import com.viddin.videos.free.R
import com.gamedog.vididin.VidiConst
import com.viddin.videos.free.databinding.BeginnerTaskItemViewBinding
import com.gamedog.vididin.main.fragments.task.Task


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


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


    fun initUI(titleIconRes: Int, titleRes: Int, actionRes: Int) {
        mBinding.ivItemIcon.setImageResource(titleIconRes)
        mBinding.tvItemTitle.setText(titleRes)
        mBinding.tvAction.setText(actionRes)
    }


    fun setRewardInfo(taskReward: Task) {
        val isRewardGold = VidiConst.GOLD_IN_CONFIG == taskReward.reward_type;
        mBinding.ivRewardIcon.setImageResource(if (isRewardGold) R.mipmap.icon_glod_small else R.mipmap.icon_cash_s)
        mBinding.tvRewardNum.text = taskReward.reward_value.toString()
    }


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

    fun setRewardTextStyle(textSize: Int, color: Int) {
        mBinding.tvRewardNum.textSize = textSize.toFloat()
        mBinding.tvRewardNum.setTextColor(color)
    }

    fun setTaskStateInfo(
        rewardNum: String,
        textRes: Int,
        describleStr: String,
        hasClaimReward: Boolean
    ) {
        mBinding.tvRewardNum.text = rewardNum
        mBinding.tvAction.setText(textRes)
        mBinding.tvItemDescrible.text = describleStr
        mBinding.tvAction.isClickable = !hasClaimReward
        if (hasClaimReward) {
            mBinding.tvAction.apply {
                setBackgroundResource(R.drawable.bg_sub_task_disable)
                isClickable = false
                alpha = 0.4F
            }
        }
    }


}