package com.adealink.weparty.room.gift.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.SpannableStringBuilder
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.dialogfragment.BaseDialogFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.sp
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.module.wallet.WalletModule
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.DialogDiamondRechargeBinding
import com.adealink.weparty.room.datasource.local.RoomLocalService
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.R as APP_R

class DiamondRechargeDialog : BaseDialogFragment(R.layout.dialog_diamond_recharge) {

    private val binding by viewBinding(DialogDiamondRechargeBinding::bind)

    private val walletViewModel by fastLazy { WalletModule.getWalletViewModel(requireActivity()) }

    private var onConfirmClick: (() -> Unit)? = null

    private var todayNeverRemind = false

    fun setOnConfirmClick(onConfirmClick: (() -> Unit)? = null) {
        this.onConfirmClick = onConfirmClick
    }

    override fun resetWindowAttributes(window: Window) {
        super.resetWindowAttributes(window)
        window.setLayout(
            300.dp(),
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val attr = window.attributes
        attr.gravity = Gravity.CENTER
        attr.dimAmount = 0f
        window.attributes = attr
    }

    override fun initViews() {
        super.initViews()
        updateCoin(walletViewModel?.getCoin() ?: 0.0)
        updateExchangeRate()
        binding.topCloseBtn.onClick {
            dismiss()
        }
        binding.btnConfirm.onClick {
            if (todayNeverRemind) {
                RoomLocalService.lastDiamondRemindTs = System.currentTimeMillis()
            }
            onConfirmClick?.invoke()
            dismiss()
        }
        binding.btnCancel.onClick {
            dismiss()
        }
        binding.clNeverRemind.onClick {
            toggleNeverRemind()
        }
    }

    private fun updateCoin(coin: Double) {
        val coinStr = formatNumberStr(coin)
        val text = getCompatString(R.string.room_gift_coin_available, coinStr)
        binding.tvContent.text = SpannableStringBuilder(text).apply {
            findAndSetSpan(
                CenterImageSpan(
                    getCompatDrawable(APP_R.drawable.common_wallet_coin_ic).apply {
                        setBounds(0, 0, 14.dp(), 14.dp())
                    }
                ), "[coin]"
            )
        }
    }

    private fun updateExchangeRate() {
        val text = getCompatString(R.string.room_gift_exchange_rate)
        binding.tvExchanged.text = SpannableStringBuilder(text).apply {
            findAndSetSpan(
                CenterImageSpan(
                    getCompatDrawable(APP_R.drawable.common_wallet_coin_padding_ic).apply {
                        setBounds(0, 0, 11.sp(), 11.sp())
                    }
                ), "[coin]"
            )

            findAndSetSpan(
                CenterImageSpan(
                    getCompatDrawable(APP_R.drawable.common_wallet_diamond_padding_ic).apply {
                        setBounds(0, 0, 11.sp(), 11.sp())
                    }
                ), "[diamond]"
            )
        }
    }

    private fun toggleNeverRemind() {
        todayNeverRemind = !todayNeverRemind
        if (todayNeverRemind) {
            binding.ivNeverRemind.setImageResource(APP_R.drawable.common_check_selected_ic)
        } else {
            binding.ivNeverRemind.setImageResource(APP_R.drawable.common_check_unselected_ic)
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        walletViewModel?.coinLD?.observe(viewLifecycleOwner) {
            updateCoin(it)
        }
    }

    override fun loadData() {
        super.loadData()
    }

}