package com.mx.cneeds.server.wechat.websocket.message.handler.auth;

import com.mx.cneeds.server.wechat.common.codec.Invocation;
import com.mx.cneeds.server.wechat.common.dispatcher.MessageHandler;
import com.mx.cneeds.server.wechat.websocket.config.NettyChannelManager;
import com.mx.cneeds.server.wechat.websocket.message.pojo.auth.AuthRequest;
import com.mx.cneeds.server.wechat.websocket.message.pojo.auth.AuthResponse;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class AuthRequestHandler implements MessageHandler<AuthRequest> {

    @Autowired
    private NettyChannelManager nettyChannelManager;

    @Override
    public void execute(Channel channel, AuthRequest authRequest) {
        // 如果未传递 accessToken
        if (StringUtils.isEmpty(authRequest.getAccessToken())) {
            AuthResponse authResponse = new AuthResponse().setCode(1).setMessage("认证 accessToken 未传入");
            channel.writeAndFlush(new Invocation(AuthResponse.TYPE, authResponse));
            return;
        }

        // 添加到 WebSocketUtil 中
        // 考虑到代码简化，我们先直接使用 accessToken 作为 User
        nettyChannelManager.addUser(channel, authRequest.getAccessToken());

        // 判断是否认证成功。这里，假装直接成功
        AuthResponse authResponse = new AuthResponse().setCode(0);
        channel.writeAndFlush(new Invocation(AuthResponse.TYPE, authResponse));
    }

    @Override
    public String getType() {
        return AuthRequest.TYPE;
    }

}
