package com.market.model;

import com.market.utils.SpotConstant;
import org.springframework.web.socket.WebSocketSession;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 现货 WebSocket 客户端会话
 *
 * @author system
 * @date 2026-05-07
 */
public class SpotClientSession {

    /** 客户端ID */
    private String id;

    /** WebSocket会话 */
    private WebSocketSession session;

    /**
     * 订阅的数据流
     * key: channel，value: 订阅参数
     */
    private Map<String, SubscribeData> subscriptions = new ConcurrentHashMap<>();

    /** 连接参数（queryString 解析结果） */
    private Map<String, String> parameters;

    /** 最后 Pong 时间（心跳检测用） */
    private LocalDateTime lastPongTime;

    /** 最后 Ping 时间 */
    private LocalDateTime lastPingTime;

    /** 创建时间 */
    private LocalDateTime createdTime;

    /** 请求消息队列（高优先级） */
    private BlockingQueue<String> reqQueue = new LinkedBlockingQueue<>(SpotConstant.REQ_QUEUE_SIZE);

    /** 广播消息队列（低优先级） */
    private BlockingQueue<String> broadcastQueue = new LinkedBlockingQueue<>(SpotConstant.BROADCAST_QUEUE_SIZE);

    /** 是否是 Flutter 客户端 */
    private boolean isFlutter;

    /** 已认证用户ID（未登录时为 null） */
    private String uid;

    public SpotClientSession(String id, WebSocketSession session) {
        this.id = id;
        this.session = session;
        this.lastPongTime = LocalDateTime.now();
        this.lastPingTime = LocalDateTime.now();
        this.createdTime = LocalDateTime.now();
    }

    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public WebSocketSession getSession() { return session; }
    public void setSession(WebSocketSession session) { this.session = session; }
    public Map<String, SubscribeData> getSubscriptions() { return subscriptions; }
    public void setSubscriptions(Map<String, SubscribeData> subscriptions) { this.subscriptions = subscriptions; }
    public Map<String, String> getParameters() { return parameters; }
    public void setParameters(Map<String, String> parameters) { this.parameters = parameters; }
    public LocalDateTime getLastPongTime() { return lastPongTime; }
    public void setLastPongTime(LocalDateTime lastPongTime) { this.lastPongTime = lastPongTime; }
    public LocalDateTime getLastPingTime() { return lastPingTime; }
    public void setLastPingTime(LocalDateTime lastPingTime) { this.lastPingTime = lastPingTime; }
    public LocalDateTime getCreatedTime() { return createdTime; }
    public void setCreatedTime(LocalDateTime createdTime) { this.createdTime = createdTime; }
    public BlockingQueue<String> getReqQueue() { return reqQueue; }
    public void setReqQueue(BlockingQueue<String> reqQueue) { this.reqQueue = reqQueue; }
    public BlockingQueue<String> getBroadcastQueue() { return broadcastQueue; }
    public void setBroadcastQueue(BlockingQueue<String> broadcastQueue) { this.broadcastQueue = broadcastQueue; }
    public boolean isFlutter() { return isFlutter; }
    public void setFlutter(boolean flutter) { isFlutter = flutter; }
    public String getUid() { return uid; }
    public void setUid(String uid) { this.uid = uid; }

    /**
     * 订阅数据
     */
    public static class SubscribeData {

        /** 订阅动作 */
        private String action;

        /** 订阅参数 */
        private Map<String, Object> params;

        /** 是否是 Flutter 客户端 */
        private boolean isFlutter;

        public String getAction() { return action; }
        public void setAction(String action) { this.action = action; }
        public Map<String, Object> getParams() { return params; }
        public void setParams(Map<String, Object> params) { this.params = params; }
        public boolean isFlutter() { return isFlutter; }
        public void setFlutter(boolean flutter) { isFlutter = flutter; }
    }
}
