package com.yidian.shenghuoquan.newscontent.wallet.ui

import android.os.Bundle
import android.text.TextWatcher
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.yidian.bcommon.AppConfig
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.bcommon.extensions.hide
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.extensions.show
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ActivityWithdrawBinding
import com.yidian.shenghuoquan.newscontent.ui.ProtocolActivity
import com.yidian.shenghuoquan.newscontent.utils.TextWatcherAdapter
import com.yidian.xpage.XPageManager
import java.lang.Exception

/**
 * 钱包 - 提现
 */
class WithdrawActivity : BaseActivity<ActivityWithdrawBinding>() {

  private var availableBalance: Double = 0.00

  override fun createViewBinding(): ActivityWithdrawBinding {
    return ActivityWithdrawBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return XRouterPathConstants.WITHDRAW
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initView()
    initClick()
  }

  private fun initView() {
    initTitleBar(viewBind.include.toolbar, viewBind.include.tvTitle, "提现")
    // 初始化或者请求用户头像、昵称、手机号、费率、可用余额等
  }

  private fun initClick() {
    // 监听金额输入
    viewBind.etAmount.addTextChangedListener(object : TextWatcherAdapter() {
      override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if (!s.isNullOrBlank()) {
          viewBind.ivClearAmount.show()
          try {
            val enterAmount = s.toString().toDouble()
            if (enterAmount > availableBalance) {
              viewBind.llBalanceAndFee.hide()
              viewBind.tvEnterError.show()
              setChangeButtonStatus(false)
            } else {
              viewBind.llBalanceAndFee.show()
              viewBind.tvEnterError.hide()
              setChangeButtonStatus(true)
              // 计算手续费或者请求接口返回手续费

            }
          } catch (e: Exception) {
            e.printStackTrace()
          }
        } else {
          viewBind.ivClearAmount.hide()
        }
      }
    })
    // 输入金额清空
    viewBind.ivClearAmount.clickAntiShake {
      viewBind.etAmount.setText("")
    }
    // 全部提现
    viewBind.tvWithdrawAll.clickAntiShake {
      viewBind.etAmount.setText(availableBalance.toString())
    }
    // 提现
    viewBind.tvToWithdraw.clickAntiShake {
      // 请求提现接口，提现成功返回我的钱包，并刷新钱包余额

    }
  }

  private fun setChangeButtonStatus(flag: Boolean) {
    if (flag) {
      viewBind.tvToWithdraw.setTextColor(ContextCompat.getColor(this, R.color.color_1852F1))
      viewBind.tvToWithdraw.isEnabled = true
    } else {
      viewBind.tvToWithdraw.setTextColor(ContextCompat.getColor(this, R.color.color_661852F1))
      viewBind.tvToWithdraw.isEnabled = false
    }
  }
}
