package cn.muchinfo.rma.global

import cn.muchinfo.rma.global.data.AccountData
import cn.muchinfo.rma.global.data.QuoteDayData
import cn.muchinfo.rma.view.base.app.Constant
import com.blankj.utilcode.util.SPUtils
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock

class GlobalDataCollection : BaseGlobalData() {

    /**
     * 获取错误信息
     * @param errorcode
     * @return
     */
    fun getErrorString(errorcode: String): String {
        var errorString = ""
        val dataList = errorCodeEntityList
        for (i in dataList.indices) {
            if (errorcode == dataList[i].errorcode) {
                errorString = dataList[i].description
            }
        }
        return errorString
    }

    /**
     * 获取外部资金账号
     */
    val accountId: Long
        get() {
            val accountId: Long
            accountId = if (instance
                    ?.getAccountDataList() == null || instance!!.getAccountDataList().isEmpty()
            ) {
                0L
            } else {
                if (SPUtils.getInstance()
                        .getLong(Constant.SELECT_ACCOUNT_ID, 0) == 0L
                ) {
                    instance!!.accountDataList[0].accountid
                } else {
                    SPUtils.getInstance()
                        .getLong(Constant.SELECT_ACCOUNT_ID)
                }
            }
            return accountId
        }

    /**
     * 获取当前选择的资金账户信息
     * @return
     */
    val accountData: AccountData?
        get() {
            var accountData: AccountData? = null
            if (accountDataList == null) {
                accountData = AccountData()
            } else {
                if (SPUtils.getInstance().getLong(Constant.SELECT_ACCOUNT_ID, 0) == 0L) {
                    if (instance?.accountDataList?.size != 0){
                        accountData = instance?.accountDataList?.get(0)
                    }
                } else {
                    for (i in accountDataList.indices) {
                        if (accountDataList[i].accountid == SPUtils.getInstance().getLong(Constant.SELECT_ACCOUNT_ID)
                        ) {
                            accountData = accountDataList[i]
                        }
                    }
                }
                // 还是没找到，就去第一个
                if (accountData == null) {
                    if (accountDataList.isNotEmpty()){
                        accountData = accountDataList[0]
                        SPUtils.getInstance().put(Constant.SELECT_ACCOUNT_ID, accountData.accountid)
                    }
                }
            }
            return accountData
        }

    /**
     * 一个月时间差
     *
     * @return
     */
    fun oneMonth(): String {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_MONTH, -7)
        val format = SimpleDateFormat("yyyy-MM-dd")
        return format.format(calendar.time)
    }

    /**
     * 七天时间差
     *
     * @return
     */
    fun curWeek(): String {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_MONTH, -7)
        val format = SimpleDateFormat("yyyy-MM-dd")
        return format.format(calendar.time)
    }

    /**
     * 当前的账号
     */
    var currentAccountId: String? = null
        get() {
            return field ?: loginRsp.accountIDsList[0].toString()
        }

    /**
     * 获取商品信息最后更新时间
     * @return
     */
    val goodsInfoUpdateTime: String
        get() {
            val systemParamsArrayList =
                instance!!.loginQueryData.systemParams
            var updateTime = ""
            for (j in systemParamsArrayList!!.indices) {
                val (_, _, paramcode, _, _, _, _, _, _, _, _, updatetime) = systemParamsArrayList[j]
                if (paramcode == "023") {
                    updateTime = updatetime
                }
            }
            return updateTime
        }

    companion object {
        private val _lock: Lock =
            ReentrantLock()
        var instance: GlobalDataCollection? = null
            get() {
                if (field == null) {
                    _lock.lock()
                    field = GlobalDataCollection()
                    _lock.unlock()
                }
                return field
            }
            private set
    }
}