/*******************************************************************************
 * Copyright (c) 2024-2025 R3944Realms. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 *     http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.r3944realms.dg_lab.websocket.sharedData;

import com.google.common.collect.Maps;
import com.r3944realms.dg_lab.api.websocket.message.data.PowerBoxData;
import com.r3944realms.dg_lab.api.websocket.sharedData.ISharedData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.Map;
import java.util.Timer;

/**
 * 服务器PowerBox共享数据
 */
public class ServerPowerBoxSharedData implements ISharedData,Cloneable {
    @SuppressWarnings("MethodDoesntCallSuperMethod")
    @Override
    public ServerPowerBoxSharedData clone() {
        ServerPowerBoxSharedData clone = new ServerPowerBoxSharedData(punishmentDuration, punishmentTime);
        clone.channels.addAll(channels);
        clone.channelIdMap.putAll(channelIdMap);
        clone.powerBoxDataMap.putAll(powerBoxDataMap);
        clone.connections.putAll(connections);
        clone.relations.putAll(relations);
        clone.clientTimers.putAll(clientTimers);
        //不会给计时器(因为毫无意义)
        return clone;
    }

    /**
     * The Channels.
     */
    public final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    /**
     * The Channel id map.
     */
// 映射表
    public final Map<String, String> channelIdMap = Maps.newConcurrentMap();
    /**
     * The Power box data map.
     */
// targetId -> PowerData
    public final Map<String, PowerBoxData> powerBoxDataMap = Maps.newConcurrentMap();

    /**
     * The Connections.
     */
// 储存已连接的用户及其标识
    public final Map<String, ChannelHandlerContext> connections = Maps.newConcurrentMap();
    /**
     * The Relations.
     */
// 存储消息关系
    public final Map<String, String> relations = Maps.newConcurrentMap();
    /**
     * The Client timers.
     */
// 存储定时器
    public final Map<String, Timer> clientTimers = Maps.newConcurrentMap();
    /**
     * The Punishment duration.
     */
//默认发送时间1秒
    public final int punishmentDuration;
    /**
     * The Punishment time.
     */
// 默认一秒发送1次
    public final int punishmentTime;
    /**
     * The Heart timer.
     */
// 心跳定时器（该为线程安全的类）
    public Timer heartTimer = null;

    /**
     * Instantiates a new Server power box shared data.
     */
    public ServerPowerBoxSharedData() {
        this(5, 1);
    }

    /**
     * Instantiates a new Server power box shared data.
     *
     * @param punishmentDuration the punishment duration
     * @param punishmentTime     the punishment time
     */
    public ServerPowerBoxSharedData(int punishmentDuration, int punishmentTime) {
        this.punishmentDuration = punishmentDuration > 0 ? punishmentDuration : 5;
        this.punishmentTime = punishmentTime> 0 ? punishmentTime : 1;
    }

}
