# Copyright (C) 2021 Neythen Treloar neythen@fintic.io
#
# This file is part of stockTicker project for justinodunn.
#
# stockTicker can not be copied and/or distributed without the express
# permission of Neythen Treloar

from flask import Flask, render_template, request
from stockTicker import StockTicker
from werkzeug.utils import secure_filename
import os
import pytz
import datetime
from datetime import timedelta
import threading
import csv
import pexpect
import time
import json
from multiprocessing import Process
import subprocess
from subprocess import Popen, PIPE
import numpy as np
import copy
import urllib.request
import sys
#stock_ticker = StockTicker()
import traceback

#open('log.txt', 'w').close() #wipe logs 


#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py")
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py")
#time.sleep(3)


#api_caller.sendline('A')


#pexpect.spawn("./check_update.sh")

displaying_screensaver = False
uploading = False
screensaver_p = None 
ticker_stopped = False

try:
    f = open('csv/display_settings.json', 'r')
    professional = json.load(f)[0] == "Professional"
    f.close()
except:
    professional = False

command = 300
tickerList = 0
DelayTime = 20
LastCommand = ''
speedTime = 25 

LOGO_FOLDER = 'logos/'
CSV_FOLDER = 'csv/new/'
ALLOWED_EXTENSIONS = {'csv', 'png'}

ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
time.sleep(2) # give the ticker time to initialise 


try:
    f = open('csv/system_info.json', 'r')
    system_info = json.load(f)
except Exception as e:
    system_info = {"update_available": False, "first_boot": False}
    f = open('csv/system_info.json', 'w')
    json.dump(system_info, f)
    system_info = json.load(f)
f.close()
             
ticker.sendline('*') # run startup gif by default 
time.sleep(8)
if system_info['first_boot']: # let startup message display 
    
    ticker.sendline('-')
    system_info['first_boot'] = False
    f =  open('csv/system_info.json', 'w')
    json.dump(system_info,f)
    f.close()
else:
    ticker.sendline('A') # run by default 

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def process_file(path, filename):
    default_csv = csv.writer(open('csv/tickers.csv', 'w+'))
    new_csv = csv.reader(open((path), 'r'))

    for row in new_csv:
        default_csv.writerow(row)

class DummyProcess():
    def close(self):
        return True

    
# try:
#     f = open('csv/last_updates.json', 'r')
#     last_updates = json.load(f)
#     f.close()
#     last_updates['stocks']['force'] = True
#     #last_updates['weather']['force'] = True
#     f = open('csv/last_updates.json', 'w')
#     json.dump(last_updates, f)
#     f.close()  
# except:
#     pass


