/*
 * Copyright 2008-2026 Async-IO.org
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.atmosphere.cpr;

import org.atmosphere.cache.UUIDBroadcasterCache;
import org.atmosphere.util.SimpleBroadcaster;

import jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class BroadcasterListenerTest {
    private AtmosphereFramework framework;
    private static final AtomicBoolean completed = new AtomicBoolean();
    private static final AtomicBoolean postCreated = new AtomicBoolean();
    private static final AtomicBoolean preDestroyed = new AtomicBoolean();
    private static final AtomicBoolean onMessage = new AtomicBoolean();

    @BeforeEach
    public void create() throws Throwable {
        framework = new AtmosphereFramework();
        framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
        framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

            @Override
            public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
                return suspended(req, res);
            }

            public void action(AtmosphereResourceImpl r) {
                try {
                    resumed(r.getRequest(), r.getResponse());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ServletException e) {
                    e.printStackTrace();
                }
            }
        }).addBroadcasterListener(new L());
    }

    @AfterEach
    public void after() {
        BAR.count.set(0);
        framework.destroy();
    }

    public final static class L extends BroadcasterListenerAdapter {

        @Override
        public void onPostCreate(Broadcaster b) {
            postCreated.set(true);
        }

        @Override
        public void onComplete(Broadcaster b) {
            completed.set(true);
        }

        @Override
        public void onPreDestroy(Broadcaster b) {
            preDestroyed.set(true);
        }

        @Override
        public void onMessage(Broadcaster b, Deliver d) {
            onMessage.set(true);
        }
    }

    @Test
    public void testGet() throws IOException, ServletException {
        framework.addAtmosphereHandler("/*", new AR()).init();
        AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
        framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
        assertTrue(completed.get());
        assertTrue(postCreated.get());
        assertTrue(preDestroyed.get());
        assertTrue(onMessage.get());
    }

    @Test
    public void testOnBroadcast() throws IOException, ServletException {
        framework.addAtmosphereHandler("/*", new BAR()).init();

        AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
        framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
        assertEquals(1, BAR.count.get());
    }

    @Test
    public void testOnRemove() throws IOException, ServletException {
        framework.addAtmosphereHandler("/*", new BAR()).init();

        AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
        framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
        assertEquals(1, BAR.count.get());
    }

    @Test
    public void testLongPollingOnBroadcast() throws IOException, ServletException {
        framework.addAtmosphereHandler("/*", new BAR()).init();

        Map<String, String> m = new HashMap<String, String>();
        m.put(HeaderConfig.X_ATMOSPHERE_TRANSPORT, HeaderConfig.LONG_POLLING_TRANSPORT);
        AtmosphereRequest request = new AtmosphereRequestImpl.Builder().headers(m).pathInfo("/a").method("GET").build();
        framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
        assertEquals(1, BAR.count.get());
        assertTrue(onMessage.get());
    }

    @Test
    public void testCachedOnBroadcast() throws IOException, ServletException {
        framework.setBroadcasterCacheClassName(UUIDBroadcasterCache.class.getName()).addAtmosphereHandler("/*", new CachedAR()).init();

        Map<String, String> m = new HashMap<String, String>();
        m.put(HeaderConfig.X_ATMOSPHERE_TRACKING_ID, UUID.randomUUID().toString());
        m.put(HeaderConfig.X_ATMOSPHERE_TRANSPORT, HeaderConfig.LONG_POLLING_TRANSPORT);
        AtmosphereRequest request = new AtmosphereRequestImpl.Builder().headers(m).pathInfo("/a").method("GET").build();
        framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
        assertEquals(3, CachedAR.count.get());
    }

    public final static class CachedAR implements AtmosphereHandler {

        static AtomicInteger count = new AtomicInteger();

        @Override
        public void onRequest(AtmosphereResource r) throws IOException {
            try {
                Broadcaster b = r.getBroadcaster();
                r.suspend();
                b.broadcast("test1").get();
                r.resume();

                ((AtmosphereResourceImpl) r).reset();

                b.broadcast("test2").get();
                b.broadcast("test3").get();
                b.broadcast("test4").get();

                r.setBroadcaster(b);
                r.addEventListener(new AtmosphereResourceEventListenerAdapter() {
                    @Override
                    public void onBroadcast(AtmosphereResourceEvent event) {
                        if (List.class.isAssignableFrom(event.getMessage().getClass())) {
                            count.set(List.class.cast(event.getMessage()).size());
                        }
                    }
                }).suspend();
                b.destroy();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                e1.printStackTrace();
            }
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent e) throws IOException {
        }

        @Override
        public void destroy() {
        }
    }

    public final static class BAR implements AtmosphereHandler {

        static AtomicInteger count = new AtomicInteger();

        @Override
        public void onRequest(AtmosphereResource e) throws IOException {
            try {
                e.addEventListener(new AtmosphereResourceEventListenerAdapter() {
                    @Override
                    public void onBroadcast(AtmosphereResourceEvent event) {
                        count.incrementAndGet();
                    }
                }).suspend();
                e.getBroadcaster().broadcast("test").get();
                e.getBroadcaster().destroy();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                e1.printStackTrace();
            }
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent e) throws IOException {
        }

        @Override
        public void destroy() {
        }
    }

    public final static class AR implements AtmosphereHandler {

        @Override
        public void onRequest(AtmosphereResource e) throws IOException {
            try {
                e.getBroadcaster().broadcast("test").get();
                e.getBroadcaster().destroy();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                e1.printStackTrace();
            }
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent e) throws IOException {
        }

        @Override
        public void destroy() {
        }
    }
}
