#####################################################################
#                                                                   #
#  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 is for features that are developed only for Odysee. Apart from
# livestreams which are in a dedicated file.

import os
import json
import urllib.request
import urllib.parse
from gi.repository import Gtk

from flbry import ui
from flbry import fetch

# This will make odysee think that we are just a normal browser running windows ( lol )
# and connecting from Odysee.com
headers = {"Origin":"https://odysee.com",
           "Referer":"https://odysee.com/",
           "User-Agent":"Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"}

def get_odysee_notifications(win):

    # This function will query the odysee api for notifications.

    auth = win.settings["auth_token"]
    if not auth:
        return False

    url = "https://api.odysee.com/notification/list?auth_token="+auth+"&is_app_readable=true&type=comment_replies%2Chyperchat_replies"

    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))
    if "data" in data:
        return data["data"]
    else:
        return False

def get_odysee_views(win, claim_id):

    # This function will return the views of a particular claim ID

    auth = win.settings["auth_token"]
    if not auth:
        return False
    
    url="https://api.odysee.com/file/view_count?auth_token="+auth+"&claim_id="+claim_id
    
    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))
    if "data" in data:
        return data["data"]
    else:
        return False

def get_odysee_subs(win, claim_id):

    # This function will return the views of a particular claim ID

    auth = win.settings["auth_token"]
    if not auth:
        return False
    
    url="https://api.odysee.com/subscription/sub_count?auth_token="+auth+"&claim_id="+claim_id
    
    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))
    if "data" in data:
        return data["data"]
    else:
        return False

def sync_function(email, password):

    # This function will sync the odysee wallet.

    # This function is being possible thanks to TrueAuraCoral
    # on Notabug. https://notabug.org/jyamihud/FastLBRY-terminal/issues/17#issuecomment-29418

    # First we create a "new" user. It will generate an authentication token
    # and then we could active that token using the email and password. So the
    # server of Odysee could give us access to our wallet.

    url = "https://api.odysee.com/user/new"
    req = urllib.request.Request(url, data=None)
    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))

    # We should have our auth token. If we don't, we abort the operation.
    
    try:
        auth_token = data["data"]["auth_token"]
    except:
        return "Failed to generate auth token."

    # Next step is authenticate this auth-token that we just got.
    # For some reason Odysee doesn't want a json. But a url string as a
    # payload. IDK what is wrong with them.

    login_data = "auth_token="+urllib.parse.quote_plus(auth_token)\
        +"&email="+urllib.parse.quote_plus(email)\
        +"&password="+urllib.parse.quote_plus(password)
    login_data = login_data.encode("utf-8")

    
    try:
        url = "https://api.odysee.com/user/signin"
        req = urllib.request.Request(url, data=login_data)
        f = urllib.request.urlopen(req)
        data = json.loads(f.read().decode('utf-8'))

    except Exception as e:
        print(e)
        return "Email or Password wrong."

    # Now we have the authenticated auth-token.
    # Now we can go to the next step.

    wallet_hash = fetch.lbrynet("sync_hash")

    # Next step. To retrieve the wallet from Odysee.

    hash_data = "auth_token="+urllib.parse.quote_plus(auth_token)\
        +"&hash="+urllib.parse.quote_plus(wallet_hash)
    hash_data = hash_data.encode("utf-8")
    
    try:
        url = "https://api.lbry.com/sync/get"
        req = urllib.request.Request(url, data=hash_data)
        f = urllib.request.urlopen(req)
        data = json.loads(f.read().decode('utf-8'))

    except Exception as e:
        print(e)
        return "Could not recieve wallet."

    # And the last step. Applying the sync.

    sync_apply = fetch.lbrynet("sync_apply",{"password": "",
                                             "blocking": True,
                                             "data": data["data"]["data"]})

    return {"auth_token":auth_token}
    
    
def sync_ui(win):

    # This is an odysee login window.

    dialogWindow = Gtk.Dialog("FastLBRY - Sync with Odysee",
            buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                     Gtk.STOCK_OK, Gtk.ResponseType.OK),
        )

    #dialogWindow.set_size_request(300,300)
    dialogWindow.set_default_icon_from_file("icon.png")

    mainbox = dialogWindow.get_content_area()

    # Email

    ebox = Gtk.HBox()
    mainbox.pack_start(ebox, 0,0,5)

    ebox.pack_start(Gtk.Label("Email : "), 0,0,5)
    email = Gtk.Entry()
    ebox.pack_start(email, 1,1,5)

    # Password
    
    ebox = Gtk.HBox()
    mainbox.pack_start(ebox, 0,0,5)

    ebox.pack_start(Gtk.Label("Password : "),0,0,5)
    passw, passe =  ui.password_entry(win)
    ebox.pack_start(passw, 1,1,5)


    mainbox.show_all()
    response = dialogWindow.run()

    if response == Gtk.ResponseType.OK:

        # Before we start the sync function. We need to tell the user that it might take a while

        email = email.get_text()
        password = passe.get_text()
        
        for i in mainbox.get_children():
            i.destroy()
        mainbox.pack_start(Gtk.Label("Syncing ... ( may take a while )"), 1,1,1)
        mainbox.show_all()
        
        response = sync_function(email, password)

        if type(response) == dict:
                      
            dialogWindow.destroy()
            return response

        else:
            for i in mainbox.get_children():
                i.destroy()
                mainbox.pack_start(Gtk.Label(response), 1,1,1)
                mainbox.show_all()
    
