from apidaze.script import Builder, Record, Answer, Echo, Speak, Wait
from apidaze.script import Bind, SpeakLanguages, Conference, Playback, Ringback
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
import re


def intro(localurl):
    builder = Builder()
    ringback = Ringback(file=f'{localurl}/apidazeintro.wav')
    wait8 = Wait(8)
    answer = Answer()
    record = Record(name='example_recording')
    wait = Wait(delay=2)
    playback = Playback(file=f'{localurl}/apidazeintro.wav')
    speak = Speak(text='This example script will show you some things you can do with our API')

    builder.add(
        ringback
        ).add(
        wait
        ).add(
        answer
        ).add(
        record
        ).add(
        wait
        ).add(
        playback
        ).add(
        speak
        ).add(
        wait)

    speak = Speak(text='Press 1 for an example of text to speech, press 2 to enter an echo line to check voice latency or press 3 to enter a conference.', input_timeout=10000)
    bind1 = Bind(action=f'{localurl}/step1.xml', text='1')
    bind2 = Bind(action=f'{localurl}/step2.xml', text='2')
    bind3 = Bind(action=f'{localurl}/step3.xml', text='3')
    speak.add(bind1).add(bind2).add(bind3)

    builder.add(speak)

    return str(builder)


def step1():
    builder = Builder()
    speak = Speak(text="Our text to speech leverages Google's cloud APIs to offer the best possible solution")
    wait1 = Wait(delay=1)

    builder.add(speak).add(wait1)

    speak = Speak(text='A wide variety of voices and languages are available.  Here are just a few', lang=SpeakLanguages.en_AU)
    builder.add(speak).add(wait1)

    speak = Speak(lang=SpeakLanguages.fr_FR, text='Je peux parler français')
    builder.add(speak).add(wait1)

    speak = Speak(lang=SpeakLanguages.de_DE, text='Auch deutsch')
    builder.add(speak).add(wait1)

    speak = Speak(lang=SpeakLanguages.ja_JP, text='そして日本人ですら')
    builder.add(speak).add(wait1)

    speak = Speak(text="You can see our documentation for a full list of supported languages and voices for them.  We'll take you back to the menu for now.")
    wait2 = Wait(2)

    builder.add(speak).add(wait2)
    return str(builder)


def step2():
    builder = Builder()
    speak = Speak(text='You will now be joined to an echo line.')
    echo = Echo(500)
    builder.add(speak).add(echo)
    return str(builder)


def step3():
    builder = Builder()
    speak = Speak('You will be entered into a conference call now.  You can initiate more calls to join participants or hangup to leave')
    conf = Conference(room='SDKTestConference')
    builder.add(speak).add(conf)
    return str(builder)


class Handler(BaseHTTPRequestHandler):
    def _set_headers(self, length: str, header: (str, str) = ("Content-type", "text/xml")):
        self.send_response(200)
        self.protocol_version = 'HTTP/1.1'
        self.send_header(header[0], header[1])
        self.send_header('Content-Length', length)
        self.end_headers()

    def _serve_wav(self):
        current_dir = os.getcwd()
        file = open(current_dir + '/resources/apidazeintro.wav', "rb")
        data = file.read()
        file.close()
        length = len(data)
        self._set_headers(header=("Content-type", "audio/wav"), length=str(length))
        self.wfile.write(data)

    def do_HEAD(self):
        if self.path == '/apidazeintro.wav':
            self._serve_wav()

    def do_GET(self):
        if re.search(r'/step1.xml', self.path):
            data = step1().encode('utf-8')
            length = len(data)
            self._set_headers(length=str(length))
            self.wfile.write(data)
        elif re.search(r'/step2.xml', self.path):
            data = step2().encode('utf-8')
            length = len(data)
            self._set_headers(length=str(length))
            self.wfile.write(data)
        elif re.search(r'/step3.xml', self.path):
            data = step3().encode('utf-8')
            length = len(data)
            self._set_headers(length=str(length))
            self.wfile.write(data)
        elif re.search(r'/apidazeintro.wav', self.path):
            self._serve_wav()
        else:
            data = intro('http://localhost:8080').encode('utf-8')
            length = len(data)
            self._set_headers(length=str(length))
            self.wfile.write(data)


port = 8080
handler = Handler

httpd = HTTPServer(("", port), handler)
httpd.serve_forever()