#####################################################################
#                                                                   #
#  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.  #
#                                                                   #
#####################################################################

from gi.repository import Gtk

def populate_dict(treeStore, data, parent=None):

    # This will take a dictionary and populate a tree out of it.

    if type(data) in [list, tuple]:
        # Inspired by the Json visualizer of Firefox
        newvalue = {}
        for n, i in enumerate(data):
            newvalue[n] = i
        data = newvalue
    
    for key in data:
        value = data[key]

        if not type(value) in [dict, list, tuple]:
            putvalue = str(value)
        else:
            putvalue = None
            
        currentIter = treeStore.append(parent, [str(key),putvalue])
        if type(value) in [dict, list, tuple]:
            populate_dict(treeStore, value, currentIter)
        
            
            
def data_widget(data):

    # This will create and return a treeview object

    data_tree_store = Gtk.TreeStore(str, str)
    populate_dict(data_tree_store, data)
    data_tree_view = Gtk.TreeView(data_tree_store)
    key_column = Gtk.TreeViewColumn("Category")
    text_col = Gtk.CellRendererText()
    text_col.set_property("editable", True)
    key_column.pack_start(text_col, True)
    key_column.add_attribute(text_col, "text", 0)
    data_tree_view.append_column(key_column)

    value_column = Gtk.TreeViewColumn("Information")
    text_col = Gtk.CellRendererText()
    text_col.set_property("editable", True)
    value_column.pack_start(text_col, True)
    value_column.add_attribute(text_col, "text", 1)
    data_tree_view.append_column(value_column)

    return data_tree_view
