package com.jxy.marketall.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.marketall.entity.BiAn;
import com.jxy.marketall.entity.BinanceKLine;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.sql.SQLOutput;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @Description TODO
 * @Date 2022/7/6 10:54
 * @Author 杜懿
 */
@Component
public class MyWebSocketClient {

    private BiAn biAn = new BiAn();

    public WebSocketClient getWS(String url){
        try{
            WebSocketClient wbc = new WebSocketClient(new URI(url)) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    //System.out.println("连接成功");
                }

                @Override
                public void onMessage(String message) {
                    //message转json
                    //System.out.println("message:"+message);
                    JSONObject jsonObject = JSON.parseObject(message);
                    String dataStr = JSONObject.toJSONString(jsonObject.get("data"));
                    //System.out.println("data:"+dataStr);
                    String kStr = JSONObject.toJSONString(JSON.parseObject(dataStr).get("k"));
                    //System.out.println("kData:"+kStr);

                    BinanceKLine binanceKLine = JSON.parseObject(kStr, BinanceKLine.class);

                    String volumeStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("v"));
                    Double volume = JSON.parseObject(volumeStr, Double.class);

                    String buyVolumeStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("V"));
                    Double buyVolume = JSON.parseObject(buyVolumeStr, Double.class);

                    String qualityStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("q"));
                    Double quality = JSON.parseObject(qualityStr, Double.class);

                    String buyQualityStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("Q"));
                    Double buyQuality = JSON.parseObject(buyQualityStr, Double.class);

                    String lowPriceStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("l"));
                    Double lowPrice = JSON.parseObject(lowPriceStr, Double.class);

                    String openTimeStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("t"));
                    Date openTime = JSON.parseObject(openTimeStr, Date.class);
                    Calendar openTimeCalendar = new GregorianCalendar();
                    openTimeCalendar.setTime(openTime);
                    openTimeCalendar.add(openTimeCalendar.HOUR,+8);
                    openTime = openTimeCalendar.getTime();

                    String closeTimeStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("T"));
                    Long closeTimeLong = JSON.parseObject(closeTimeStr, Long.class);
                    closeTimeLong +=1;

                    Date closeTime = new Date(closeTimeLong);
                    Calendar closeTimeCalendar = new GregorianCalendar();
                    closeTimeCalendar.setTime(closeTime);
                    closeTimeCalendar.add(closeTimeCalendar.HOUR,+8);
                    closeTime = closeTimeCalendar.getTime();

                    BiAn biAn = new BiAn();
                    biAn.setOpenTime(openTime);
                    biAn.setOpenPrice(binanceKLine.getO());
                    biAn.setHighPrice(binanceKLine.getH());
                    biAn.setLowPrice(lowPrice);
                    biAn.setClosePrice(binanceKLine.getC());
                    biAn.setVolume(volume);
                    biAn.setCloseTime(closeTime);
                    biAn.setQuality(quality);
                    biAn.setNum(binanceKLine.getN());
                    biAn.setBuyVolume(buyVolume);
                    biAn.setBuyQuality(buyQuality);
                    biAn.setSymbol(binanceKLine.getS());
                    //System.out.println("币安："+biAn);
                    setBiAn(biAn);

                }

                @Override
                public void onClose(int code, String reason, boolean remote) {

                }

                @Override
                public void onError(Exception ex) {

                }

            };
            wbc.connect();
            int count = 0;
            while (wbc.getReadyState().ordinal() == 0){
                try {
                    Thread.sleep(1000);
                }catch (Exception e){


                }
                count++;
                if(count > 10){
                    break;
                }
            }
            if(wbc.getReadyState().ordinal() == 1){
                return wbc;
            }
            return wbc;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public BiAn getData(){
        return biAn;
    }

    public void setBiAn(BiAn biAn){
        this.biAn = biAn;
    }
}
