"""
Demo Flask application to test the operation of Flask with socket.io

Aim is to create a webpage that is constantly updated with random numbers from a background python process.

30th May 2014

===================

Updated 13th April 2018

+ Upgraded code to Python 3
+ Used Python3 SocketIO implementation
+ Updated CDN Javascript and CSS sources

"""




# Start with a basic flask app webpage.
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from random import random
from time import sleep
from threading import Thread, Event
from jinja2 import Environment, FileSystemLoader
from contacts import change_contact
from photos import change_photo
from homepage import homepage
from call import call
import os


__author__ = ''

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

#turn the flask app into a socketio app
socketio = SocketIO(app)

#random number Generator Thread
thread = Thread()
thread_stop_event = Event()

class RandomThread(Thread):
    def __init__(self):
        self.delay = 3
        super(RandomThread, self).__init__()

    def randomNumberGenerator(self):
        #infinite loop of magical random numbers
        contact_selected = [True, False, False]
        paysages = os.listdir("static/photos")
        photo_selected = [False] * len(paysages)
        photo_selected[0]=True

        comments = {"paysage":{"contact":"Morgan", "comment":"La Bretagne ça vous gagne!", "date":"12/04/2019"},
                    "paysage_2":{"contact":"Yves", "comment":"Les alpes en automne!", "date":"16/05/2019"},
                    "paysage_3":{"contact":"Marc", "comment":"La bretagne en été c'est très beau", "date":"19/06/2019"},
                    "paysage_4":{"contact":"Morgan", "comment":"Posé en Y dans mon char à voile", "date":"21/06/2019"},
                    "paysage_5":{"contact":"Marc", "comment":"Quel soleil Mamie!", "date":"04/07/2019"},
                    "paysage_6":{"contact":"Morgan", "comment":"Le printemps est la!", "date":"07/08/2019"},
                    "paysage_7":{"contact":"Morgan", "comment":"Un peu froid ce matin!", "date":"12/06/2019"},
                    "paysage_8":{"contact":"Yves", "comment":"Vivement l'été!", "date":"10/04/2019"},
                    "paysage_9":{"contact":"Yves", "comment":"Hiboomer? ca claque", "date":"22/07/2019"}}
        contact_menu = False
        galerie_menu = False
        homepage_menu = True
        code = "Nothing"
        first = True
        while not thread_stop_event.isSet():
            if contact_menu:
                print("contact")
                if first:
                    contact_selected = change_contact(True, contact_selected, socketio, False, "None")
                    first = False
                code = ["blue"]
                sleep(5)
                print(code)
                if len(code)>0 and code[0] == "right":
                    contact_selected=change_contact(True, contact_selected, socketio, False, "rightArrow")
                    sleep(1)
                    contact_selected=change_contact(True, contact_selected, socketio, True, "None")
                elif len(code)>0 and code[0] == "left":
                    contact_selected=change_contact(True, contact_selected, socketio, False, "leftArrow")
                    sleep(1)
                    contact_selected=change_contact(False, contact_selected, socketio, True, "None")
                elif len(code)>0 and code[0] == "orange":
                    contact_selected=change_contact(True, contact_selected, socketio, False, "orangeButton")
                    sleep(1)
                    contact_menu = False
                    homepage_menu = True
                elif len(code)>0 and code[0] == "blue":
                    contact_selected=change_contact(True, contact_selected, socketio, False, "blueButton")
                    sleep(1)
                    os.system("cvlc --no-video-title-show --play-and-exit --fullscreen ./static/call_doran.mp4")

            elif galerie_menu:
                print("galerie")
                photo_selected = change_photo(photo_selected, comments, True, socketio, "None")
                code = ["right"]
                print(code)
                if len(code)>0 and code[0] == "right":
                    print("change right")
                    photo_selected = change_photo(photo_selected, comments, False, socketio, "change")
                elif len(code)>0 and code[0] == "left":
                    print("change left")
                    photo_selected = change_photo(photo_selected, comments, True, socketio, "change")
                elif len(code)>0 and code[0] == "orange":
                    photo_selected = change_photo(photo_selected, comments, True, socketio, "home")
                    sleep(0.5)
                    galerie_menu = False
                    homepage_menu = True
                    homepage(socketio)
                elif len(code)>0 and code[0] == "blue":
                    galerie_menu = False
                    contact_menu = True
            elif homepage_menu:
                print("homepage")
                homepage(socketio)
                sleep(0.5)
                code = ["orange"]
                print(code)
                if len(code)>0 and code[0] == "orange":
                    homepage_menu = False
                    galerie_menu = True
                if len(code)> 0 and code[0] == "blue":
                    homepage_menu = False
                    contact_menu = True
                    first = True

                

    def run(self):
        self.randomNumberGenerator()


@app.route('/')
def index():
    #only by sending this page first will the client be connected to the socketio instance
    return render_template('index.html')

@socketio.on('connect', namespace='/test')
def test_connect():
    # need visibility of the global thread object
    global thread
    print('Client connected')

    #Start the random number generator thread only if the thread has not been started before.
    if not thread.isAlive():
        print("Starting Thread")
        thread = RandomThread()
        thread.start()

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')


if __name__ == '__main__':
    socketio.run(app)
