# Script runs the client side of the application
# It is responsible for sending current layout to the server and receiving the contents to download

# 1 - Importing necessar/y libraries
import os
from glob import glob
from shutil import rmtree
import requests
import time
import logging
import zipfile

# 2 - Setting up logging with basic configuration
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def get_hardwarekey():
    # HTTP GET to player local web server (localhost:9696/info)
    try:
        response = requests.get("http://localhost:9696/info")
        # UTF-8 BOM (decode using utf-8-sig)
        response.encoding = "utf-8-sig"
        hardwareKey = response.json()["hardwareKey"]
        return hardwareKey
    except requests.exceptions.RequestException as e:
        # print(e)
        return None


def get_contents(hardwareKey):
    # HTTP POST to server (http://localhost/api/display)
    try:
        # Send hardwareKey to server as form data
        # DEBUG
        response = requests.post(
            "http://localhost/api/display", data={"hardwareKey": hardwareKey}
        )
        # PRODUCTION
        # response = requests.post('https://cms.combosmart.com/api/display', data={'hardwareKey': hardwareKey})
        return response.json()
    except requests.exceptions.RequestException as e:
        # print(e)
        return None


def download_content(link):
    # HTTP GET to server (http://localhost/cms/mensagens/owners/2/2220.zip)
    try:
        response = requests.get(link)
        # Check if .\downloads folder exists
        if not os.path.exists(".\\downloads"):
            os.makedirs(".\\downloads")
        # Write content to .\downloads folder
        path = ".\\downloads\\" + link.split("/")[-1]
        # Check if downloaded file already exists
        if os.path.exists(path):
            # print('File already exists:', path)
            return path
        with open(path, "wb") as f:
            f.write(response.content)
        return path
    except requests.exceptions.RequestException as e:
        # print(e)
        return None


def unzip_content(downloaded):
    try:
        # Unzip contents in ..\messages folder, split zipfile name and append to path
        # 2-24-0.zip -> ..\messages\2\24\
        # Check if folder exists, if not create it
        if not os.path.exists("..\\messages"):
            os.makedirs("..\\messages")
        # Split filename and create path
        filename = downloaded.split("\\")[-1]
        # print('Unzipping:', filename)
        first = filename.split("-")[0]
        # print(first)
        second = filename.split("-")[1]
        version = filename.split("-")[2]
        # print(second)
        path = "..\\messages\\" + first + "\\" + second + "\\"
        if not os.path.exists(path):
            os.makedirs(path)
        # Check version control file
        if os.path.exists(path + "version.txt"):
            with open(path + "version.txt", "r") as f:
                current = f.read()
            if current == version:
                # print('Version already exists:', version)
                return path
            else:
                with open(path + "version.txt", "w") as f:
                    f.write(version)
        else:
            # Create version control file
            with open(path + "version.txt", "w") as f:
                f.write(version)
        try:
            with zipfile.ZipFile(downloaded, "r") as zip_ref:
                zip_ref.extractall(path)
            return path
        except Exception as e:
            print(e)
            return False
    except Exception as e:
        print(e)
        return False


def main():
    hardwarekey = get_hardwarekey()
    if hardwarekey:
        # print('Hardwarekey fetched from player:', hardwarekey)
        contents = get_contents(hardwarekey)
        if contents:
            paths = []
            downloads = []
            # Run through JSON links and download contents into .\downloads folder
            # List example:
            # ["http://localhost/cms/mensagens/owners/2/2220.zip","http://localhost/cms/mensagens/owners/2/2240.zip"]
            for link in contents:
                if link:
                    # print('Downloading content:', link)
                    downloaded = download_content(link)
                    downloads.append(downloaded)
                    if downloaded:
                        unziped = unzip_content(downloaded)
                        paths.append(unziped)
                    else:
                        print("Failed to download content:", link)
                else:
                    print("Empty link in contents")
            # Clears downloads that is not coming from api
            downloadeds = glob(".\\downloads\\*")
            for zips in downloadeds:
                if zips not in downloads:
                    os.remove(zips)
            # Clears folders that are not in paths
            folders = glob("..\\messages\\*\\*\\")
            for folder in folders:
                if folder not in paths:
                    rmtree(folder)

        else:
            print("Failed to fetch contents from server")
    else:
        print("Failed to fetch hardwarekey from player")


if __name__ == "__main__":
    updtime = 30
    # print('Script de mensagens iniciado, atualizando a cada', updtime, 'segundos')
    while True:
        main()
        time.sleep(updtime)
