#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os

from xmleditor import newValues
from contentGenerator import base, longString, inHex
from attachmentGenerator import makeSTRINGattachment, makeBLOBattachment
from datetime import datetime
from resultReader import readResults
from configParser import getConfig


jmeterPath = getConfig("config.yml")[1]


def runTest(threads, duration, testPath, CSVname):
    funcCommand = "sh " + jmeterPath + "/jmeter -Jthreads="+str(threads)+" -Jduration="+str(duration)+" -n -t "+testPath+" -l TestCSV/" + CSVname + "_" + datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + ".csv"
    results = os.popen(funcCommand).read()
    return results


def longstringOUT(threads, duration, size, graphBoolean):
    testname = "longstringOUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("longstringOUT", "v1", "size", str(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def longstringIN(threads, duration, size, graphBoolean):
    testname = "longstringIN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("longstringIN", "v1", "content", longString(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def flatOUT(threads, duration, elements, graphBoolean):
    testname = "flatOUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_elements-" + str(elements)
    newValues("flatOUT", "v1", "limit", str(elements), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def flatIN(threads, duration, elements, graphBoolean):
    testname = "flatIN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_elements-" + str(elements)
    newValues("flatIN", "v1", "nr", "ABCDEF", elements, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def nestedOUT(threads, duration, elements, graphBoolean):
    testname = "nestedOUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_elements-" + str(elements)
    newValues("nestedOUT", "v1", "limit", str(elements), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def nestedIN(threads, duration, elements, graphBoolean):
    testname = "nestedIN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_elements-" + str(elements)
    newValues("nestedIN", "v1", "jupp", "ABCDEF", 0, elements)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def BLOBbase64OUT(threads, duration, size, graphBoolean):
    testname = "BLOB_base64_OUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("BLOBbase64OUT", "v1", "size", str(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def BLOBbase64IN(threads, duration, size, graphBoolean):
    testname = "BLOB_base64_IN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("BLOBbase64IN", "v1", "img", base(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def BLOBhexOUT(threads, duration, size, graphBoolean):
    testname = "BLOB_hex_OUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("BLOBhexOUT", "v1", "size", str(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def BLOBhexIN(threads, duration, size, graphBoolean):
    testname = "BLOB_hex_IN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("BLOBhexIN", "v1", "img", inHex(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def BLOBattachmentOUT(threads, duration, size, graphBoolean):
    testname = "BLOB_attachment_OUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("BLOBattachmentOUT", "v1", "size", str(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def BLOBattachmentIN(threads, duration, size, graphBoolean):
    testname = "BLOB_attachment_IN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("BLOBattachmentIN", "v1", "img", "cid:1339838373853", 0, 0)
    makeBLOBattachment(size)
    results = runTest(threads, duration, "TestJMX/loadXMLattachment.jmx", testname)
    readResults(results, testname, graphBoolean)


def longstringattachmentOUT(threads, duration, size, graphBoolean):
    testname = "longstring_attachment_OUT_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("longstringattachmentOUT", "v1", "size", str(size), 0, 0)
    results = runTest(threads, duration, "TestJMX/loadXML.jmx", testname)
    readResults(results, testname, graphBoolean)


def longstringattachmentIN(threads, duration, size, graphBoolean):
    testname = "longstring_attachment_IN_" + "Threads-" + str(threads) + "_Duration-" + str(duration) + "_size-" + str(size)
    newValues("longstringattachmentIN", "v1", "content", "cid:1339838373853", 0, 0)
    makeSTRINGattachment(size)
    results = runTest(threads, duration, "TestJMX/loadXMLattachment.jmx", testname)
    readResults(results, testname, graphBoolean)
