package io.wdd.func.xray.beans.node;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.PROXY_NODE_INDEX_MAP;


/**
 * https://blog.csdn.net/allway2/article/details/115034476
 */
//Graph class
class Graph {

    /**
     * 网络图的 邻接表
     */
    List<List<Integer>> adj_matrix = new ArrayList<>();


    public Graph(List<Edge> edgeList) {
        // 初始化 邻接表
        int totalNode = 0;
        for (Edge edge : edgeList) {
            totalNode = Math.max(
                    totalNode,
                    Math.max(
                            edge
                                    .getSource()
                                    .getNum(),
                            edge
                                    .getDestination()
                                    .getNum()
                    )

            );
        }
        ArrayList<Integer> tmp = new ArrayList<>();
        for (int i = 0; i <= totalNode; i++) {
           tmp.add(0);
        }
        for (int i = 0; i <= totalNode; i++) {
            adj_matrix.add(new ArrayList<>(tmp));
        }

        // 连接各个边界条件
        for (Edge edge : edgeList) {
            int source = edge
                    .getSource()
                    .getNum();

            int destination = edge
                    .getDestination()
                    .getNum();

            List<Integer> sourceList = adj_matrix.get(source);
            sourceList.add(
                    destination,
                    edge.getWeight()
            );
        }

    }

    public void printGraphMatrix() {

        System.out.println("---------  Graph Matrix ----------");
        System.out.println("----------------------------------");
        System.out.println();
        adj_matrix
                .stream()
                .forEach(
                        System.out::println
                );
        System.out.println("----------------------------------");

    }


    public void printGraph() {

        System.out.println("---------- Graph Adjust ----------");
        System.out.println("----------------------------------");

        int height = adj_matrix.size()-1 ;
        int weight = adj_matrix
                .get(0)
                .size()-1;

        for (int heigh = 0; heigh < height; heigh++) {
            List<Integer> list = adj_matrix.get(heigh);
            System.out.print("FROM [ " + PROXY_NODE_INDEX_MAP.get(heigh) + " ] ==> ");
            for (int width = 0; width < weight; width++) {
                if (list.get(width) != 0) {
                    System.out.print(PROXY_NODE_INDEX_MAP.get(width) + ": " + list.get(width) + "\t");
                }
            }
            System.out.print("\n");
        }
        System.out.println("----------------------------------");

    }


    public static void main(String[] args) {

        List<Edge> edgeList = Arrays.asList(

        );

        Graph graph = new Graph(edgeList);

        graph.printGraphMatrix();

        System.out.println("=========================");

        graph.printGraph();


    }
}
