# -*- coding: utf-8 -*-
from ambrogio.plugin import Plugin
import re

def is_allowed_specific_char(string):
    charRe = re.compile(r'[^A-Z0-9.]')
    string = charRe.search(string)
    return not bool(string)


class Treasurer(Plugin):
	def receive_message(self, ambrogio, message):
		# ambrogio.store_value(message.sender,message.text)
		# print(ambrogio.retrieve_value(message.sender))
		command = message.text
		# print(command)
		# print(type(command))
		if command == 'BALANCE':
			print("krta")
		elif command == "HISTORY":
			record = ambrogio.retrieve_value(1)
			if message.sender in record:
				l = record[message.sender]
				for ele in l:
					print(ele)
		else:
			f = command.split()
			if len(f) > 2:
				exit()

			good = set('1234567890.+*|QWERTYUIOPLKJHGFDSAZXCVBNM,')

			if set(command) <= good and '|' in command:
			    x = command.split('|')
			    if len(x)== 2:
			    	good1 = set('1234567890.')
			    	if set(x[0]) <= good1:
			    		good2 = set('123456789QWERTYUIOPASDFGHJKLZXCVBNM,+*')
			    		if set(x[1]) <= good2:
			    			print('The string is fine')


			# comand = input()
			y= command.split("|")
			perman_part = y[1].split(",")
			perman_extra = y[1].split(",")
			i=0
			x = y[1]+","
			amount = int(y[0])

			extra = 0
			parts = 0
			gte = False
			gtp = False
			seen_star = False

			namelist = y[1].split(",")
			for c in namelist:
				c = c[:2]

			v=1#for checking no duplicate name are in the list
			# if len(namelist) is not 1:
			# 	for c in namelist:
			# 		if c in namelist[int(v):len(namelist)]:
			# 			print("wrong")
			# 			exit()
			# 			v +=1
			print(namelist)
			for c in x:
				if c is "+":
					gte = True
					continue
				if c is "*":
					gtp = True
					seen_star = True
					continue
				if gtp:
					parts += int(c)
					gtp = False
				if gte:
					extra += int(c)
					gte = False
				if c is ",":
					if seen_star:
						seen_star=False
					else:
						parts +=1
					# print(str(parts) + "fghj")
					perman_part[i] = parts
					parts =0
					perman_extra[i] = extra
					extra =0
					i += 1
			parts = sum(perman_part)
			extra = sum(perman_extra) 

			left = amount-extra
			final = y[1].split(",")
			for s in range(len(final)):
				final[s] = perman_part[s]*left/parts + perman_extra[s]

			payer = message.sender
			record = ambrogio.retrieve_value(1)
			for t in range(len(namelist)):
				if namelist[t] in record and namelist[t] != payer:
					record[namelist[t]].append(str(message.date) + " you pay back " + str(final[t]))
				elif namelist[t] in record and namelist[t] == payer:
					record[namelist[t]].append(str(message.date) + " you get back " + str(amount-final[t]))
				elif namelist[t] == payer:
					record[namelist[t]] = [(str(message.date) + " you get back " + str(amount-final[t]))]
				else:
					record[namelist[t]] = [str(message.date) + " you pay back " + str(final[t])]

			if payer not in namelist:
				if payer in record:
					record[payer].append(str(message.date) + " you get back " + str(amount))
				else:
					record[payer]= [(str(message.date) + " you get back " + str(amount))]


			ambrogio.store_value(1,record)

			print(perman_part)
			print(perman_extra)
			print(final)
			################### Above is the code to find how the bill is going to be split#######################
		

		# pass

	def init_plugin(self, ambrogio):
		peoples = dict()   #key- logon ka naam ; value- money(+-)
		record = dict()	   #key- logon ka naam ; value- list of strings jinme records likhe jaenge
		ambrogio.store_value(1,record)