import random
random.seed("something")

class TestGen:
	def __init__(self):
		self.test_number = 0

	def generate_samples(self):
        #1 test
		self.print_sample(2, [(1, 4), (3, 1)], 2, [(1, 1, 3), (1, 2, 3)])
        
	def print_sample(self, n, hs, t, qs):
		self.test_number += 1
		test_name = "../preliminary/" + "{0:0=2d}".format(self.test_number)
		test_file = open(test_name, "w")
		print(n, file=test_file)
		print('\n'.join(' '.join(str(element) for element in segment) for segment in hs), file=test_file)
		print(t, file=test_file)
		print('\n'.join(' '.join(str(element) for element in query) for query in qs), file=test_file)
		test_file.close()

	def print_test(self, n, hs, t, qs):
		self.test_number += 1
		test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
		test_file = open(test_name, "w")
		print(n, file=test_file)
		print('\n'.join(' '.join(str(element) for element in segment) for segment in hs), file=test_file)
		print(t, file=test_file)
		print('\n'.join(' '.join(str(element) for element in query) for query in qs), file=test_file)
		test_file.close()

	def gen_hs(self, n, mx):
		A = []
		for i in range(n):
			A.append((random.randint(1, mx), random.randint(1, mx)))
		return A
	
	def gen_query(self, t, n, maxk):
		A = []
		for i in range(t):
			r = random.randint(1, n)
			l = random.randint(1, r)
			A.append((l, r, random.randint(1, maxk)))
		return A  		

	def make_test(self, maxn, maxt, maxk, cnt):
		for i in range(cnt//5):	
			n = random.randint(1, maxn)
			t = random.randint(1, maxt)
			self.print_test(n, self.gen_hs(n, 100), t, self.gen_query(t, n, maxk))
		                                                                                  
		for i in range(cnt//5):	
			n = random.randint(1, maxn)
			t = random.randint(1, maxt)
			self.print_test(n, self.gen_hs(n, 1000), t, self.gen_query(t, n, maxk))
                                                                                             
		for i in range(cnt//5):	
			n = random.randint(1, maxn)
			t = random.randint(1, maxt)
			self.print_test(n, self.gen_hs(n, 100000), t, self.gen_query(t, n, maxk))
		
		for i in range(cnt//5):	
			n = random.randint(1, maxn)
			t = random.randint(1, maxt)
			self.print_test(n, self.gen_hs(n, 1000000), t, self.gen_query(t, n, maxk))
		
		for i in range(cnt//5):	
			n = random.randint(1, maxn)
			t = random.randint(1, maxt)
			self.print_test(n, self.gen_hs(n, 1000000000), t, self.gen_query(t, n, maxk))
					    		
   
	def generate_all_tests(self):
		self.generate_samples()                       
		self.test_number = 0                 
		#First group of tests (01-)
		self.make_test(100, 10, 100, 10)	
		#Second group of tests (-)
		self.make_test(100000, 1, 1000000000, 10)        
		#Third group of tests (-)
		self.make_test(100000, 100000, 1000000000, 30)

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

import time
start = time.time()
writer = TestGen()
writer.generate_all_tests()
finish = time.time()
print('All tests were generated. Elapsed time: ', finish - start)
