package com.game.business.websocket;


import com.game.business.config.GameTwoConfig;
import com.game.business.config.SpringContextSetting;
import com.game.business.util.Common;
import com.game.business.websocket.client.*;
import com.game.common.utils.spring.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.websocket.ContainerProvider;
import java.net.URI;

public class WebSocketConnent {

    private String WS_URL;
    private String WS_REMARK_URL;
    private String WS_NOW_URL;
    private String WS_URL_NAME;
    private int WS_CONNENT_NUM = 0;
    private int WS_CONNENT_MAX_NUM = 10;

    public WebSocketConnent(){}

    public WebSocketConnent(String WS_URL, String WS_REMARK_URL, String WS_URL_NAME){
        this.WS_URL = WS_URL;
        this.WS_REMARK_URL = WS_REMARK_URL;
        this.WS_URL_NAME = WS_URL_NAME;
    }

    public void connect() throws Exception{
        Environment environment = SpringUtils.getBean(Environment.class);
        while (true){
            try {
                System.out.println("正在" + (this.WS_CONNENT_NUM == 0 ? "" : ("第" + this.WS_CONNENT_NUM + "次")) + "连接WebSocket[" + this.WS_URL_NAME + "]......");

                if(StringUtils.isBlank(WS_NOW_URL)){
                    WS_NOW_URL = WS_URL;
                }else{
                    if(WS_NOW_URL.equals(WS_URL)){
                        WS_NOW_URL = WS_REMARK_URL;
                    }else{
                        WS_NOW_URL = WS_URL;
                    }
                }

                if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
                    GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
                    GameTwoClient client = SpringContextSetting.getBean(GameTwoClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
                    GameThreesClient client = SpringContextSetting.getBean(GameThreesClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
                    GameFourClient client = SpringContextSetting.getBean(GameFourClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GAME_FIVE_NAME.equals(this.WS_URL_NAME)){
                    GameFiveClient client = SpringContextSetting.getBean(GameFiveClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GAME_SIX_NAME.equals(this.WS_URL_NAME)){
                    GameSixClient client = SpringContextSetting.getBean(GameSixClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GAME_SEVEN_NAME.equals(this.WS_URL_NAME)){
                    GameSevenClient client = SpringContextSetting.getBean(GameSevenClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }else if(Common.GAME_EIGHT_NAME.equals(this.WS_URL_NAME)){
                    GameEightClient client = SpringContextSetting.getBean(GameEightClient.class);
                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                }

                System.out.println(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_NOW_URL + "]");
                this.WS_CONNENT_NUM = 0;
                break;
            }catch (Exception e){
                if(this.WS_CONNENT_MAX_NUM > this.WS_CONNENT_NUM){
                    this.WS_CONNENT_NUM ++;
                }
                System.out.println("连接[" + this.WS_URL_NAME + "]异常，" + this.WS_CONNENT_NUM + "秒后尝试重新连接：" + e.getMessage());
                Thread.sleep(this.WS_CONNENT_NUM * 1000);
            }
        }
    }
}
