def sameValueCapacitors(capacitors):
    mxx = len(capacitors)
    last = 0
    flagLeave = False
    lstOfCapValue = [
        100, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000, 16000,
        17000, 18000, 20000, 30000
    ]
    for capV in lstOfCapValue:
        if not flagLeave:
            os.system(
                "cp -avr ./analytic_data/folder0/myTest.glm ./analytic_data/folder1/."
            )
            os.system(
                "cp -avr ./analytic_data/folder0/gridLAB_D_Model.glm ./analytic_data/folder1/."
            )

            for cp in list(capacitors.keys()):
                totalMap = data_analyzer.fromJsonToPython(
                    "./analytic_data/folder1/allAnalyticData.json")
                voltage = totalMap["NODES"][cp]["nominal_voltage"]
                addCapacitor("./analytic_data/folder1/gridLAB_D_Model.glm",
                             "cap" + str(cp), cp, "ABC", voltage, capV, capV,
                             capV)
                addCapacitorText("./analytic_data/folder1/myTest.glm",
                                 "cap" + str(cp), cp, "ABC", voltage, capV,
                                 capV, capV)
            if (run_gridlabd.runGridlabd("./analytic_data/folder1/",
                                         "myTest.glm")):
                data_analyzer.createTotalData("output_voltage_1.csv")
                if len(getProblemNodesFromTotalMapFile(
                        path).keys()) <= mxx and len(
                            getOverNodesFromTotalMapFile(path).keys()) == 0:
                    mxx = len(getProblemNodesFromTotalMapFile(path).keys())
                    last = capV
                if len(getOverNodesFromTotalMapFile(path).keys()) > 0:
                    flagLeave = True
                    continue
            if len(getProblemNodesFromTotalMapFile(path).keys()) == 0 and len(
                    getOverNodesFromTotalMapFile(path).keys()) == 0:
                print("Balanced")
                return -1
        else:
            continue
    os.system(
        "cp -avr ./analytic_data/folder0/myTest.glm ./analytic_data/folder1/.")
    os.system(
        "cp -avr ./analytic_data/folder0/gridLAB_D_Model.glm ./analytic_data/folder1/."
    )
    for cp in list(capacitors.keys()):
        totalMap = data_analyzer.fromJsonToPython(
            "./analytic_data/folder1/allAnalyticData.json")
        voltage = totalMap["NODES"][cp]["nominal_voltage"]
        addCapacitor("./analytic_data/folder1/gridLAB_D_Model.glm",
                     "cap" + str(cp), cp, "ABC", voltage, last, last, last)
        addCapacitorText("./analytic_data/folder1/myTest.glm", "cap" + str(cp),
                         cp, "ABC", voltage, last, last, last)
    return last
def writeCapacitors(capacitors):
    capV = 10000
    for cp in list(capacitors.keys()):
        totalMap = data_analyzer.fromJsonToPython(
            "./analytic_data/folder1/allAnalyticData.json")
        voltage = totalMap["NODES"][cp]["nominal_voltage"]
        addCapacitor("./analytic_data/folder1/gridLAB_D_Model.glm",
                     "cap" + str(cp), cp, "ABC", voltage, capV, capV, capV)
        addCapacitorText("./analytic_data/folder1/myTest.glm", "cap" + str(cp),
                         cp, "ABC", voltage, capV, capV, capV)
def getOverNodesFromTotalMapFile(pathToFile):
    return data_analyzer.fromJsonToPython(pathToFile)["OVERLOAD_NODES"]
def getProblemNodesFromTotalMapFile(pathToFile):
    return data_analyzer.fromJsonToPython(pathToFile)["UNDER_VOLTAGE_NODES"]
def cap_has_children(cap, current_capacitor_placement):
    return len(current_capacitor_placement.get(cap)) != 0


if __name__ == '__main__':
    capNum = input("Please entter num of capacitors: ")
    os.system(
        "cp -avr ./resources/Model_European_System/glm/* ./analytic_data/folder0/. "
    )
    if (run_gridlabd.runGridlabd("./analytic_data/folder0/", "myTest.glm")):
        data_analyzer.createTotalData("output_voltage_1.csv", 0)
        lstOfProblemNodes = getProblemNodesFromTotalMapFile(
            "./analytic_data/folder0/allAnalyticData.json")
        print("Num  of under voltage nodes : " + str(len(lstOfProblemNodes)))
        allDataFileBefore = data_analyzer.fromJsonToPython(
            "./analytic_data/folder0/allAnalyticData.json")
        print(allDataFileBefore["UNDER_VOLTAGE_NODES"])
        if (len(lstOfProblemNodes) == 0):
            print("Net Is Balanced")

        graph_handler.draw("europeanTreeGraph.dot", allDataFileBefore,
                           "netBefore.jpeg")

        G = graph_handler.load(
            "resources/Model_European_System/savedGraphEurope.adjlist")
        T = graph_handler.convertToDirectedGraph(G)
        # a = capacitorsPlacementAlgorithm(4, G, T,{'Bus102': 4, 'Bus106': 4, 'Bus109': 4, 'Bus88': 4, 'Bus290': 4, 'Bus327': 4})
        lstOfCapacitorsPlacement = capacitorsPlacementAlgorithm(
            int(capNum), G, T, lstOfProblemNodes)
        print("Caapacitor placement : " + str(lstOfCapacitorsPlacement.keys()))
        tryToSolve(lstOfCapacitorsPlacement, G)