package test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

import impl.Afficheur;
import impl.Canal;
import impl.CapteurImpl;
import impl.DiffusionEpoque;
import impl.Scheduler;

public class TestAlgoDiffusionEpoq {

    static int NB_CANAUX = 3;
    public static List<Integer>[] rEpoq;



    private static void display(List<Integer>[] res){
        for(int i = 0; i< NB_CANAUX; i++){
            System.out.println("canal " + i + ":");
            for(int j = 0; j< res[i].size(); j++){
                System.out.print(res[i].get(j));
                if(j<res[i].size()-1) System.out.print(", ");
            }
            System.out.println(" ");
        }
    }

    @BeforeAll
    public static void testAtom() throws InterruptedException{

        ArrayList<Integer>[] r = new ArrayList[NB_CANAUX];
        for(int i = 0; i<NB_CANAUX; i++){
            r[i] = new ArrayList<Integer>();
        }

        Afficheur[] a = new Afficheur[NB_CANAUX];

        Scheduler scheduler = new Scheduler(NB_CANAUX);
        CapteurImpl c = new CapteurImpl();

        for(int i = 0; i<NB_CANAUX; i++){
            Afficheur afficheur = new Afficheur();
            afficheur.setId(i);
            a[i] = afficheur;
            Canal canal = new Canal(c, scheduler, afficheur);
            c.attach(canal);
        }

        DiffusionEpoque algo = new DiffusionEpoque(c);
        c.setAlgoDiffusion(algo);
         
        ScheduledExecutorService clock =  scheduler.getScheduler();
        ScheduledFuture<?> future = clock.scheduleAtFixedRate(() -> c.tick(), 0, 500, TimeUnit.MILLISECONDS);

        Thread.sleep(10000);
        System.out.println("STOP");
        System.out.println("waitting for lasts display...");
        future.cancel(false);
        Thread.sleep(3100); //3000ms max + 100ms de marge
        
        clock.shutdown();
        
        for(int i = 0; i<NB_CANAUX; i++){
            r[i] = a[i].vals;
        }
        rEpoq = r;

        display(rEpoq);
    }


    @Test
    void testGoodOrder(){
        boolean r = true;

        for(int i = 0; i< NB_CANAUX && r; i++){
            int test = 0;
            for(int j = 0; j< rEpoq[i].size() && r; j++){
                r = r && rEpoq[i].get(j)>test;
                test=rEpoq[i].get(j);
            }
        }
        assertTrue(r);
    }

}
