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.common.AppConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.clickAntiShake
import com.yidian.common.extensions.hide
import com.yidian.common.extensions.initTitleBar
import com.yidian.common.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
    }
  }
}
