# THIS FILE IS A PART OF FreeGILE
# PYTHON 3

import os
import datetime
import json
from subprocess import *

# GTK module ( Graphical interface
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import Gdk
import cairo

# Own modules
from settings import settings
from settings import talk
from settings import fileformats
from settings import oscalls
from project_manager import pm_project

#UI modules
from UI import UI_elements
from UI import UI_color
from UI import UI_math
from UI import UI_Markdown

# Studio
from studio import studio_dialogs
from studio import analytics
from studio import story

def layer(win, call):
    
    ##########################################################################
    
    # This file will give the user documentation about various part of FreeGILE
    
    ##########################################################################


    # Determening whether we are in a Tiny mode or not. Whether to shrink
    # everything or keep it full screen.
    
    tiny = False
    if win.current["w"] < 1280:
        tiny = True
    
    
    # Making the layer
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, win.current['w'],
                                                      win.current['h'])
    layer = cairo.Context(surface)
    
    
    #text setting
    layer.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
    
    UI_color.set(layer, win, "dark_overdrop")
    layer.rectangle(
        0,
        0,
        win.current["w"],
        win.current["h"],
        )
    layer.fill()
    
    nodeX = 500
    if tiny:
        nodeX = 100
    
    UI_color.set(layer, win, "node_background")
    UI_elements.roundrect(layer, win, 
        100,
        100,
        nodeX,
        win.current["h"]-200,
        10)
    
    
    
    
    
    # Exit button
    def do():
        win.current["calls"][call]["var"] = False 
        
    exitX = 560
    if tiny:
        exitX = 150
        
    UI_elements.roundrect(layer, win, 
        exitX,
        win.current["h"]-140,
        40,
        40,
        10,
        button=do,
        icon="cancel",
        tip=talk.text("cancel"))
    
    
    x = 110
    y = 170
    width = nodeX - 20
    height = win.current["h"]-270
    
    # Search

    if not tiny:
    
        UI_elements.image(layer, win, "settings/themes/"\
            +win.settings["Theme"]+"/icons/search.png", 
            x+width/4,
            y-50, 
            40,
            40)

        UI_elements.text(layer, win, "in_help",
            x+width/4+50,
            y-50,
            width/2-50,
            40)
    
    ### MARKDOWN
    
    
        
    UI_Markdown.draw(layer, win, "help_markdown", 
        nodeX+200,
        100,
        win.current["w"]-(nodeX+300),
        win.current["h"]-200)

    
    # Clip
    
    UI_elements.roundrect(layer, win, 
        x,
        y,
        width,
        height-60,
        10,
        fill=False)
    layer.clip()
    
    clip = [
        x,
        y,
        width,
        height-60]
    
    # Little testing thing. Make it True to see where it's clipping.
    if False:
        # Background
        UI_color.set(layer, win, "dark_overdrop")
        layer.rectangle(x,y,width, height)
        layer.fill()
    
    
    # Setting up the scroll
    if "help" not in win.scroll:
        win.scroll["help"] = 0
    
    current_Y = 0
    
    # So for the help dialog there will be basically a list of documentations
    # With links to the various documentation peaces. for this I will need to
    # create a little dictionary
    
    documentations = {
        talk.text("readme"):[
            ["Markdown", "README.md"],
            ["icon", "scene"]
            ],
        talk.text("license"):[
            ["Markdown", "LICENSE.md"],
            ["icon", "scene"]
            ],
        talk.text("documentation_installation"):[
            ["Markdown", "wiki/docs/Installation.md"],
            ["video", "https://search.brave.com/"],
            ["icon", "new"]
            ],
        talk.text("documentation_project_manager"):[
            ["Markdown", "wiki/docs/Project-Manager.md"],
            ["video", "https://search.brave.com/"],
            ["icon", "configure_file"]
            ],
        talk.text("documentation_story_editor"):[
            ["Markdown", "wiki/docs/Story-Editor.md"],
            ["icon", "node"]
            ],
        talk.text("documentation_script_writer"):[
            ["Markdown", "wiki/docs/Script-Writer.md"],
            ["icon", "frase"]
            ],
        talk.text("documentation_analytics"):[
            ["Markdown", "wiki/docs/Analytics.md"],
            ["icon", "analytics"]
            ],
        talk.text("documentation_assets"):[
            ["Markdown", "wiki/docs/Assets.md"],
            ["update", "https://notabug.org/jyamihud/FreeGILE/wiki/Version+20.1211+%28Asset+Manager+Alpha+++Default+Blend+Files++Project%27s+Settings%29"],
            ["icon", "obj"]
            ],
        talk.text("documentation_link_assets"):[
            ["Markdown", "wiki/docs/LinkingAssets.md"],
            ["update", "https://notabug.org/jyamihud/FreeGILE/wiki/Version+20.1263+%28+Linking+Assets+++Configuring+Assets+%29"],
            ["icon", "link"]
            ],
        talk.text("documentation_render"):[
            ["Markdown", "wiki/docs/Rendering.md"],
            ["update", "https://notabug.org/jyamihud/FreeGILE/wiki/Version+20.1266+%28+Rendering+Of+Shots+%29"],
            ["icon", "render"]
            ],
        talk.text("documentation_multiuser"):[
            ["Markdown", "wiki/docs/Multiuser.md"],
            ["update", "https://notabug.org/jyamihud/FreeGILE/wiki/Version+21.0"],
            ["icon", "multiuser"]
            ]
        }
        
    
    # Okay let's draw this crazy behimith. 
    
    if "current_help_selected" not in win.current:
        win.current["current_help_selected"] = False
        
    for name in documentations:
        
        # Let's make the search work.
        
        # First I want it to automatically select the stuff with the right name
        if win.text["in_help"]["text"].lower() == name.lower():
            win.current["current_help_selected"] = name
            win.text["in_help"]["text"] = ""
        
        # Now let's ignore all those not in the search
        if win.text["in_help"]["text"] and win.text["in_help"]["text"].lower() not in  name.lower():
            continue
        
        # let's simplify the name
        doc = documentations[name]
        
        # There will be a simple button. That will open one up. 
        def do():
            
            win.text["in_help"]["text"] = ""
            win.scroll["markdown"] = 0
            if name != win.current["current_help_selected"]:
                win.current["current_help_selected"] = name
            else:
                win.current["current_help_selected"] = False

        # I want to make sure that there is no icon before making it
        # be "question"
        
        showicon  = "question"
        for entry in doc:
            if entry[0] == "icon":
                showicon = entry[1]
                
        UI_elements.roundrect(layer, win, 
            x,
            y+current_Y + win.scroll["help"],
            width,
            40,
            10,
            icon=showicon,
            button=do,
            tip=name)
        
        # And a text. The name of the entry
        if not tiny:

            UI_color.set(layer, win, "text_normal")
            layer.set_font_size(20)
            layer.move_to(x+50,
                    y+current_Y + win.scroll["help"]+25)
            layer.show_text(name)

        # Now if it's selected we going to see what links are inside.
        if name == win.current["current_help_selected"]:
            
            # But first let's draw a roundrect arround the douche
            
            UI_color.set(layer, win, "progress_background")
            UI_elements.roundrect(layer, win, 
                x,
                y+current_Y + win.scroll["help"],
                width,
                40,
                10,
                fill=False)
            layer.stroke()
            
            current_Y = current_Y + 50
            
            # Let's not list all the entries.
            
            for entry in doc:
            
                
                    
                if entry[0] == "Markdown":

                    
                        
                    win.current["mdfs"]["help_markdown"] = entry[1]
                    
                    #win.current["current_help_selected"] = ""

                    continue

                # icon do not show
                elif entry[0] == "icon":
                    continue
                
                # Launch button
                def do():
                    oscalls.Open(entry[-1])
                
                UI_elements.roundrect(layer, win, 
                    x+10,
                    y+current_Y + win.scroll["help"],
                    width-20,
                    40,
                    10,
                    icon=entry[0],
                    button=do,
                    tip=entry[-1])
                
                # And a text. The name of the entry

                if not tiny:

                    UI_color.set(layer, win, "text_normal")
                    layer.set_font_size(20)
                    layer.move_to(x+70,
                            y+current_Y + win.scroll["help"]+25)
                    layer.show_text(talk.text("documentation_type_"+entry[0]))

                current_Y = current_Y + 50
        else:
        
            current_Y = current_Y + 50
    
    
    # Call
    def do():
        os.system("xdg-open https://meet.jit.si/FreeGILEDevelopmentConference")
        talk.alert(talk.text("opened_browser"))
        
    UI_elements.roundrect(layer, win, 
        x,
        y+current_Y + win.scroll["help"],
        width,
        40,
        10,
        button=do,
        icon="call",
        tip=talk.text("contact_us_tip")) 
    
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(x+50,
            y+current_Y + win.scroll["help"]+25)
    layer.show_text(talk.text("contact_us_now"))
    
    current_Y = current_Y + 50

    # Matrix Chat
    def do():
        os.system("xdg-open https://matrix.to/#/#FreeGILE:matrix.org")
        talk.alert(talk.text("opened_browser"))

    UI_elements.roundrect(layer, win, 
        x,
        y+current_Y + win.scroll["help"],
        width,
        40,
        10,
        button=do,
        icon="frase",
        tip=talk.text("matrix_chat_tooltip")) 
    
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(x+50,
            y+current_Y + win.scroll["help"]+25)
    layer.show_text(talk.text("matrix_chat_button"))
    
    current_Y = current_Y + 50

    
    # Report Bug Button
    def do():
        os.system("xdg-open https://notabug.org/troler/FreeGILE/issues")
        talk.alert(talk.text("opened_browser"))
        
    UI_elements.roundrect(layer, win, 
        x,
        y+current_Y + win.scroll["help"],
        width,
        40,
        10,
        button=do,
        icon="bug",
        tip=talk.text("report_bug_tip"))
    
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(x+50,
            y+current_Y + win.scroll["help"]+25)
    layer.show_text(talk.text("report_bug_now"))
    
    current_Y = current_Y + 50
    
    ###########################
        
    UI_elements.scroll_area(layer, win, "help", 
        x,
        y,
        width,
        height-60,
        current_Y,
        bar=True,
        mmb=True,
        url="help"
    )
    
    
    return surface
