/*******************************************************************************
 * 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.utils.timeTask;


import com.r3944realms.dg_lab.utils.annoation.NeedCompletedInFuture;
import com.r3944realms.dg_lab.api.websocket.message.Message;
import com.r3944realms.dg_lab.api.websocket.message.PowerBoxMessage;
import com.r3944realms.dg_lab.api.websocket.message.role.PlaceholderRole;
import com.r3944realms.dg_lab.api.websocket.message.role.WebSocketClientRole;
import io.netty.channel.ChannelHandlerContext;

import java.util.TimerTask;
import java.util.function.Consumer;

/**
 * The type Dg lab timer task.
 */
public class DgLabTimerTask extends TimerTask {
    /**
     * The Client.
     */
    final ChannelHandlerContext client;
    /**
     * The Target.
     */
    final ChannelHandlerContext target;
    /**
     * The Client id.
     */
    String clientId, /**
     * The Target id.
     */
    targetId;
    /**
     * The Total sends.
     */
    Integer totalSends;
    /**
     * The Send message data.
     */
    final Message sendMessageData;
    /**
     * The Timer consumer.
     */
    final Consumer<Object> timerConsumer;
    /**
     * The Channel.
     */
    final char channel;

    /**
     * Instantiates a new Dg lab timer task.
     *
     * @param client          the client
     * @param target          the target
     * @param sendMessageData the send message data
     * @param totalSends      the total sends
     * @param timerConsumer   the timer consumer
     * @param channel         the channel
     */
    public DgLabTimerTask(ChannelHandlerContext client, ChannelHandlerContext target, Message sendMessageData, Integer totalSends, Consumer<Object> timerConsumer, char channel) {
        this.channel = channel;
        this.client = client;
        this.target = target;
        this.sendMessageData = sendMessageData;
        this.totalSends = totalSends;
        this.timerConsumer = timerConsumer;
    }

    /**
     * Sets id.
     *
     * @param clientId the client id
     * @param targetId the target id
     */
    public void setId(String clientId, String targetId) {
        this.clientId = clientId;
        this.targetId = targetId;
    }

    @Override
    public void run() {
        if(totalSends > 0) {//一一个固定频率发送特定的客户端
            send(target,sendMessageData);
            totalSends--;
        }
        if(totalSends <= 0) {//达到发送上限
            PowerBoxMessage over = PowerBoxMessage.createPowerBoxMessage("clientMsg", clientId, targetId, "send-over", new PlaceholderRole("Server"), new WebSocketClientRole("Cl" + clientId));
            send(client, over);
            timerConsumer.accept(null);
        }
    }
    @NeedCompletedInFuture
    private void send(ChannelHandlerContext target, Message message) {
        target.writeAndFlush(message.getDataJson());
    }
}
