import { TradeMode } from '@/common/constants/enumCommon';
import { initData } from '@/common/methods';
import APP from '@/services';
import { Ref, ref } from 'vue';
import { Goods } from '../go/ermcp/goodsInfo/interface';
import { QueryQuoteDayRsp } from '../go/quote/interface';
import { Goodsgroup } from '../go/useInfo/interface';
import { getMarketByTradeMode } from './market';

// 获取全部商品信息
export function getGoodsList(): Goods[] {
    return APP.get('Goods')
}

// 通过trademode 获取商品
export function getGoodsListByTrade(trademode: TradeMode): Ref<Goods[]> {
    const result = ref<Goods[]>([])
    initData(() => {
        const marketInfo = getMarketByTradeMode(trademode)
        if (marketInfo) {
            result.value = getGoodsList().filter(e => e.marketid === marketInfo.marketid)
        }
    })
    return result
}

// 通过trademode 获取行情，并获取goodsname
export function getQutoGoodsByTradeMode(trademode: TradeMode) {
    const result = ref<QueryQuoteDayRsp[]>([])
    initData(() => {
        // 获取市场信息
        const marketInfo = getMarketByTradeMode(trademode)
        if (marketInfo) {
            // 过滤商品信息
            const goodsList = getGoodsList().filter(e => e.marketid === marketInfo.marketid)
            goodsList.forEach(el => {
                // 找到行情数据
                const quote = getQuoteDayInfoByCode(el.goodscode);
                if (quote) {
                    Object.assign(quote, { goodsname: el.goodsname })
                    result.value.push(quote)
                }
            })
        }
    })
    return result
}

export function getGoodsByCode(goodsCode: string): Goods | undefined {
    return getGoodsList().find(e => e.goodscode === goodsCode)
}

// 获取全部盘面信息
export function getAllQuoteDayInfo(): QueryQuoteDayRsp[] {
    return APP.get('quoteDayInfo')
}

// 通过goodscode获取对应的商品盘面信息
export function getQuoteDayInfoByCode(goodsCode: string): QueryQuoteDayRsp | undefined {
    return getAllQuoteDayInfo().find(e => e.goodscode === goodsCode)
}

// 通过goodcode 获取商品最新价
export function getQuoteDayInfoByCodeFindPrice(goodsCode: string) {
    const goods = getGoodsByCode(goodsCode);
    let result: number | string = '--';
    if (goods) {
        const quoteData = getQuoteDayInfoByCode(goodsCode);
        // 期货合约取价顺序：现价－>实时结算价->昨结算价
        if (quoteData) {
            if (quoteData.last) {
                result = quoteData.last;
            } else if (quoteData.settle) {
                result = quoteData.settle;
            } else if (quoteData.presettle) {
                result = quoteData.presettle;
            } else {
                result = '--'
            }
        }
        // result = typeof result === 'number' ? Number(result.toFixed(goods?.decimalplace)) : result;
    }
    return result
}

// 获取全部商品组信息
export function getAllGoodsGroups(): Goodsgroup[] {
    return APP.get('goodsgroups')
}

export function getGoodsGroupsByTradeMode(trademode: TradeMode): Goodsgroup[] {
    const marketInfo = getMarketByTradeMode(trademode)
    let result: Goodsgroup[] = []
    if (marketInfo) {
        result = getAllGoodsGroups().filter(e => e.marketid === marketInfo.marketid)
    } else {
        console.warn(`trademode:${trademode}市场信息不存在`)
    }
    return result
}

// 通过goodsid 获取商品合约单位
export function getGoodsAgreeunitByGoodsId(goodsid: number): number | null {
    const result = getGoodsList().find(el => el.goodsid === goodsid)
    return result ? result.agreeunit : null
}