package com.gamedog.vididin.features.withdraw.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.gamedog.vididin.manager.WithdrawManager
import com.viddin.videos.free.R
import com.viddin.videos.free.databinding.WithdrawItemViewBinding as ViewBinding


class WithDrawItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var mItemIndex: Int = 0
    private var mCashNum: Double = 0.0
    private var mBinding: ViewBinding


    init {
        mBinding = ViewBinding.inflate(LayoutInflater.from(context), this, true)
    }


    /**
     * For withdraw number in top area
     */
    fun setNumAndAction(itemIndex: Int, cashNum: Double, clickAction: (Int)->Unit) {
        mItemIndex = itemIndex
        mCashNum = cashNum
        mBinding.tvWithdrawNum.text = ResUtil.getString(R.string.cash) + " " +
                if (cashNum.toString().endsWith(".0")) cashNum.toString().substring(0, cashNum.toString().indexOf(".0")) else cashNum.toString()
        mBinding.tvSacar.setOnClickListener {
            clickAction.invoke(mItemIndex)
        }

        updateProgressAndButUI()
    }


    fun getCashNum(): Double {
        return mCashNum
    }

    fun updateProgressAndButUI() {
        var itemProgress = WithdrawManager.instance().getItemProgress(mItemIndex) * 100
        //var itemState = WithdrawManager.instance().getItemState(mItemIndex)

        with(mBinding) {
            progressBar.setProgress(itemProgress.toInt())
            tvProgress.text = String.format("%.2f", itemProgress) + ResUtil.getString(R.string.percent)

            var canClickable: Boolean = itemProgress >= 100F
            tvSacar.isClickable = canClickable
            tvSacar.alpha = if (canClickable) 1F else 0.5F
        }
    }


}