package com.remax.visualnovel.widget.ui.lock

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.WidgetLockTagBinding
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.setMargin
import com.remax.visualnovel.manager.login.LoginManager
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.view.UITokenLinearLayout
import com.dylanc.viewbinding.nonreflection.inflate

/**
 * Created by HJW on 2022/8/31
 *
 */
class LockViewGroup @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenLinearLayout(context, attrs, defStyleAttr) {

    private var binding: WidgetLockTagBinding? = null

    private val small = 1
    private val large = 2

    init {
        binding = inflate(WidgetLockTagBinding::inflate)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.LockViewGroup)
        val radiusToken = typedArray.getString(R.styleable.LockViewGroup_radiusToken) ?: ""
        val size = typedArray.getInt(R.styleable.LockViewGroup_lockViewGroupSize, small)
        typedArray.recycle()
        binding?.run {
            group.changeBackground {
                this.radiusToken = radiusToken
            }
            setSize(size)
        }
    }

    fun setSize(size: Int) {
        binding?.run {
            when (size) {
                small -> {
                    unlockGroup.isVisible = false
                    with(lockIv) {
                        textSize = 24f
                        updateLayoutParams<ConstraintLayout.LayoutParams> {
                            bottomToTop = R.id.lockTv
                            bottomToBottom = ConstraintLayout.LayoutParams.UNSET
                        }
                        setMargin(bottomMargin = 6.dp)
                    }
                    with(lockTv) {
                        isVisible = true
                        setSizeType(R.string.txt_label_m, 16.dp)
                        setMargin(bottomMargin = 0, topMargin = 6.dp)
                        updateLayoutParams<ConstraintLayout.LayoutParams> {
                            topToBottom = R.id.lockIv
                            bottomToTop = ConstraintLayout.LayoutParams.UNSET
                        }
                    }
                }

                large -> {
                    unlockGroup.isVisible = true
                    with(lockIv) {
                        textSize = 40f
                        updateLayoutParams<ConstraintLayout.LayoutParams> {
                            bottomToTop = ConstraintLayout.LayoutParams.UNSET
                            bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
                        }
                        setMargin(bottomMargin = 0.dp)
                    }
                    with(lockTv) {
                        isVisible = false
                        setSizeType(R.string.txt_label_l, 16.dp)
                        setMargin(bottomMargin = 24.dp, topMargin = 0)
                        updateLayoutParams<ConstraintLayout.LayoutParams> {
                            topToBottom = ConstraintLayout.LayoutParams.UNSET
                            bottomToTop = R.id.unlockGroup
                        }
                    }
                }
            }
        }
    }

    /*fun setMyBalance() {
        // 我的钱包余额
        binding?.unlockPriceView?.setPrice(formatPrice(WalletManager.balance))
    }*/

    @SuppressLint("SetTextI18n")
    fun setPreviewUnlockInfo(unlockPrice: Long?, clickUnlock: (() -> Unit)? = null) {
        binding?.let {
            val priceTxt = "${formatPrice(unlockPrice)} ${context.getString(R.string.unlock)}"
            it.lockTv.setPrice(priceTxt)
            it.buttonIcon.setTxtAndClick(priceTxt) {
                LoginManager.checkLogin {
                    clickUnlock?.invoke()
                }
            }
        }
    }


}