import csv
import random


def fetchText(indText,config):
	"""This function fetch the text for the image with two characters
	rtext.csv definition is: 1st column the name of the file (i.e. B001.png), 2nd number of actors (at the moment
	they are limited to two; then a couple of columns or each actor with x and y coord of the strings; after the coords the outcomes, 
	one column for each actor
	Delimiter is ; and line feeds @, if there aren't any options, it returns 0 (no text)
	It returns two arrays, coords is a tuple (x,y) and result is the outcome"""
	with open(config["csvLocation"]+"/"+config["csvSpeech"]) as rtext:
		csvReader = csv.reader(rtext,delimiter=';')
		for row in csvReader:
			if row[0]==indText:
				noActors = int(row[1])
				if noActors == 0:
					return 0
				else:
					firstElement = 2+(noActors*2)
					lastElement = len(row)-(noActors-1)
					randQuote = random.randrange(firstElement,lastElement,noActors)
					coords = []
					result = []
					for x in range(0,noActors):
						coords.append((row[2+x*2],row[3+x*2]))
						result.append(row[randQuote+x])
					return coords,result