import pickle
import hashlib
import os
from helpers import BlockHelper as blockHelper
from helpers import TaskHelper as taskHelper

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    BLINK = '\033[5m'

def computeHash(data):
    hash = hashlib.sha256()
    data = str(data).encode()
    hash.update(data)
    return hash.hexdigest()

def clearScreen():
    os.system('cls' if os.name == 'nt' else 'clear')

def printLogo():
    print(f"""{bcolors.HEADER}
----------------------v1.0.0----------------------
  ____                 _      _           _       
 / ___| ___   ___   __| | ___| |__   __ _(_)_ __  
| |  _ / _ \ / _ \ / _` |/ __| '_ \ / _` | | '_ \ 
| |_| | (_) | (_) | (_| | (__| | | | (_| | | | | |
 \____|\___/ \___/ \__,_|\___|_| |_|\__,_|_|_| |_|

------------------Initializing--------------------
    {bcolors.ENDC}""")

def createFile(fileloc):
    try:
        savefile = open(fileloc, "xb")
        savefile.close()
        return True
    except:
        return False

def saveFile(fileloc, data):
    savefile = open(fileloc, "ab")
    pickle.dump(data, savefile)
    savefile.close()

def resetFile(fileloc):
    savefile = open(fileloc, "wb")
    savefile.close()

def loadFile(fileloc):
    loadfile = open(fileloc ,"rb")

    objs = []
    while True:
        try:
            objs.append(pickle.load(loadfile))
        except EOFError:
            break

    return objs

def loginStartup(self):
    # TODO - check if there are unvalidated blocks
    blockHelper.validateMinedBlock(self)
    # TODO - check if there are any notifcations
    taskHelper.displayUnreadLogs(self)

def errorMessage(text):
    return bcolors.FAIL + text + bcolors.ENDC

def successMessage(text):
    return bcolors.OKGREEN + text + bcolors.ENDC

def warningMessage(text):
    return bcolors.WARNING + text + bcolors.ENDC

def infoMessage(text):
    return bcolors.OKBLUE + text + bcolors.ENDC

def boldMessage(text):
    return bcolors.BOLD + text + bcolors.ENDC

def underlineMessage(text):
    return bcolors.UNDERLINE + text + bcolors.ENDC

def headerMessage(text):
    return bcolors.HEADER + text + bcolors.ENDC

def blinkMessage(text):
    return bcolors.BLINK + text + bcolors.ENDC


    