import os, shutil
from random import *
import random
seed("omg")

class WarTestGen:
	def __init__(self):
		self.test_number = 0
	
	def print_test(self, N, M, T, VMPS, EDGES, ATTACKED, WLVS):
		self.test_number += 1
		test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
		test_file = open(test_name, "w")
		print(N, M, T, file=test_file)
		print(' '.join(str(element) for element in VMPS), file=test_file)		
		print('\n'.join(' '.join([str(element[0]), str(element[1])]) for element in EDGES), file=test_file)
		print(ATTACKED, WLVS, file=test_file)
		test_file.close()
	

	def gen_graph(self, N, M):
		A = []
		was = {}
		for i in range(0, M):
			a = random.randint(1, N) 
			b = random.randint(1, N)
			while (((a, b) in was) or a == b):
				a = random.randint(1, N)
				b = random.randint(1, N)
			was[(a, b)] = True
			was[(b, a)] = True				
			A.append((a, b))
		
		return A
    	
	def gen_VMP(self, N, MINK, MAXK):
		A = []
		for i in range(0, N):
			A.append(random.randint(MINK, MAXK))
		return A
	def spec_test(self):
		N = 1000
		M = 999
		ED = []
		for i in range(1, N):
			ED.append((i, i + 1))
		VMP = []
		VMP.append(2)
		for i in range(2, N + 1):
			VMP.append(1)
		WLVS = N + 1
		T = 1
		AT = 1
		self.print_test(N, M, T, VMP, ED, AT, WLVS)
		

		N = 1000
		M = 999
		ED = []
		for i in range(1, N):
			ED.append((i, i + 1))
		VMP = []
		VMP.append(2)
		for i in range(2, N + 1):
			VMP.append(1)
		WLVS = N + 2
		T = 1
		AT = 1
		self.print_test(N, M, T, VMP, ED, AT, WLVS)
		
		N = 1000
		ED = []
		M = 1000
		for i in range(1, N):
			ED.append((i, i + 1))
		ED.append((N, 1))
		VMP = []
		VMP.append(2)
		for i in range(2, N + 1):
			VMP.append(1)
		WLVS = N + 1
		T = 2
		AT = 1
		self.print_test(N, M, T, VMP, ED, AT, WLVS)
		

		N = 1000
		ED = []
		M = 1000
		for i in range(1, N):
			ED.append((i, i + 1))
		ED.append((N, 1))
		VMP = []
		VMP.append(2)
		for i in range(2, N + 1):
			VMP.append(1)
		WLVS = N + 2
		T = 2
		AT = 1
		self.print_test(N, M, T, VMP, ED, AT, WLVS)
		
		N = 1000
		M = 999
		ED = []
		for i in range(1, N):
			ED.append((1, i))
		VMP = []
		VMP.append(2)
		for i in range(2, N + 1):
			VMP.append(1)
		WLVS = N + 2
		T = 1
		AT = 1
		self.print_test(N, M, T, VMP, ED, AT, WLVS)		


		N = 500
		M = 0
		ED = []
		for i in range(1, N + 1):
			for j in range(i + 1, N + 1):
				ED.append((i, j))
				M = M + 1
		VMP = []
		VMP.append(100)
		for i in range(2, N + 1):
			VMP.append(1)
		WLVS = N + 2
		T = 99
		AT = 1
		self.print_test(N, M, T, VMP, ED, AT, WLVS)		



	def gen(self):
		T = 5
		N = 5
		M = 6
		VMPS = [4, 5, 4, 4, 1]
		ED = [(1,5), (1,3), (2,1), (3,5), (2,5), (4,3)]
		AT = 1
		WLVS = 8
		self.print_test(N, M, T, VMPS, ED, AT, WLVS)

		T = 1
		N = 5
		M = 5
		VMPS = [3, 1, 5, 1, 1]
		ED = [(1,2), (1,5), (1,4), (4,5), (2,3)]
		AT = 1
		WLVS = 11
		self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		
		
		for i in range(0, 5):
			N = random.randint(5, 8)
			M = random.randint(N, N + 3)
			VMPS = self.gen_VMP(N, 1, 5)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(3, 10)
			T = random.randint(1, 3)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 5):
			N = random.randint(10, 20)
			M = random.randint(10, 15)
			VMPS = self.gen_VMP(N, 1, 5)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(20, 30)
			T = random.randint(1, 3)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		self.spec_test()

		for i in range(0, 5):
			N = random.randint(20, 30)
			M = random.randint(N, (int)(N * (N - 1) / 3))
			VMPS = self.gen_VMP(N, 50, 100)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(50, 100)
			T = random.randint(10, 20)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 5):
			N = random.randint(500, 1000)
			M = random.randint(N, (int)(N * (N - 1) / 3))
			VMPS = self.gen_VMP(N, 20, 100)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(500, 10000)
			T = random.randint(20, 30)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 2):
			N = random.randint(500, 1000)
			M = random.randint(N, (int)(N * (N - 1) / 3))
			VMPS = self.gen_VMP(N, 100, 1000)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(500, 10000)
			T = random.randint(1, 1)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 1):
			N = random.randint(500, 1000)
			M = 0
			VMPS = self.gen_VMP(N, 4, 9)
			ED = self.gen_graph(N, M)
			AT = 1
			WLVS = 10
			T = random.randint(1, 1)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 1):
			N = random.randint(500, 1000)
			M = 0
			VMPS = self.gen_VMP(N, 10, 15)
			ED = self.gen_graph(N, M)
			AT = 1
			WLVS = 10
			T = random.randint(1, 1)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 1):
			N = random.randint(500, 1000)
			M = 0
			VMPS = self.gen_VMP(N, 10, 15)
			ED = self.gen_graph(N - 1, M)
			AT = N
			WLVS = 10
			T = random.randint(1, 1)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		

		for i in range(0, 1):
			N = random.randint(500, 1000)
			M = 0
			VMPS = self.gen_VMP(N, 10, 15)
			ED = self.gen_graph(N - 1, M)
			AT = N
			WLVS = 20
			T = random.randint(1, 1)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		for i in range(0, 5):
			N = 100000
			M = random.randint(10000, 50000)
			VMPS = self.gen_VMP(N, 100, 1000)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(500, 10000)
			T = random.randint(100, 1000)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 5):
			N = 1000
			M = random.randint((int)(N * (N - 1) / 6), (int)(N * (N - 1) / 3))
			VMPS = self.gen_VMP(N, 100, 1000)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(500, 10000)
			T = random.randint(100, 1000)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 2):
			N = 100000
			M = random.randint(100000, 100000)
			VMPS = self.gen_VMP(N, 1000, 5000)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(500, 10000)
			T = random.randint(500, 1000)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
		
		for i in range(0, 2):
			N = 100000
			M = random.randint(100000, 100000)
			VMPS = self.gen_VMP(N, 1000, 5000)
			ED = self.gen_graph(N, M)
			AT = random.randint(1, N)
			WLVS = random.randint(10000, 10000)
			T = random.randint(1, 1)
			self.print_test(N, M, T, VMPS, ED, AT, WLVS)
			


if os.path.exists('../tests'):
	shutil.rmtree('../tests', ignore_errors=False, onerror=None)
try:
	os.makedirs('../tests')
except OSError:
	pass

writer = WarTestGen()
writer.gen()
