package com.bizzan.bitrade.consumer;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bizzan.bitrade.engine.ContractCoinMatchFactory;
import com.bizzan.bitrade.engine.TradingAccountMonitor;
import com.bizzan.bitrade.entity.CoinThumb;
import com.bizzan.bitrade.job.ExchangePushJob;
import com.bizzan.bitrade.service.KLineService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MarketConsumer {

	@Autowired
	private ContractCoinMatchFactory matchFactory;
	@Autowired
    private TradingAccountMonitor tradingAccountMonitor;
	@Autowired
    private KLineService kLineService;
    
	//@KafkaListener(topics = {"market-contract"})
    public void onMarketChange(String content) {
    	if (StringUtils.isEmpty(content)) {
            return;
        }
        List<CoinThumb> list = JSON.parseArray("["+content+"]", CoinThumb.class);
        for(CoinThumb thumb : list) {
	        String symbol = thumb.getSymbol();
        	if(!this.matchFactory.containsContractCoinMatch(symbol))
        		continue;
	        this.kLineService.updateThumb(symbol, thumb);
            this.matchFactory.getContractCoinMatch(symbol).refreshThumb(thumb);

            // 委托触发 or 爆仓
            this.matchFactory.getContractCoinMatch(symbol).refreshPrice(thumb);
        }
    }
}
