#####################################################################
#                                                                   #
#  THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE   #
# LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet )  #
# FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk )      #
# WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
# IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
# OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE.        #
#                                                                   #
#      ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS       #
# (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK.  #
# YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
# THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER    #
# VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG   #
# WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ).         #
#                                                                   #
# THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE   #
# NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
# THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT    #
# SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
# THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT.       #
#                                                                   #
# THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL  #
# FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE   #
# THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD   #
# TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT.  #
#                                                                   #
#####################################################################

# This file will perform a simple search on the LBRY network.

from subprocess import *
import json

from flbry import url
from flbry.variables import *
from flbry import markdown
from flbry import channel
from flbry import settings

def list(claim_id, url, comment_id=""):


    # This function will list a list of comments of a certain claim_id
    # It will preview a very basic form of comment. You will have
    # to select a comment to interact with it further. Like read the
    # whole text, if it not short ( or in a markdown format ). Or
    # do other things like replies.

    w, h = tsize()
    
    page_size = h - 5
    page = 1
    
    while True:

        # Printing the search query and page number
        center("COMMENTS OF: "+url+" PAGE : "+str(page))

        if not comment_id:
            out = check_output(["flbry/lbrynet",
                         "comment", "list", claim_id,
                         '--page='+str(page),
                            '--page_size='+str(page_size)])
        else:
            out = check_output(["flbry/lbrynet",
                                "comment", "list", '--claim_id='+claim_id,
                            '--parent_id='+comment_id,
                         '--page='+str(page),
                            '--page_size='+str(page_size),
                            '--include_replies'])
            
        # Now we want to parse the json
        try:
            out = json.loads(out)
        except:
            print("   Connect to LBRY first.")
            return

        d = {"categories":["Tip LBC", "Comments", "Channel",  "Preview"],
                          "size":[1,1,2,5],
                          "data":[]}
        
        try:
            

            # List what we found
            for n, i in enumerate(out["items"]):
                
                
                preview = "---!Failed Loading comment---"
                support = 0
                replies = 0
                bywho = "[anonymous]"
                
                try:
                    comment = i["comment"]
                    preview = comment.replace("\n", " ")
                    support = i["support_amount"]
                    bywho = i["channel_name"]
                    replies = i["replies"]
                except:
                    pass
                
                d["data"].append([support, replies, bywho, preview])

            table(d)
                
            # Tell the user that he might want to load more
            center("---type 'more' to load more---")
            page = page +1

        # Error messages    
        except Exception as e:
            if "code" in out:
                print("    Error code: ", out["code"]  )
                if out["code"] == -32500:
                    print("   SDK is still starting. Patience!")
            else:
                print( e)
            return

        # Making sure that we stop every time a new page is reached
        c =  input(typing_dots())
        if c != "more":
            break
    try:
        c = int(c)
        
    except:
        return

    while True:
        view(out["items"][c])
        c = input(typing_dots())
        if not c:
            break
        try:
            c = int(c)
        except:
            return

