package com.game.business.websocket.client;

import com.alibaba.fastjson2.JSONObject;
import com.game.business.config.GameOneConfig;
import com.game.business.util.Common;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;

@Component
@ClientEndpoint
public class GameOneClient {

    @OnOpen
    public void onOpen(Session session) throws Exception{
        System.out.printf("game one 游戏已连接 server");
        // 发送游戏编码
        session.getBasicRemote().sendText("{\"code\":\"" + Common.GAME_ONE_CODE + "\"}");
    }

    @OnClose
    public void onClose(Session session) throws Exception{
        System.out.printf("game on 游戏已断开 server");
        connect();
    }

    @OnError
    public void onError(Session session, Throwable throwable) throws Exception{
        System.out.printf("game one 连接异常 [" + throwable.getMessage() + "]");
        connect();
    }

    public void connect(){
        synchronized (this){
            try {
                GameOneConfig.webSocketConnent.connect();
            }catch (Exception e){
                System.out.printf("连接 game on [socket] 异常" + e.getMessage());
            }
        }
    }

    @OnMessage
    public void onMessage(Session session, String message){
        try {
            if(StringUtils.isBlank(message)){
                System.out.printf("game one 数据为空");
                return;
            }

            System.out.printf("game one 接收数据[" + message + "]");

            JSONObject jsonObject = JSONObject.parseObject(message);
            String code = jsonObject.getString("code");

            if(StringUtils.isBlank(code) || !code.equals(Common.GAME_ONE_CODE)){
                System.out.printf("game one 接收数据错误[" + message + "]");
                return;
            }

//            GameType gameType = new GameType();
//            gameType.setGameTypeCode(code);
//            GameType dbGameType = gameTypeService.selectGameType(gameType);
//            if(dbGameType == null){
//                System.out.printf("game one 游戏类型数据库不匹配");
//                return;
//            }
//
//            GameRecord record = JSONObject.parseObject(message, GameRecord.class);
//            record.setGameTypeId(dbGameType.getGameTypeId());
//            gameRecordService.insertGameRecord(record);
//
//            GameLottery gameLottery = new GameLottery();
//            gameLottery.setGameTypeId(dbGameType.getGameTypeId());
//            gameLottery.setGameDate(record.getGameDate());
//
//            GameLottery dbGameLottery = gameLotteryService.selectGameLottery(gameLottery);
//
//            if(dbGameLottery == null){
//                gameLotteryService.insertGameLottery(gameLottery);
//            }else{
//                if(record.getIsLottery() == 1) {
//                    dbGameLottery.setGameLotteryDate(record.getGameRecordDate());
//                    dbGameLottery.setGameLotterySucc(record.getGameLotterySucc());
//                    gameLotteryService.updateGameLottery(dbGameLottery);
//                }
//            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.printf("game one 接收数据异常[" + e.getMessage() + "]");
        }
    }
}
