package com.gamedog.vididin.core.login.login

import com.ama.core.architecture.util.DateUtil
import com.ama.core.architecture.util.DeviceUtil
import com.ama.core.architecture.util.SpUtil
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.beans.Account
import com.gamedog.vididin.beans.BankInfo


object AccountManager {

    private val mAccount: Account? by lazy {
        var account = SpUtil.instance().getObject<Account>(SpUtil.KEY_ACCOUNT)
        if (account == null) {
            val deviceUUId = DeviceUtil.generateDeviceId()
            account = Account(accountId = "user_$deviceUUId", deviceUUId=deviceUUId, createdAt = DateUtil.getCurTimeMs())
            account.let {
                SpUtil.instance().putObject(SpUtil.KEY_ACCOUNT, account)
            }
        }
        account
    }

    private fun saveAccountInfo() {
        SpUtil.instance().putObject(SpUtil.KEY_ACCOUNT, mAccount)
    }

    fun getAccount() : Account? {
        return mAccount
    }

    fun getGold(): Long {
        return mAccount?.goldCount ?: 0L
    }

    fun getCash(): Float {
        return mAccount?.cashCount ?: 0F
    }

    fun addGold(newGold: Int) {
        mAccount?.goldCount += newGold
        saveAccountInfo()
        NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Gold_Changed, null)
    }

    fun addCash(newCash: Float) {
        mAccount?.cashCount += newCash
        saveAccountInfo()
        NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Cash_Changed, null)
    }

    fun getBankInfo(): BankInfo? {
        return mAccount?.bankInfo
    }

    fun hasValidBankInfo(): Boolean {
        val backInfo = getBankInfo()
        return !backInfo?.bankName.isNullOrEmpty() && !backInfo.bankAccount.isNullOrEmpty()
    }

    fun saveBankAccount(bankAccount: String?) {
        if (bankAccount.isNullOrEmpty()) {
            mAccount?.bankInfo = null
        } else {
            mAccount?.bankInfo = BankInfo(bankAccount=bankAccount)
        }
        saveAccountInfo()
        NotifyMan.instance().sendEvent(
            VididinEvents.Event_Account_Bank_Info_Changed, NotifyMan.NotifyData(bankAccount))
    }

}