def view(i):

    # This function will give a user an ability to interact / read a
    # given comment.

    
     preview = "---!Failed Loading comment---"
     comment = ""
     support = 0
     bywho = "[anonymous]"
     replies = 0

     try:
         comment = i["comment"]
         preview = comment.replace("\n", " ")
         support = i["support_amount"]
         bywho = i["channel_name"]
         replies = i["replies"]
     except:
         pass

     # TIP LBC     # COMMENTS ( REPLIES ) # CHANNEL 
     d = {"categories":["Tip LBC", "Comments",  "Channel"],
                          "size":[1,1,3],
                          "data":[[support, replies, bywho]]}
     table(d, False)

     # Preview
     d = {"categories":["Preview"],
                          "size":[1],
                          "data":[[preview]]}
     table(d, False)
     
     

     # The help thing
     center("--- for comment commands list type 'help' --- ")

     # List of commands for autocomplete feature.
     complete([
         "help",
         "read",
         "channel",
         "comments",
         "reply",
         "delete",
         "edit"
     ])
     
     # let's implement commands
     while True:
        c =  input(typing_dots())

        if not c:
            break

        elif c == "help":
            markdown.draw("help/comments.md", "Comments Help")

        elif c == "read":
            
            savedes = open("/tmp/fastlbrylastcomment.md", "w")
            savedes.write(comment)
            savedes.close()
            
            markdown.draw("/tmp/fastlbrylastcomment.md", "Full Text Of a Comment")

        elif c == "channel":
            channel.simple(i["channel_url"])

        elif c == "comments":
            list(i["claim_id"], " ", i["comment_id"],)

        elif c.startswith("reply"):
            c = c + ' '
            post(i["claim_id"], c[c.find(" "):], i["comment_id"])

        elif c == "delete":
            out = check_output(["flbry/lbrynet", "comment", "abandon", i["comment_id"]])
            out = json.loads(out)
            try:
                if out["abandoned"] == True:
                    center("Comment deleted!", "bdgr")
                    break
            except:
                if out["message"].startswith("Couldn't find channel with channel_id"):
                    center("You can't delete a comment you didn't post", "bdrd")
        elif c.startswith("edit"):
            c = c + ' '
            update(i, c[c.find(" "):])

def post(claim_id, args,  parent_id=""):

    # This will post a comment under either a publication or a
    # comment as a reply. 

    if len(args) > 1:
        text = file_or_editor(args, "Type your reply here. Don't forget to save. Then return to FastLBRY.")
    else:
        text = input("   Text: ")
            
    
    post_as = channel.select("Reply as who? Select Channel.")
    

    if not post_as.startswith("@"):
        post_as = "@"+post_as
    

    if not parent_id:
        out = check_output(["flbry/lbrynet",
                         "comment", "create",
                        text,
                        '--channel_name='+post_as,
                        '--claim_id='+claim_id])
    else:
        out = check_output(["flbry/lbrynet",
                         "comment", "create",
                        text,
                        '--channel_name='+post_as,
                        '--parent_id='+parent_id,
                        '--claim_id='+claim_id])

    out = json.loads(out)
    if "message" in out:
        print("ERROR! "+out["message"], "bdrf")
    else:
        center("Comment is sent.", "bdgr")

