# PingüiMaya. Open SourceCode
# https://pingui.tk

from cryptography.fernet import Fernet
import os
import shutil
from docx import Document
from fpdf import FPDF


key = "7nznUGP6Pbbgjrq6FrpP2hrb8B1xQz2f3O62YDhw-M8="

def __init__():
    #---
    try:
        os.mkdir("txtstrPREFERENCES")
    
    except:
        pass


    try:
        
        write(key,"DEFAULTKEY")
        os.rename("DEFAULTKEY.txt","DEFAULTKEY.key")
        shutil.move("DEFAULTKEY.key","txtstrPREFERENCES/DEFAULTKEY.key")
       

    except:
        pass
    

def newkey(name):
    if  ".key" in name:
        name = name

    if ".key" not in name:
        name = name + ".key"

    key = Fernet.generate_key()
    with open(name,"wb") as mk:
        mk.write(key)


def loadkey(name):
    if  ".key" in name:
        name = name

    if ".key" not in name:
        name = name + ".key"


    with open(name,"rb") as mk:
        key = mk.read()
        

    return key


def encrypt(file, CUSTOM):
    if  ".txt" in file:
        file = file

    if ".txt" not in file:
        file = file + ".txt"


    if CUSTOM == None:
        keyn = loadkey("txtstrPREFERENCES/DEFAULTKEY.key")

    if CUSTOM != None:
        if  ".key" in CUSTOM:
            CUSTOM = CUSTOM

        if ".key" not in CUSTOM:
            CUSTOM = CUSTOM + ".key"

        keyn = loadkey(CUSTOM)
        print(keyn)

    
    f = Fernet(keyn)
    with open(file,"rb") as org:
        original = org.read()

    encrypted = f.encrypt(original)

    with open(file, "wb") as encryp:
        encryp.write(encrypted)


def decrypt(file,CUSTOM):
    if  ".txt" in file:
        file = file

    if ".txt" not in file:
        file = file + ".txt"


    if CUSTOM == None:
        keyn = loadkey("txtstrPREFERENCES/DEFAULTKEY.key")

    if CUSTOM != None:
        if  ".key" in CUSTOM:
            CUSTOM = CUSTOM

        if ".key" not in CUSTOM:
            CUSTOM = CUSTOM + ".key"

        keyn = loadkey(CUSTOM)

    f = Fernet(keyn)
    
    with open(file, "rb") as encfile:
        encrypted = encfile.read()

    decrypted = f.decrypt(encrypted)

    with open(file,"wb") as decfile:
        decfile.write(decrypted)

    


def write(text,name):
    if  ".txt" in name:
        name = name

    if ".txt" not in name:
        name = name + ".txt"

    file = open(name, "w")
    file.write(text)
    file.close()


def read(rute):
    textc = ""
    if "txt" in rute:
        
        with open(rute, "r") as archive:
            for line in archive:
                textc = textc + "" + line
                
        return textc

    else:
        
        rute = rute + ".txt"
        with open(rute, "r") as archive:
            for line in archive:
                textc = textc + "" + line

        return textc

    

def rename(rute,name):
    if  "txt" in rute:
        rute = rute

    if "txt" not in rute:
        rute = rute + ".txt"


    if  ".txt" in name:
        name = name

    if ".txt" not in name:
        name = name + ".txt"

    
    os.rename(rute, name)


def toword(file):
    if  ".txt" in file:
        file = file

    if ".txt" not in file:
        file = file + ".txt"

    text = read(file)
    text = str(text)

    document = Document()
    document.add_paragraph(text)
    file = file.replace(".txt",".docx")
    document.save(file)


def topdf(file):
    if  ".txt" in file:
        file = file

    if ".txt" not in file:
        file = file + ".txt"

    pdf = FPDF()  
  

    # Add a page
    pdf.add_page()
 
    # set style and size of font
    # that you want in the pdf
    pdf.set_font("Arial", size = 8)

    # open the text file in read mode
    f = open(file, "r")
 
    # insert the texts in pdf
    for x in f:
        pdf.cell(1, 10, txt = x,ln = 1)
 


    file = file.replace(".txt",".pdf")
    pdf.output(file)  

    

    
    

    