app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def index():
    global command
    all_features = ['Current Weather','Daily Forecast','News', 'Sports (Upcoming Games)','Sports (Past Games)','Sports (Live Games)',
                'Sports (Team Stats)','Custom Images', 'Custom GIFs', 'Custom Messages', 'Stocks', 'Crypto', 'Forex', 'Commodities', 'Indices', 'Movies', 'IPO Calendar']
    
    global professional
    
    f = open('csv/display_settings.json', 'r')
    feature_settings = json.load(f)
    feature_settings2 = feature_settings[0]
    f.close()
    
   

    if not professional:
        f = open('csv/display_settings.json', 'r')
        currently_displaying = json.load(f)[1]
        f.close()
        not_displaying = [f for f in all_features if f not in currently_displaying[0]]
        not_displaying2 = [f for f in all_features if f not in currently_displaying[0]]

    elif professional:
        f = open('csv/display_settings.json', 'r')
        currently_displaying = json.load(f)[1]
        f.close()
        not_displaying = [f for f in all_features if f not in currently_displaying[0]]
        not_displaying2 = [f for f in all_features if f not in currently_displaying[1]]
    
    with open('api_keys.txt', 'r') as f:
        api_key2 = f.readlines()

    try:
        with open('movie_api_key.txt', 'r') as f:
            movie_api = f.readlines()
    except:
        movie_api = ''
    
    try:
        with open('ipo_api_key.txt', 'r') as f:
            ipo_api = f.readlines()
    except:
        ipo_api = ''    
    
    with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'r') as f:
        wifiline = f.readlines()

    now = datetime.datetime.now()
    timeString = now.strftime("%Y-%m-%d %H:%M")
    logos_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos')
    LogoList = os.listdir(logos_path)
	
    f = open('csv/stocks_settings.json', 'r')
    stocks_settings = json.load(f)
    f.close()
    
    f = open('csv/commodities_settings.json', 'r')
    commodities_settings = json.load(f)
    f.close()
    
    f = open('csv/indices_settings.json', 'r')
    indices_settings = json.load(f)
    f.close()    
    
    f= open('csv/crypto_settings.json', 'r')
    crypto_settings = json.load(f)
    f.close()
    
    f= open('csv/movie_settings.json', 'r')
    movie_settings = json.load(f)
    f.close()    

    f= open('csv/ipo_settings.json', 'r')
    ipo_settings = json.load(f)
    f.close()
    
    f = open('csv/forex_settings.json', 'r')
    forex_settings = json.load(f)
    f.close()
    
    f = open('csv/current_weather.json', 'r')
    current_weather= json.load(f)
    f.close()
    
    f = open('csv/daily_weather.json', 'r')
    daily_weather = json.load(f)
    f.close()
    
    f = open('csv/news_settings.json', 'r')
    news_settings = json.load(f)
    f.close()
    
    f = open('csv/upcoming_games.json', 'r')
    upcoming_games = json.load(f)
    f.close()
    
    f = open('csv/live_games.json', 'r')
    live_games = json.load(f)
    f.close()
    
    f = open('csv/past_games.json', 'r')
    past_games = json.load(f)
    f.close()
    
    f = open('csv/league_tables.json', 'r')
    team_stats = json.load(f)
    f.close()
    
    f = open('csv/image_settings.json', 'r')
    image_settings = json.load(f)
    f.close()
    
    f = open('csv/GIF_settings.json', 'r')
    GIF_settings = json.load(f)
    f.close()
    
    f = open('csv/message_settings.json', 'r')
    message_settings = json.load(f)
    f.close()
    
    f = open('csv/general_settings.json', 'r')
    general_settings = json.load(f)
    f.close()

    try:
        f = open('csv/scheduler.json','r')
        scheduler_settings = json.load(f)
        f.close()
    except:
        scheduler_settings = {"shutdown": {"hour": "00", "minute": "00", "enabled": False}, "reboot":{"hour": "00", "minute": "00", "enabled": False}, "timezone": "GMT", "brightness1":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness2":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness3":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness4":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}}
   
    try: # incase this doesnt exist
        api_keys = api_key2[1]
    except:
        api_keys = ''
    
    try:
        movie_api_key = movie_api[0]
    except:
        movie_api_key = ''

    try:
        ipo_api_key = ipo_api[0]
    except:
        ipo_api_key = ''
        
    try:
        wifi_SSID = wifiline[5][6:].replace('"','')
    except:
        wifi_SSID = ''
        
    try:
        wifi_PSK = wifiline[6][5:].replace('"','')
    except:
        wifi_PSK = ''


    templateData = {
        'system_info':system_info,
        'currently_displaying': currently_displaying,
        'feature_settings2': feature_settings2,
        'not_displaying': not_displaying, 
        'not_displaying2': not_displaying2, 
        'stocks_settings': stocks_settings, 
        'commodities_settings': commodities_settings,
        'indices_settings': indices_settings,
        'crypto_settings': crypto_settings,
        'forex_settings': forex_settings,
        'current_weather': current_weather,
        'daily_weather': daily_weather,
        'movie_settings': movie_settings,
        'ipo_settings': ipo_settings,
        'news_settings': news_settings,
        'upcoming_games': upcoming_games,
        'past_games': past_games,
        'live_games': live_games,
        'team_stats': team_stats,
        'image_settings':image_settings,
        'GIF_settings':GIF_settings, 
        'message_settings':message_settings,
        'professional':professional,
        'general_settings':general_settings,
        'api_keys':api_keys,
        'movie_api_key':movie_api_key,
        'ipo_api_key':ipo_api_key,
        'wifi_SSID':wifi_SSID,
        'wifi_PSK':wifi_PSK,
	'scheduler_settings':scheduler_settings
      }
      

    return render_template('index.html', **templateData)



def scheduled_brightness():
    try:
        while True:
            try:
                f = open('csv/scheduler.json','r')
                schedules = json.load(f)
                f.close()
            except:
                schedules = {"shutdown": {"hour": "00", "minute": "00", "enabled": False}, "reboot":{"hour": "00", "minute": "00", "enabled": False}, "timezone": "GMT", "brightness1":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness2":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness3":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness4":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}}

            timezone_brightness = schedules['timezone']

            brightness1_hour = schedules['brightness1']['hour']
            brightness1_minute = schedules['brightness1']['minute']
            brightness1_bright = schedules['brightness1']['bright']
            brightness1_enabled = schedules['brightness1']['enabled']

            brightness2_hour = schedules['brightness2']['hour']
            brightness2_minute = schedules['brightness2']['minute']
            brightness2_bright = schedules['brightness2']['bright']
            brightness2_enabled = schedules['brightness2']['enabled']

            brightness3_hour = schedules['brightness3']['hour']
            brightness3_minute = schedules['brightness3']['minute']
            brightness3_bright = schedules['brightness3']['bright']
            brightness3_enabled = schedules['brightness3']['enabled']

            brightness4_hour = schedules['brightness4']['hour']
            brightness4_minute = schedules['brightness4']['minute']
            brightness4_bright = schedules['brightness4']['bright']
            brightness4_enabled = schedules['brightness4']['enabled']

            try:
                if brightness1_enabled and datetime.datetime.now(pytz.timezone(timezone_brightness)).strftime("%H:%M") == brightness1_hour+':'+brightness1_minute:
                    brightness = max(min(int(brightness1_bright), 10), 1)
                    ticker.sendline(str(brightness-1))
                    f = open('csv/general_settings.json', 'r')
                    general_settings = json.load(f)
                    f.close()
                    general_settings['brightness'] = int(brightness)
                    f = open('csv/general_settings.json', 'w')
                    json.dump(general_settings, f)
                    f.close()
            except:
                pass
            try:
                if brightness2_enabled and datetime.datetime.now(pytz.timezone(timezone_brightness)).strftime("%H:%M") == brightness2_hour+':'+brightness2_minute:
                    brightness = max(min(int(brightness2_bright), 10), 1)
                    ticker.sendline(str(brightness-1))
                    f = open('csv/general_settings.json', 'r')
                    general_settings = json.load(f)
                    f.close()
                    general_settings['brightness'] = int(brightness)
                    f = open('csv/general_settings.json', 'w')
                    json.dump(general_settings, f)
                    f.close()
            except:
                pass
            try:
                if brightness3_enabled and datetime.datetime.now(pytz.timezone(timezone_brightness)).strftime("%H:%M") == brightness3_hour+':'+brightness3_minute:
                    brightness = max(min(int(brightness3_bright), 10), 1)
                    ticker.sendline(str(brightness-1))
                    f = open('csv/general_settings.json', 'r')
                    general_settings = json.load(f)
                    f.close()
                    general_settings['brightness'] = int(brightness)
                    f = open('csv/general_settings.json', 'w')
                    json.dump(general_settings, f)
                    f.close()
            except:
                pass
            try:
                if brightness4_enabled and datetime.datetime.now(pytz.timezone(timezone_brightness)).strftime("%H:%M") == brightness4_hour+':'+brightness4_minute:
                    brightness = max(min(int(brightness4_bright), 10), 1)
                    ticker.sendline(str(brightness-1))
                    f = open('csv/general_settings.json', 'r')
                    general_settings = json.load(f)
                    f.close()
                    general_settings['brightness'] = int(brightness)
                    f = open('csv/general_settings.json', 'w')
                    json.dump(general_settings, f)
                    f.close()
            except:
                pass
            time.sleep(20)
    except:
        pass

scheduled_b = Process(target=scheduled_brightness)
scheduled_b.start()



def save_displaying(input_settings):
    
    global professional
  
    all_settings = ['Stocks', 'Crypto', 'Forex', 'Commodities', 'Indices', 'Current Weather', 'Daily Forecast', 'News', 'Sports (Upcoming Games)', 'Sports (Past Games)',
                    'Sports (Live Games)', 'Sports (Team Stats)', 'Custom Images', 'Custom GIFs', 'Custom Messages', 'Movies', 'IPO Calendar']
    professional = len(input_settings) == 2
    if professional:
        all_settings = ['Stocks', 'Crypto', 'Forex', 'Commodities', 'Indices', 'Current Weather', 'News', 'Daily Forecast', 'Sports (Upcoming Games)',
                        'Sports (Past Games)', 'Sports (Team Stats)', 'Sports (Live Games)', 'Custom Messages', 'Custom Images', 'Movies', 'IPO Calendar']
        
   
    positions = []
    display_settings = []

    if professional:
        
        input_settings[0] = [i for i in input_settings[0] if i in all_settings]
        input_settings[1] = [i for i in input_settings[1] if i in all_settings]
   
        
    
    
    
    s = "Professional" if professional else "Standard"
    display_settings = [s] + [input_settings]
    with open('csv/display_settings.json', 'w') as f:
        json.dump(list(display_settings), f)
        
        
        
@app.route ("/start", methods = ['PUT', 'POST'])
def start():
     
    global displaying_screensaver
    global ticker
    #global api_caller
    global professional
    global ticker_stopped
    if displaying_screensaver:
        screensaver_p.close()
        ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
        #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py")
        #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py")
        displaying_screensaver = False
        
    if ticker_stopped:
        ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
        ticker_stopped = False
        
    #api_caller.sendline('A')

    global scheduled_b
    if scheduled_b.is_alive():
        scheduled_b.terminate()
    scheduled_b = Process(target=scheduled_brightness)
    scheduled_b.start()

    ticker.sendline('K')

    ticker.sendline('A')
    return index()
    
@app.route("/stop")
def stop():

    global displaying_screensaver
    global ticker
    #global api_caller
    global professional
    global ticker_stopped
    
    
    
    if not displaying_screensaver:
        ticker.sendline('K')
        time.sleep(0.1) # give time for leds to turn off
        ticker.close()
    else:
        screensaver_p.close()
        
    if not ticker_stopped:
        
        time.sleep(0.1) # give time for leds to turn off
        ticker.close()
        ticker_stopped = True
        
     
    if displaying_screensaver:
        screensaver_p.close()
        #ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
        #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py")
        #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py")
        displaying_screensaver = False


    scheduled_b.terminate()

    return index()

@app.route("/update", methods=['PUT','POST'])
def update():

    
    
    try:
        f = open('csv/system_info.json', 'r')
        system_info = json.load(f)
    except Exception as e:
        system_info = {"update_available": False, "first_boot": False}
      
    system_info['update_available'] = False
    f = open('csv/system_info.json', 'w')
    json.dump(system_info, f)
    f.close()
    

    os.system("./update.sh")
    os.system("sudo reboot now")
    return index()

@app.route("/restart")
def restart():
    os.system("sudo reboot now")
    return index()
    
@app.route("/reset")
def reset():
   
    os.system("sudo ./setup_config_files.sh")
    time.sleep(2)
    os.system("sudo reboot now")
    return index()
    
@app.route("/save", methods = ['PUT', 'POST', 'GET'])
def save():
   
    global uploading
    
    
    data = str(request.data.decode('utf-8'))
    
    
    input_settings = json.loads(data)
  

  
    save_displaying(input_settings['displaying'])
    
    input_settings= input_settings['feature_settings']
    
    feature = input_settings['feature'] 
    if feature in ['Stocks', 'Crypto', 'Forex', 'Commodities', 'Indices']:
        save_trade_settings(input_settings)
    elif feature in ['Current Weather', 'Daily Forecast']:
        save_weather_settings(input_settings)
    elif feature == 'News':
        save_news_settings(input_settings)
    elif feature == 'Movies':
        save_movie_settings(input_settings)
    elif feature == 'IPO Calendar':
        save_ipo_settings(input_settings)
    elif 'Sports' in feature:
        save_sports_settings(input_settings)
        
    elif feature in ['Custom GIFs', 'Custom Images']:
        
        
        images = request.files
        names = list(request.files.keys())

        for name in names:

            images[name].save('user_uploads/' +name)
     
        
        save_image_settings(input_settings)
        
    elif feature == 'Custom Messages':
        save_message_settings(input_settings)
        
    return index()

# saves files uploaded to the webpage for images and GIFs
@app.route("/upload", methods = ['PUT', 'POST', 'GET'])
def upload():
 
    global uploading
    uploading = True
    try:
        
        images = request.files
        names = list(request.files.keys())

        for name in names:
            images[name].save('user_uploads/' +name)
 
    except Exception as e:

         uploading = False
    return index()

def remove_old_uploads():
    #remove old files 
    
    
    
    f = open('csv/image_settings.json', 'r')
    image_settings = json.load(f)
    f.close()
    f = open('csv/GIF_settings.json', 'r')
    GIF_settings = json.load(f)
    f.close()
    for filename in os.listdir('user_uploads'):
        if filename not in image_settings['images'] and filename not in GIF_settings['images']:
            os.remove('user_uploads/'+filename)



@app.route("/brightness", methods=['PUT','POST'])
def brightness():
    global brightness
    data= request.data.decode('utf-8')
    settings = json.loads(data)

    brightness =settings['brightness']
    brightness = max(min(int(brightness), 10), 1)
    ticker.sendline(str(brightness-1))
    f = open('csv/general_settings.json', 'r')
    general_settings = json.load(f)
    f.close()
    general_settings['brightness'] = int(brightness)
    f = open('csv/general_settings.json', 'w')
    json.dump(general_settings, f)
    f.close()
    return index()


def edit_wpa_sup(country, ssid, pwd):
    current_wpa = open('/etc/wpa_supplicant/wpa_supplicant.conf')

    wpa_lines = current_wpa.readlines()

    wpa_lines[2] = 'country={}\n'.format(country)

    current_wpa.close()

    #remove this line to append to end instead of overwriting all networks 
    wpa_lines = wpa_lines[0:3]

    
    # create new file from scratch
    wpa_lines = []
    wpa_lines.append('ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n')
    wpa_lines.append('update_config=1\n')
    wpa_lines.append('country={}\n'.format(country))

    
    wpa_lines.append('\n')
    wpa_lines.append('network={\n')
    wpa_lines.append('\tssid="{}"\n'.format(ssid))
    wpa_lines.append('\tpsk="{}"\n'.format(pwd))
    wpa_lines.append('}\n')

    wpa_string = ''.join(wpa_lines)
  
    current_wpa = open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w')
    current_wpa.write(wpa_string)
    
    
@app.route("/wifi", methods = ['PUT', 'POST', 'GET'])
def set_wifi():

    data= request.data.decode('utf-8')
    
    settings = json.loads(data)
    
    country = settings['country'].upper()
    ssid = settings['ssid']
    pwd = settings['pwd']
    
    f = open('csv/general_settings.json', 'r')
    general_settings = json.load(f)
    f.close()
    general_settings['country_code'] = country
    f = open('csv/general_settings.json', 'w')
    json.dump(general_settings, f)
    f.close()
    
    edit_wpa_sup(country, ssid, pwd)
    # resstart netoworking
    os.system('wpa_cli -i wlan0 reconfigure')
    return index()

#def check_internet_connection(host='http://google.com'):
    
    #try:
        #urllib.request.urlopen(host) #Python 3.x
        #return True
    #except:
        #return False
        
#def check_network_connection():
    #ps = subprocess.Popen(['iwconfig'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    #try:
        #output = subprocess.check_output(('grep', 'ESSID'), stdin=ps.stdout)
        #return True
    #except subprocess.CalledProcessError:
        # grep did not match any lines
        
        #return False
    
def edit_hosts(hostname):
    current_hosts = open('/etc/hosts')

    hosts_lines = current_hosts.readlines()


    hosts_lines[5] = '127.0.1.1       {}'.format(hostname)

    current_hosts.close()
    
    hosts_string = ''.join(hosts_lines)
    
    current_hosts = open('/etc/hosts', 'w+')
    current_hosts.write(hosts_string)
    
@app.route("/hostname", methods = ['PUT', 'POST', 'GET'])
def hostname():
    data= request.data.decode('utf-8')
    settings = json.loads(data)
    hostname = settings['hostname']

    
    edit_hosts(hostname)
    os.system("sudo hostnamectl set-hostname {}".format(hostname))
    os.system("sudo systemctl restart avahi-daemon")
    
    f = open('csv/general_settings.json', 'r')
    general_settings = json.load(f)
    f.close()
    
    general_settings['hostname'] = hostname
    f = open('csv/general_settings.json', 'w')
    json.dump(general_settings, f)
    f.close()
    
    return index()


@app.route("/saveWeatherAPIKey", methods = ['PUT', 'POST'])
def saveWeatherAPIKey():
    
    data= request.data.decode('utf-8')
    settings = json.loads(data)
    
    key = settings['api_key']

    with open('./api_keys.txt') as f:
        lines = f.readlines()
        if len(lines) == 1:
            lines.append(str(key))
        elif len(lines) == 2:
            lines[1] = str(key)

    with open('./api_keys.txt', 'w') as f:
        for line in lines:
            f.write(line)
    return index()
    
 
    
@app.route("/saveMovieAPIKey", methods = ['PUT', 'POST'])
def saveMovieAPIKey():
    
    data= request.data.decode('utf-8')
    settings = json.loads(data)
    
    key = settings['api_key']

    with open('movie_api_key.txt', 'w') as f:
        f.write(str(key))

    return index()
    
@app.route("/saveIpoAPIKey", methods = ['PUT', 'POST'])
def saveIpoAPIKey():
    
    data= request.data.decode('utf-8')
    settings = json.loads(data)
    
    key = settings['api_key']

    with open('ipo_api_key.txt', 'w') as f:
        f.write(str(key))

    return index()

@app.route("/savePortfolioSettings", methods = ['PUT', 'POST'])
def savePortfolioSettings():
	
    data= request.data.decode('utf-8')
    settings = json.loads(data)
 
    #THIS IS TO CREATE PORTFOLIO JSON FILE IF IT DOESN'T EXIST
    initialize_json = '{"symbols":{}}'
    if 'portfolio_settings.json' not in os.listdir('csv/'):
        with open('csv/portfolio_settings.json', 'w') as f:
            f.write(initialize_json)
	
    try:
        f = open('csv/portfolio_settings.json')
        portfolio = json.load(f)
        f.close()

        shares1 = settings['shares']
        cost1 = settings['cost']
        symbol1 = settings['symbol']
	
        days1 = settings['days']
        day_start = datetime.datetime.strptime(str(days1), "%Y-%m-%d")
        day_today = datetime.datetime.strptime(datetime.datetime.now(pytz.utc).strftime("%Y-%m-%d"), "%Y-%m-%d")
        days1 = str((day_today - day_start).days)

        portfolio['symbols'][symbol1] = {'shares':shares1, 'day':days1, 'cost':cost1}

        f = open("csv/portfolio_settings.json", 'w+')
        json.dump(portfolio, f)
        f.close()
    except:
        pass

    return index()

@app.route("/savePortfolioCryptoSettings", methods = ['PUT', 'POST'])
def savePortfolioCryptoSettings():
	
    data= request.data.decode('utf-8')
    settings = json.loads(data)
 
    #THIS IS TO CREATE PORTFOLIO JSON FILE IF IT DOESN'T EXIST
    initialize_json = '{"symbols":{}}'
    if 'portfolio_crypto_settings.json' not in os.listdir('csv/'):
        with open('csv/portfolio_crypto_settings.json', 'w') as f:
            f.write(initialize_json)
	
    try:
        f = open('csv/portfolio_crypto_settings.json')
        portfolio = json.load(f)
        f.close()

        shares1 = settings['shares']
        cost1 = settings['cost']
        symbol1 = settings['symbol']
	
        days1 = settings['days']
        day_start = datetime.datetime.strptime(str(days1), "%Y-%m-%d")
        day_today = datetime.datetime.strptime(datetime.datetime.now(pytz.utc).strftime("%Y-%m-%d"), "%Y-%m-%d")
        days1 = str((day_today - day_start).days)

        portfolio['symbols'][symbol1] = {'shares':shares1, 'day':days1, 'cost':cost1}

        f = open("csv/portfolio_crypto_settings.json", 'w+')
        json.dump(portfolio, f)
        f.close()
    except:
        pass

    return index()


@app.route("/deletePortfolioSettings", methods = ['PUT', 'POST'])
def deletePortfolioSettings():

    data= request.data.decode('utf-8')
    settings = json.loads(data)

    try:
        g = open('csv/portfolio_settings.json')
        portfolio = json.load(g)
        g.close()
	
        symbol = settings
        # DELETE SYMBOLS FUNCTION
        try:
            portfolio['symbols'].pop(symbol)
        except:
            pass

        f = open("csv/portfolio_settings.json", 'w+')
        json.dump(portfolio, f)
        f.close()
    except:
        pass

    return index()

@app.route("/deletePortfolioCryptoSettings", methods = ['PUT', 'POST'])
def deletePortfolioCryptoSettings():

    data= request.data.decode('utf-8')
    settings = json.loads(data)

    try:
        g = open('csv/portfolio_crypto_settings.json')
        portfolio = json.load(g)
        g.close()
	
        symbol = settings
        # DELETE SYMBOLS FUNCTION
        try:
            portfolio['symbols'].pop(symbol)
        except:
            pass

        f = open("csv/portfolio_crypto_settings.json", 'w+')
        json.dump(portfolio, f)
        f.close()
    except:
        pass

    return index()


@app.route("/screensaver", methods = ['PUT', 'POST'])
def screensaver():
    global displaying_screensaver
    global ticker
    #global api_caller
    global screensaver_p
    
    data = str(request.data)
    
    if displaying_screensaver:
        screensaver_p.close()
    else:
        #api_caller.close()
        ticker.close()

    
    if "Pulsating Colors" in data:
        screensaver_p = pexpect.spawn("sudo -E python3 pulsing-colors.py --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=4 -r 32 --led-cols 64 -c 2 -P 1")
        
    elif "Rotating Square" in data:
        screensaver_p = pexpect.spawn("sudo ./demo --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=128 -D 0")
        
    elif "Pulsating brightness" in data:
        screensaver_p = pexpect.spawn("sudo -E python3 pulsing-brightness.py --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=4 -r 32 --led-cols 64 -c 2 -P 1")
        
    elif "Volume Bars" in data:
        screensaver_p = pexpect.spawn("sudo ./demo --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=128 -D 9")

    elif "Evolution of Color" in data:
        screensaver_p = pexpect.spawn("sudo ./demo --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=128 -D 10 -m 100")

    elif "Conway's Game of Life" in data:
        screensaver_p = pexpect.spawn("sudo ./demo --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=128 -D 7 -m 100")

    elif "Abelian Sandpile Model" in data:
        screensaver_p = pexpect.spawn("sudo ./demo --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=128 -D 6")

    elif "Grayscale Block" in data:
        screensaver_p = pexpect.spawn("sudo ./demo --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=128 -D 5")
        
    elif "Sleep" in data:
        screensaver_p = DummyProcess()
    else: #default in case user hasnt set one yet
        screensaver_p = DummyProcess()
        
    scheduled_b.terminate()        
    displaying_screensaver = True
    return index()
        
        
            

def combine_dict(current_settings, input_symbols, current_key):
    # removes keys not in input from current_settings[current_key] and adds keys not in current from input 
    new_settings = copy.deepcopy(current_settings)
    new_settings[current_key] = {}
    
    current_symbols = list(current_settings[current_key].keys())
    
    # add any stock that arent current in the settings
    for IS in input_symbols:
        if IS not in current_symbols:
            new_settings[current_key][IS] = []
        else:
            new_settings[current_key][IS] = current_settings[current_key][IS]
    
            
    return new_settings
    
def save_trade_settings(input_settings):

   
    filename = input_settings['feature'].lower() + '_settings.json'
    f = open('csv/' + filename, 'r')
    current_settings = json.load(f)

    f.close()
    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    current_settings['percent'] = input_settings['percent']
    current_settings['point'] = input_settings['point']
    current_settings['logos'] = input_settings['logos']
    current_settings['title'] = input_settings['title']
    if input_settings['feature'] == 'Stocks':
        current_settings['prepost'] = input_settings['prepost']
    if input_settings['feature'] == 'Stocks' or input_settings['feature'] == 'Crypto':
        current_settings['chart'] = input_settings['chart']    
    
    current_settings = combine_dict(current_settings, input_settings['symbols'], 'symbols')
    
    f = open('csv/' + filename, 'w')
    json.dump(current_settings, f)
    f.close()
    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()
    
    if any([current_settings['symbols'][k] == [] for k in input_settings['symbols']]):
        last_updates[input_settings['feature'].lower()]['force'] = True
        if input_settings['feature'] == 'Stocks':
            last_updates['prepost']['force'] = True
        
   
        
    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()
        
    
def save_weather_settings(input_settings):
    

   
   
    filename = 'current_weather.json' if input_settings['feature'] == 'Current Weather' else 'daily_weather.json'

    f = open('csv/' + filename, 'r')
    current_settings = json.load(f)
    f.close()
    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    current_settings['temp'] = input_settings['temp'].lower()
    current_settings['wind_speed'] = input_settings['wind_speed'].lower()
    
    current_settings['title'] = input_settings['title']
    
    if input_settings['feature'] == 'Daily Forecast':
        current_settings['current_weather'] = input_settings['current_weather']
    
    '''
    locations = {}
    for key in input_settings['locations']:
        locations[key] = []
    current_settings['locations'] = locations
    '''
    
    current_settings = combine_dict(current_settings, input_settings['locations'], 'locations')
    f = open('csv/' + filename, 'w+')
    json.dump(current_settings, f)
    f.close()
    
    #api_caller.sendline('w')
    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()
    if any([current_settings['locations'][k] == [] for k in input_settings['locations']]):
        
        last_updates['weather']['force'] = True
        
    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()
    
def save_news_settings(input_settings):
    filename = 'news_settings.json' 
    f = open('csv/' + filename, 'r')
    current_settings = json.load(f)
    f.close()
    

    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    current_settings['title'] = input_settings['title']
    current_settings['category'] = input_settings['category']
    current_settings['country'] = input_settings['country']
    current_settings['use_category'] = input_settings['use_category']
    current_settings['use_country'] = input_settings['use_country']
    current_settings['num_headlines'] = input_settings['num_headlines']
    
    
   
 
    f = open('csv/' + filename, 'w')
    json.dump(current_settings, f)
    f.close()
    #api_caller.sendline('n')
    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()
    
    last_updates['news']['force'] = True
        
    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()
    
    
def save_movie_settings(input_settings):
    filename = 'movie_settings.json' 
    f = open('csv/' + filename, 'r')
    current_settings = json.load(f)
    f.close()
    

    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    current_settings['title'] = input_settings['title']
    current_settings['category'] = input_settings['category']
        
 
    f = open('csv/' + filename, 'w')
    json.dump(current_settings, f)
    f.close()
    #api_caller.sendline('n')
    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()
    
    last_updates['movies']['force'] = True
        
    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()


def save_ipo_settings(input_settings):
    filename = 'ipo_settings.json' 
    f = open('csv/' + filename, 'r')
    current_settings = json.load(f)
    f.close()
    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    current_settings['title'] = input_settings['title']
 
    f = open('csv/' + filename, 'w')
    json.dump(current_settings, f)
    f.close()
    #api_caller.sendline('n')
    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()
    
    last_updates['ipo']['force'] = True
        
    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()


def save_sports_settings(input_settings):
    
    feature = input_settings['feature']
    
    if feature == 'Sports (Upcoming Games)':
        filename = 'upcoming_games.json'
        update_key = 'sports_u'
    elif feature == 'Sports (Past Games)':
        filename = 'past_games.json'
        update_key = 'sports_p'
    elif feature == 'Sports (Live Games)':
        filename = 'live_games.json'
        update_key = 'sports_l'
    elif feature == 'Sports (Team Stats)':
        filename = 'league_tables.json'
        update_key = 'sports_t'
    
    f = open('csv/' + filename, 'r')
    current_settings = json.load(f)
    f.close()
    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    current_settings['title'] = input_settings['title']
    current_settings['feature'] = input_settings['feature']
    
    current_settings = combine_dict(current_settings, input_settings['leagues'], 'leagues')
    
    f = open('csv/' + filename, 'w')
    json.dump(current_settings, f)
    f.close()
    
    #api_caller.sendline('S')
    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()
    
    if any([current_settings['leagues'][k] == [] for k in input_settings['leagues']]):
        
        last_updates[update_key]['force'] = True
        
    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()
    
# for images and GIFs
def save_image_settings(input_settings):
    filename = 'image_settings.json' if input_settings['feature'] == 'Custom Images' else 'GIF_settings.json' 
    
    current_settings = input_settings
    
    current_settings['speed'] = input_settings['speed'].lower()
    current_settings['speed2'] = input_settings['speed2'].lower()
    current_settings['animation'] = input_settings['animation'].lower()
    
    
    del current_settings['feature']
    f = open('csv/' + filename, 'w')
    json.dump(current_settings, f)
    f.close()
    remove_old_uploads()


def save_message_settings(input_settings):
    
    f = open('csv/message_settings.json', 'r')
    current_settings = json.load(f)
    f.close()
    new_settings = copy.deepcopy(input_settings)

    for i,IS in enumerate(input_settings['messages']):
        
        # check if this is in current_settings
        for CS in current_settings['messages']:
            if IS['name'] == CS['name']:
                new_settings['messages'][i] = CS
             
                break
                
    f = open('csv/message_settings.json', 'w')
    json.dump(new_settings, f)
    f.close()
    


@app.route("/shutdown")
def shutdown():
    os.system("sudo shutdown now")
    return index()
    
	
@app.route("/saveSchedulerSettings", methods = ['PUT', 'POST'])
def saveSchedulerSettings():
    
    data= request.data.decode('utf-8')
    input_settings = json.loads(data)
    
    initialize_json = '{"shutdown": {"hour": "00", "minute": "00", "enabled": false}, "reboot":{"hour": "00", "minute": "00", "enabled": false}, "timezone": "GMT", "brightness1":{"hour": "00", "minute": "00", "bright": "10", "enabled": false}, "brightness2":{"hour": "00", "minute": "00", "bright": "10", "enabled": false}, "brightness3":{"hour": "00", "minute": "00", "bright": "10", "enabled": false}, "brightness4":{"hour": "00", "minute": "00", "bright": "10", "enabled": false}}'

    if 'scheduler.json' not in os.listdir('csv/'):
        with open('csv/scheduler.json', 'w') as f:
            f.write(initialize_json)

    try:
        with open('csv/scheduler.json','r') as f:
            scheduler_settings = json.load(f)
    except:
        scheduler_settings = {"shutdown": {"hour": "00", "minute": "00", "enabled": False}, "reboot":{"hour": "00", "minute": "00", "enabled": False}, "timezone": "GMT", "brightness1":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness2":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness3":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}, "brightness4":{"hour": "00", "minute": "00", "bright": "10", "enabled": False}}

    scheduler_settings['shutdown']['hour'] = input_settings['shutdown_hour']
    scheduler_settings['shutdown']['minute'] = input_settings['shutdown_minute']
    scheduler_settings['shutdown']['enabled'] = input_settings['shutdown_enabled']

    scheduler_settings['reboot']['hour'] = input_settings['reboot_hour']
    scheduler_settings['reboot']['minute'] = input_settings['reboot_minute']
    scheduler_settings['reboot']['enabled'] = input_settings['reboot_enabled']    

    scheduler_settings['brightness1']['hour'] = input_settings['brightness1_hour']
    scheduler_settings['brightness1']['minute'] = input_settings['brightness1_minute']
    scheduler_settings['brightness1']['enabled'] = input_settings['brightness1_enabled']
    scheduler_settings['brightness1']['bright'] = input_settings['brightness1_bright']

    scheduler_settings['brightness2']['hour'] = input_settings['brightness2_hour']
    scheduler_settings['brightness2']['minute'] = input_settings['brightness2_minute']
    scheduler_settings['brightness2']['enabled'] = input_settings['brightness2_enabled']
    scheduler_settings['brightness2']['bright'] = input_settings['brightness2_bright']
	
    scheduler_settings['brightness3']['hour'] = input_settings['brightness3_hour']
    scheduler_settings['brightness3']['minute'] = input_settings['brightness3_minute']
    scheduler_settings['brightness3']['enabled'] = input_settings['brightness3_enabled']
    scheduler_settings['brightness3']['bright'] = input_settings['brightness3_bright']

    scheduler_settings['brightness4']['hour'] = input_settings['brightness4_hour']
    scheduler_settings['brightness4']['minute'] = input_settings['brightness4_minute']
    scheduler_settings['brightness4']['enabled'] = input_settings['brightness4_enabled']
    scheduler_settings['brightness4']['bright'] = input_settings['brightness4_bright']
	
    scheduler_settings['timezone'] = input_settings['timezone']

    with open('csv/scheduler.json', 'w') as f:
        json.dump(scheduler_settings,f)

    f = open('csv/last_updates.json', 'r')
    last_updates = json.load(f)
    f.close()

    last_updates['scheduler']['force'] = True

    f = open('csv/last_updates.json', 'w')
    json.dump(last_updates, f)
    f.close()

    return index()




if __name__ == "__main__":
    
    app.run(host='0.0.0.0', port=1024, debug=False) # the debuggger causes flickering 
        
   #sudo ./demo -D1 final.ppm -t 50 -m 25 --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=256 --led-slowdown-gpio=4 
