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

# FUNTION TO PROCESS THEATRE SCRIPT
import re
from instructions import characters
from instructions import directions


def read_script(filename):
    lines = []
    started = False

    with open(filename, 'r') as f:
        for line in f.readlines():
            #print(line) 
            parts = re.match(r'(?P<character>^.+?):\s?(\[(?P<stage_directions>[^]]+)\])?\s?(?P<text>.*)', line)
            parts_character = parts.group('character')
            parts_text = parts.group('text')
            parts_directions = str(parts.group('stage_directions')).split(".")

            lines.append((parts_character,parts_text,parts_directions))

        #print(lines)
    return lines;


# FUNCTION TO SYNTHESIZE TEXT
# based on https://github.com/marytts/marytts-txt2wav/tree/python

# To play wave files
from subprocess import call

# Mary server informations
mary_host = "localhost"
mary_port = "59125"

# HTTP + URL packages
import httplib2
from urllib.parse import urlencode, quote # For URL creation

def tts(voice, input_text, speaker):
    # Build the query
    query_hash = {"INPUT_TEXT": input_text,
                "INPUT_TYPE":"TEXT", # Input text
                "LOCALE":"en_GB",
                "VOICE": voice, # Voice informations  (need to be compatible)
                "OUTPUT_TYPE":"AUDIO",
                "AUDIO":"WAVE", # Audio informations (need both)
                }
    query = urlencode(query_hash)
    print("query = \"http://%s:%s/process?%s\"" % (mary_host, mary_port, query))

    # Run the query to mary http server
    h_mary = httplib2.Http()
    resp, content = h_mary.request("http://%s:%s/process?" % (mary_host, mary_port), "POST", query)

    #  Decode the wav file or raise an exception if no wav files
    if (resp["content-type"] == "audio/x-wav"):

        # Write the wav file
        f = open("/tmp/output_wav.wav", "wb")
        f.write(content)
        f.close()

        # Play the wav file
        # output = sc.get_speaker('Scarlett')
        # a = read("/tmp/output_wav.wav")
        # data = numpy.array(a[1],dtype=float)
        # print(data)
        # with sc.get_speaker('Scarlett').player(a[0], channels=[0]) as speaker:
        #     speaker.play(data)

        # aplay -D mono3 /tmp/output_wav.wav

        call(["aplay", "-D", speaker, "/tmp/output_wav.wav"])

        #pygame.mixer.init(frequency=16000) # Initialise the mixer
        #s = pygame.mixer.Sound("/tmp/output_wav.wav")
        #s.play()
        #pygame.time.wait(int(math.ceil(s.get_length() * 1000)))

    else:
        raise Exception(content)


# Fuction to RUN THE PLAY
import paho.mqtt.client as mqtt
import json
from time import sleep

HOST = 'localhost'
PORT = 1883


def on_connect(client, userdata, flags, rc):
    print("Connected to {0} with result code {1}".format(HOST, rc))
    # Subscribe to the text detected topic
    client.subscribe("hermes/asr/textCaptured")
    client.subscribe("hermes/nlu/intentNotRecognized")
    client.subscribe('hermes/intent/jocavdh:introduce')
    client.subscribe('hermes/intent/jocavdh:play')
    client.subprocess('hermes/dialogueManager/sessionEnded')
    client.subprocess('hermes/dialogueManager/sessionStarted')


def on_message(client, userdata, msg):
    print('Google Home is not speaking anymore')
    client.connected_flag=True


def on_cancelled(client, userdata, msg):
    print('ended')
    return


def on_play(client, userdata, msg):
    client.publish("hermes/dialogueManager/configure", json.dumps({
		'siteId': 'default',
		'intents': {
			'jocavdh:play': False
		}
	}))
    call(["python3", "play_script.py"])
    print('we are here')

sessions = {}
def onSessionStarted(client, data, msg):
    sessionId = parseSessionId(msg)
    sessions[sessionId] = msg
    print('started')

def on_introduce(client,data,msg):
    for character, line, direction in read_script('plays/introduction.txt'):
        input_text = line
        voice = characters.get(character)[0]
        speaker = characters.get(character)[1]
        #speaker = 'default'
        # Some way to do something with the stage directions will come here
        action = directions.get(direction[0])
        tts(voice, input_text, speaker)

        
        sleep(1)


    print('The play is over.')


client = mqtt.Client()
client.connect(HOST, PORT, 60)
client.on_connect = on_connect
client.connected_flag=False
client.message_callback_add('hermes/intent/jocavdh:introduce', on_introduce)
client.message_callback_add('hermes/intent/jocavdh:play', on_play)
client.message_callback_add("hermes/nlu/intentNotRecognized", on_cancelled)
client.message_callback_add('hermes/dialogueManager/sessionStarted', onSessionStarted)
print('main')
listening = False

client.loop_forever()