import json
import sys
import random
from tree_maker import Tree
from fpdf import FPDF
# ref https://pyfpdf.readthedocs.io/en/latest/

# declare input data
inputfile = (
    "/home/mara/webdev/sortingtree/cleaned_data/year_name_fam_subset.json"
)
in_data = open(inputfile, "r")
data = in_data.read()
in_data.close()
data_json = json.loads(data)

reddit_file = open(
    "/home/mara/webdev/sortingtree/cleaned_data/fam_reddit_subset.json"
)
reddit_data = reddit_file.read()
reddit_file.close()
reddit_json = json.loads(reddit_data)


# declare pdf format and margins
margin = 15

zine = FPDF(orientation="L", unit="mm", format="A4")

# set font for all text
# zine.add_font(
#     'CasaleNBP', '', r"./fonts/CasaletwoNbp-Bp4V.ttf", uni=True)
# tree_font = 'CasaleNBP'

zine.add_font(
    'Kp', '', r"./fonts/KpProgrammerAlternatesNbp-Zg1q.ttf", uni=True)
tree_font = 'Kp'
font_size = 15
zine.set_font(tree_font, '', font_size)

zine.set_margins(margin, margin, margin)
zine.add_page()
zine.set_xy(zine.w/2+margin, margin)

tree = Tree(zine)
root = tree.keys_sorting(data_json)


def make_bin_tree(output, zine, tree, root):
    global data_json
    global margin

    tree.draw_year_name(data_json, root, zine.h, zine.w, margin)
    zine.output(output)
    print("PDF saved as {}".format(output))


def make_bin_tree_reddit(output, zine, tree, root, reddit_json):
    global data_json
    global margin

    tree.draw_year_name_reddit(
        data_json, reddit_json, root, zine.h, zine.w, margin)
    zine.output(output)
    print("PDF saved as {}".format(output))


def make_bin_tree_position_nodes(output, zine, tree, root):
    global data_json
    global margin

    tree.preorder_position_nodes(data_json, root, zine.h, zine.w, margin)
    print("PDF saved as {}".format(output))


def make_bin_tree_weird_position_nodes(output, zine, tree, root):
    global data_json
    global margin

    tree.weird_position_nodes(data_json, root, zine.h, zine.w, margin)
    print("PDF saved as {}".format(output))


if __name__ == '__main__':
    # input text and output pdf
    if len(sys.argv) > 1:
        if len(sys.argv) == 4:
            output = sys.argv[3]
            inputfile = sys.argv[2]
            version = sys.argv[1]
        else:
            version = sys.argv[1]
            output = "./trees/test_%d.pdf" % int(version)

    else:
        version = random.randint(1, 100)
        inputfile = "./trees/array.txt"
        output = "./trees/test_%d.pdf" % int(version)

#    make_bin_tree(output, zine, tree, root)
#    make_bin_tree_reddit(output, zine, tree, root, reddit_json)
#    make_bin_tree_position_nodes(output, zine, tree, root)
    make_bin_tree_weird_position_nodes(output, zine, tree, root)

    # TODO: add functions of the following tree methods
#    tree.draw_year(json_data, root, x, y, zine.h, zine.w, margin, steps=30)
#    tree.print_nodes(root)
#    tree.draw_year_name_reddit(
#    treejson_data, root, x, y, max_height, margin, width)
#    tree.print_bin_tree(root, x, y, max_height, margin, width, steps=max_height/3)
#    tree.print_tree_terminal(root)
#    tree.design_tree(json_data, font_size, margin, steps=10)
#    root = zine.tree_sorting([4, 8, 7, 9, 10, 1, 20, 5, 15, 11])
#    root = zine.tree_sorting(["BIN", "ARY", "IS", "FOR", "SORTING", "TREES"])
#    sentence = "BIN ARY IS FOR SORTING TREES"
#    root = tree.tree_sorting(sentence.split())
