package com.bizzan.bitrade.config;

import com.bizzan.bitrade.client.Client;
import com.bizzan.bitrade.engine.ContractCoinMatch;
import com.bizzan.bitrade.engine.ContractCoinMatchFactory;
import com.bizzan.bitrade.engine.FollowAccountMonitor;
import com.bizzan.bitrade.engine.TradingAccountMonitor;
import com.bizzan.bitrade.entity.ContractCoin;
import com.bizzan.bitrade.handler.NettyHandler;
import com.bizzan.bitrade.handler.WebsocketMarketHandler;
import com.bizzan.bitrade.job.ExchangePushJob;
import com.bizzan.bitrade.service.*;
import com.bizzan.bitrade.util.RedisManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import java.util.List;

@Component
public class ContractCoinMatchStarter implements ApplicationListener<ContextRefreshedEvent> {

    private Logger log = LoggerFactory.getLogger(ContractCoinMatchStarter.class);

    @Autowired
    private Client client;

    @Autowired
    private ContractCoinService contractCoinService;

    @Autowired
    private ContractMarketService marketService;

    @Autowired
    private ExchangePushJob exchangePushJob;

    @Autowired
    private ContractOrderEntrustService contractOrderEntrusdtService;

    @Autowired
    private MemberTransactionService memberTransactionService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberContractWalletService memberContractWalletService;
    @Autowired
    private MemberContractPositionService memberContractPositionService;
    @Autowired
    private UserPurseService userPurseService;
    @Autowired
    private FollowWalletService followWalletService;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private RedisLock redisLock;
    @Autowired
    private RedisManager redisManager;
    @Autowired
    private TradingAccountMonitor tradingAccountMonitor;
    @Autowired
    private FollowAccountMonitor followAccountMonitor;
    @Autowired
    private KLineService kLineService;

    @Autowired
    private ExperienceGiftRecordService experienceGiftRecordService;


    @Autowired
    WebsocketMarketHandler wsHandler;

    @Autowired
    NettyHandler nettyHandler;

    @Autowired
    private ContractCoinMatchFactory factory;
    
    //private WebSocketBinance w;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        log.info("=================== 合约应用启动初始化 ===================");
        List<ContractCoin> contractCoinList = contractCoinService.findAllEnabled();

        for(ContractCoin coin : contractCoinList) {
        	addContractCoinMatch(coin);
        }

        log.info("========== 交易对数量：{}", factory.getMatchMap().size());
        
        tradingAccountMonitor.initAccounts();
        followAccountMonitor.initAccounts();
        //kLineService.init();

        // 设置WebSocket
        //WebSocketConnectionManage.setClient(client);

        /*WsClientHuobi w = new WsClientHuobi(factory);
        w.setContractCoinService(contractCoinService);
        w.setContractMarketService(marketService);
        w.setExchangePushJob(exchangePushJob);
        w.setTradingAccountMonitor(tradingAccountMonitor);
        w.run();*/
        //w = new WebSocketBinance(factory, tradingAccountMonitor, exchangePushJob, marketService, kLineService);
    }
    
    public void addContractCoinMatch(ContractCoin coin) {
    	ContractCoinMatch match = new ContractCoinMatch(coin.getSymbol());
        match.setContractCoinService(contractCoinService);
        match.setContractOrderEntrustService(contractOrderEntrusdtService);
        match.setMemberTransactionService(memberTransactionService);
        match.setMemberService(memberService);
        match.setMemberContractWalletService(memberContractWalletService);
        match.setMemberContractPositionService(memberContractPositionService);
        match.setUserPurseService(userPurseService);
        match.setFollowWalletService(followWalletService);
        match.setEntityManager(entityManager);
        match.addHandler(wsHandler);
        match.addHandler(nettyHandler);
        match.setExchangePushJob(exchangePushJob);
        match.setRedisLock(redisLock);
        match.setRedisManager(redisManager);
        match.setTradingAccountMonitor(tradingAccountMonitor);
        match.setKafkaTemplate(kafkaTemplate);
        match.setExperienceGiftRecordService(experienceGiftRecordService);
        match.run();
        factory.addContractCoinMatch(coin.getSymbol(), match);
    }
}
