package com.tsi.comm.consumer;

import com.tsi.app.common.utils.ByteUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
//@SpringBootTest
public class TsiCommConsumerApplicationTests {

    @Test
    void tt() {
        int MAX_DATA_SIZE = 8155;
        int length = 8155 * 5 + 0;
        int ii;
        int current, total, dataSize;

        total = (length / MAX_DATA_SIZE) + 1;
        if (0 == (length % MAX_DATA_SIZE)) {
            total--;
        }
        current = 1;
        for (ii = 0; ii < total; ii++) {
            dataSize = MAX_DATA_SIZE;
            if ((ii+1) * MAX_DATA_SIZE > length) {
                dataSize = length - (MAX_DATA_SIZE * ii);
            }
            log.info("LENGTH: {}, CURRENT: {}, TOTAL: {}, INDEX: {}, DATA: {}", length, current, total, ii*MAX_DATA_SIZE, dataSize);
            current++;
        }
    }
    @Test
    public void nodeAdd() {
        int directionCode = 16;
        log.info("{}, {}, {}", directionCode, directionCode/10, directionCode%10);
        HashSet<String> keys = new HashSet<String>();
        keys.add("a");
        keys.add("b");
        keys.add("c");
        log.info("toString: {}", keys.toString());
        log.info(" toArray: {}", (Object) keys.toArray(new String[0]));
    }

    @Test
    public void test2() {
        String data = "02C518081A01061A11571DB1020C00020003010004006C00001900C800400200140050088040000802A0040021001001408100020C10608000010808A0000000000000000000000000000000000000000000000000000000000000000000000000000001FFC234B7EEAFFBEBB2EDEFFF0C123C030A1E020307140203020C1B320203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003010104006C00008128084441200A0451508882401408A2A0000001001000400102021010808000040820A0000000000000000000000000000000000000000000000000000000000000000000000000000001FF020303030B0C5CEDAFBBFB3BFF0C123C030A1E03071403020C1B32020300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E803";
        byte[] inBytes = ByteUtils.hexToByteArray(data);
        log.info("{}", Arrays.toString(inBytes));
    }
    @Test
    public void test1() {
        String hostName;
        String hostAddress;
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            hostName = localhost.getHostName();
            hostAddress = localhost.getHostAddress();
        } catch (UnknownHostException e) {
            hostName = "localhost";
            hostAddress = "127.0.0.1";
        }
        System.out.println("Your current IP address : " + hostAddress);
        System.out.println("Your current Hostname : " + hostName);

        InetAddress ip;
        String hostname;
        try {
            ip = InetAddress.getLocalHost();
            hostname = ip.getHostName();
            System.out.println("Your current IP address : " + ip);
            System.out.println("Your current Hostname : " + hostname);

        } catch (UnknownHostException e) {

            e.printStackTrace();
        }
    }
    class WorkerThreadTask implements Runnable {

        private ThreadPoolExecutor processExecutor;

        public WorkerThreadTask(ThreadPoolExecutor processExecutor, int idx) {
            Thread.currentThread().setName(String.format("worker-pool-%02d", idx));
            this.processExecutor = processExecutor;
        }

        @Override
        public void run() {
            while (true) {
                log.error("{}, corePoolSize: {}, activeCount: {}, maximumPoolSize: {}, poolSize: {}, taskCount: {}",
                        Thread.currentThread().getName(),
                        processExecutor.getCorePoolSize(),
                        processExecutor.getActiveCount(),
                        processExecutor.getMaximumPoolSize(),
                        processExecutor.getPoolSize(),
                        processExecutor.getTaskCount());
                try {
                    Thread.sleep(1000);
                    if (processExecutor.awaitTermination(1L, TimeUnit.MILLISECONDS))
                        log.info("Terminated");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class NamedThreadsFactory implements ThreadFactory {

        private int count = 0;
        private String Name = "MyThread-";

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, Name + ++count); // Mythread-x 형태로 쓰레드 이름 설정
        }
    }

    @Test
    public void testThread() throws InterruptedException {
        int threadPoolSize = Runtime.getRuntime().availableProcessors() * 10;
        ThreadPoolExecutor processExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
        log.info("corePoolSize: {}, activeCount: {}, maximumPoolSize: {}, poolSize: {}, taskCount: {}",
                processExecutor.getCorePoolSize(),
                processExecutor.getActiveCount(),
                processExecutor.getMaximumPoolSize(),
                processExecutor.getPoolSize(),
                processExecutor.getTaskCount());

        for (int ii = 0; ii < threadPoolSize; ii++) {
            log.info("Thread Start: {}", ii);
            processExecutor.execute(new WorkerThreadTask(processExecutor, ii));
            /*processExecutor.execute(() -> {
                while (true) {
                    try {
                        log.error("{}, corePoolSize: {}, activeCount: {}, maximumPoolSize: {}, poolSize: {}, taskCount: {}",
                                Thread.currentThread().getName(),
                                processExecutor.getCorePoolSize(),
                                processExecutor.getActiveCount(),
                                processExecutor.getMaximumPoolSize(),
                                processExecutor.getPoolSize(),
                                processExecutor.getTaskCount());
                        Thread.sleep(1000);
                    }
                    catch (Exception e) {
                        log.error("{}", e.getMessage(), e);
                    }
                }
            });*/
        }
        Thread.sleep(20000);
    }
}
