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() {
    // 把行情系统需要的数据  进行整理归类 在这里进行存储 goodsCode
    private var mGoodsInfoClass: HashMap<String, QuoteDayData>? = null

    fun getmGoodsInfoClass(): HashMap<String, QuoteDayData> {
        if (mGoodsInfoClass == null) {
            mGoodsInfoClass = HashMap()
        }
        return mGoodsInfoClass!!
    }

    /**
     * 获取错误信息
     * @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 (instance?.getAccountDataList() == null) {
                accountData = AccountData()
            } else {
                if (SPUtils.getInstance().getLong(Constant.SELECT_ACCOUNT_ID, 0) == 0L) {
                    accountData = instance!!.accountDataList[0]
                } else {
                    for (i in instance!!.getAccountDataList().indices) {
                        if (instance!!.accountDataList[i].accountid == SPUtils.getInstance()
                                .getString(Constant.SELECT_ACCOUNT_ID)
                                .toLong()
                        ) {
                            accountData = instance!!.accountDataList[i]
                        }
                    }
                }
            }
            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)
    }

    /**
     * 获取商品信息最后更新时间
     * @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
    }
}