# AGPL 3 or any later version
# (C) J.Y.Amihud ( Blender Dumbass )

import os
import json

from modules import Set
from modules.Common import *

categories = ["/articles", "/books", "/films", "/music", "/games", "/software", "/forum", "/reviews", "/malware", "/legals"]

def count(folder, text):
    walk = os.walk(folder)
    c = 0
    for i in walk:
        for b in i[2]:
            try:
                c = c + open(i[0]+"/"+b).read().count(text)
            except:
                pass
    return c

def Transfer(location):

    # This function is pretty pointless. But it is useful for me. Because I
    # Was using another, badly written server for years. And now to migrate
    # to this server, I need something that will transfer my data over. If
    # by any miracle you happended to use my code to build your own website,
    # well, using this you too could do the same. LOL.
    
    if not os.path.exists(location):
        print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Path does not exist!")
        return

    ##### ARTICLES #####

    for tab in categories:

        for article in os.listdir(location+tab):
            if article.endswith(".md"):

                title = article[article.replace("/", " ", 1).find("/")+1:].replace("-", " ").replace("_", " ").replace(".md", "").replace("%28", "(").replace("%29", ")")
                timestamp = os.path.getctime(location+tab+"/"+article.replace("%28", "(").replace("%29", ")"))
                
                text = open(location+tab+"/"+article)
                text = text.read()

                # Description is a bit more complicated
                art  = text
                while art.startswith("*"):
                    art = art[art.find("\n"):]
                    while art.startswith("\n") or art.startswith("_"):
                        art = art[1:]
                description = art[:art.find("\n")]

                # Legacy system only had non blenderdumbass authors
                # when it is a forum.
                
                author = "blenderdumbass"
                if tab == "/forum":
                    with open(location+"/accounts.json") as o:
                        accounts = json.load(o)["accounts"]

                    for person in accounts:
                        if tab+"/"+article in accounts[person].get("forums", []):
                            author = person

                with open(location+"/posters.json") as o:
                    posters = json.load(o)

                thumbnail = posters.get(tab+"/"+article, "")

                # This will only give us the general number of views.
                views = count(location+"/logs", "/graph"+tab+"/"+article)

                # But we also want to get number of views per day.
                dates = {}
                for date in os.listdir(location+"/logs"):
                    if date.endswith(".log"):
                        date = date[:-4]
                        try: datetext = open(location+"/logs/"+date+".log", "r")
                        except: continue

                        amount = datetext.read().count("/graph"+tab+"/"+article)
                        if amount > 0:
                            dates[date] = amount
                
                

                # Comments

                with open(location+"/comments.json") as o:
                    commentdata = json.load(o)

                comments = {
                    "comments":commentdata["comments"].get(tab+"/"+article, []),
                    "requests":commentdata["requests"].get(tab+"/"+article, [])
                }
                
                            
                # Making the directory for the article

                try: os.makedirs(Set.Folder()+"/tabs"+tab+"/"+article[:-3])
                except:pass

                # Transferring the texts

                textfile = Set.Folder()+"/tabs"+tab+"/"+article[:-3]+"/text.md"
                textfile = open(textfile, "w")
                textfile.write(text)
                textfile.close()

                # Metadata

                metadata = {
                    "title":title,
                    "timestamp":timestamp,
                    "description":description,
                    "author":author,
                    "thumbnail":thumbnail,
                    "views":{
                        "amount":views,
                        "viewers":[],
                        "dates":dates
                    },
                    "recording":"",
                    "comments":comments
                }

                with open(Set.Folder()+"/tabs"+tab+"/"+article[:-3]+"/metadata.json", "w") as save:
                    json.dump(metadata, save, indent=4)
                          
    #### ACCOUNTS ####

    with open(location+"/accounts.json") as o:
        accounts = json.load(o)["accounts"]
        
    for username in accounts:

        account = accounts[username]

        # We are updating the invites
        invite_codes = account.pop("invite_codes")
        account["invite_codes"] = {}
        
        for code in invite_codes:
            account["invite_codes"][code] = "Unknown"

        # We are adding some new stuff to accounts

        account["title"] = username # Visible title
        account["email"] = ""       # Contact info
        account["website"] = ""     # Website
        account["mastodon"] = ""    # Mastodon ( useful for fediverse tags )
        account["sessions"] = {}    # List of cookies accosiated with this account

        # We are saving the accounts in a different way too.
        
        with open(Set.Folder()+"/accounts/"+username+".json", "w") as save:
            json.dump(account, save, indent=4)