def inbox(opt=10):

    # This function will return the latest comments from the latest
    # publications. Similar to an email inbox. But with a limitation.

    # There is no system in the SDK to implement a history of comments
    # seamlessly. So then I need to cash a large file of comments. Or
    # do something clever. I think there will be a few options.

    # You noticed the opt=10 preset on the top. It's the default value.
    # Basically the user might type one of 4 things.

    #    inbox
    #    inbox 40     (or any number what so ever)
    #    inbox all
    #    inbox cashed

    # Each will run a slightly different algorithm to get the inbox
    # comments.

    #    inbox

    # This will use the predefined 10 and read last 10 publications
    # comments to add. It will combine them with the pre-cashed ones
    # for the user to view. As you may imagine, giving it a number as
    # in:

    #    inbox 40
    #    inbox 2
    #    inbox 50

    # Will load this number of publications. To update with them the
    # cash and then present it to the user.

    #    inbox all

    # This one will take longest. But might be useful for some users.
    # This will go through all publications and cash comments from all
    # of them.

    #    inbox cashed

    # This one is the fastest of them. It will only read the cash file
    # and present it to the user. So for instance you want to quickly
    # go back to the inbox without loading anything at all.

    
    try:
        opt = int(opt)
        reached = opt
        goal = opt
    except:
        goal = 0
        if opt == "all":
            reached = True
        else:
            reached = False

    # Updating the cash file ( inbox.json )
    page = 0
    items_total = 0
    current_item = 0

    

    try:
        with open(settings.get_settings_folder()+'inbox.json') as json_file: 
            comments_cache = json.load(json_file)
    except:
        comments_cache = []
        
    checked_publications = []
    
    while reached > 0:

        if type(reached) == int:
            reached = reached - 50

        page = page + 1
        page_size = 50

        # Getting data about publications.


        if page != 1:
            out = check_output(["flbry/lbrynet",
                         "stream", "list",
                         '--page='+str(page),
                         '--page_size='+str(page_size),
                            "--no_totals"])
        else:
            out = check_output(["flbry/lbrynet",
                         "stream", "list",
                         '--page='+str(page),
                                '--page_size='+str(page_size)])

            

        # Now we want to parse the json
        items = []
        try:
            out = json.loads(out)
            items = out["items"]
        except:
            break

        if not items:
            break

        if page == 1:
            # Getting Totals to calculate the progress bar
            if reached == True:
                items_total = out["total_items"]
            else:
                try:
                    items_total = int(opt)
                except:
                    items_total = 0
        
        # Reading items from the items
        
        for publication in items:

            # skip dublicate publications. ( like when you edited
            # a publication )
            if publication["name"] in checked_publications:
                continue
            checked_publications.append(publication["name"])

            current_item = current_item + 1

            # If above the requested amount.
            if current_item > items_total:
                break
            
            # Draw progress bar                   
            progress_bar(current_item, items_total, publication["name"])

            # let's now get all the comments
            claim_id = publication["claim_id"]

            comment_page = 0
            
            while True:

                comment_page = comment_page + 1
                
                cout = check_output(["flbry/lbrynet",
                        "comment", "list", '--claim_id='+claim_id,
                         '--page='+str(comment_page),
                            '--page_size='+str(50),
                            '--include_replies'])
                
                try:
                    cout = json.loads(cout)
                except:
                    break

                if "items" not in cout:
                    break
                for i in cout["items"]:

                    # I want to add a few things into the comment data
                    i["publication_url"] = publication["permanent_url"]
                    i["publication_name"] = publication["name"]
                    try:
                        i["publication_title"] =  publication["value"]["title"]
                    except:
                        i["publication_title"] =  publication["name"]
                        
                    if i not in comments_cache:
                        comments_cache.append(i)
                    
                
    print()

    # Let's sort the comments based on the time they were sent
    comments_cache = sorted(comments_cache, key=lambda k: k['timestamp'], reverse=True) 

    with open(settings.get_settings_folder()+'inbox.json', 'w') as fp:
            json.dump(comments_cache, fp , indent=4)



    # Now that we have comments cached and ready. I can start actually showing
    # them.

    w, h = tsize()
    page_size = (h-5)
    page = 0

    while True:

        d = {"categories":["Tip LBC", "Comments", "Publication",  "Channel",  "Preview"],
                          "size":[1,1,4,2,4],
                          "data":[]}

        items = []
        
        for n, i in enumerate(comments_cache):

            startfrom = int( page * page_size )
            endat     = int( startfrom + page_size )

            if n in range(startfrom, endat):

                items.append(i)
                
                preview = "---!Failed Loading comment---"
                support = 0
                replies = 0
                where = "[some publication]"
                bywho = "[anonymous]"
                
                try:
                    comment = i["comment"]
                    preview = comment.replace("\n", " ")
                    where = i["publication_title"]
                    support = i["support_amount"]
                    bywho = i["channel_name"]
                    replies = i["replies"]
                    
                except:
                    pass
                
                d["data"].append([support, replies, where, bywho, preview])
        table(d)

        # Tell the user that he might want to load more
        center("---type 'more' to load more---")
        

        # Making sure that we stop every time a new page is reached
        c =  input(typing_dots())
        if c == "more":
            page = page +1
            continue

        try:
            c = int(c)
        except:
            return

        view(items[c])
        c = input(typing_dots())
            
def update(i, args):
    comment = i["comment"]

    if len(args) > 1:
        text = file_or_editor(args, comment)
    else:
        print("Comment: "+comment)
        text = input("Edited comment: ")

    out = check_output(["flbry/lbrynet",
                "comment", "update", "--comment_id="+i["comment_id"],
                "--comment="+text])
    out = json.loads(out)
    try:
        if out["message"].startswith("Couldn't find channel with channel_id"):
            center("You cant' edit a comment that isn't yours", "bdrd")
    except:
        center("Comment edited!", "bdgr")
