package com.jxy.marketall.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jxy.marketall.dto.BiAnAppWSDto;
import com.jxy.marketall.entity.BiAn;
import com.jxy.marketall.entity.BinanceKLine;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

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

    public BiAnAppWSDto dto = new BiAnAppWSDto();

    public WebSocketClient getWS(String url){
        try{
            WebSocketClient wbc = new WebSocketClient(new URI(url)) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                }

                @Override
                public void onMessage(String message) {

                    JSONObject jsonObject = JSON.parseObject(message);

                    String dateStr = JSONObject.toJSONString(jsonObject.get("E"));
                    Long dateLong = JSON.parseObject(dateStr, Long.class);

                    Date date = new Date(dateLong);

                    String kStr = JSONObject.toJSONString(jsonObject.get("k"));

                    String openPriceStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("o"));
                    BigDecimal openPrice = JSON.parseObject(openPriceStr, BigDecimal.class);

                    String closePriceStr = JSONObject.toJSONString(JSON.parseObject(kStr).get("c"));
                    BigDecimal closePrice = JSON.parseObject(closePriceStr, BigDecimal.class);

                   BigDecimal changeRate = closePrice.subtract(openPrice).divide(openPrice,4,BigDecimal.ROUND_HALF_UP);



                    BiAnAppWSDto biAnData = new BiAnAppWSDto();
                    biAnData.setDate(date);
                    biAnData.setClosePrice(closePrice);
                    biAnData.setChangeRate(changeRate);

                    setData(biAnData);

                }

                @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 BiAnAppWSDto getData(){
        return this.dto;
    }



    public void setData(BiAnAppWSDto dto){
        this.dto = dto;
    }
}
