import locales
import handlers
import profiles.ProfileStep as ProfileStep
from profiles.ProfileManager import update
from bot import dir_links

def final(p):

    if p.link == "":

        links = load()

        link = links[0]
        p.link = link
        links.pop(0)


        save(links)

        update(p)


    text = (
        f"{locales.get(p.language, 'final_title')}\n\n"
        f"{locales.get(p.language, 'final_subtitle')}"
        f"{p.link}"
    )

    handlers.ui.render(
        p,
        p.tg_id,
        text,
        None,
        None
    )
    p.step = ProfileStep.FINAL

def save(links):
    with open(dir_links,'a+') as l:
        l.truncate(0)
        for link in links:
            l.write('\n'+link)
    print(f'Links: {len(links)}')


def load():
    with open(dir_links) as l:
        links=[str(x).replace('\n','') for x in l]
        if not(links==[]):
            links.pop(0)
    return links