# THIS FILE IS A PART OF VCStudio
# 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 VCStudio
    
    ##########################################################################
    
    
    # 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()
    
    
    
    UI_color.set(layer, win, "node_background")
    UI_elements.roundrect(layer, win, 
        100,
        100,
        500,
        win.current["h"]-200,
        10)
    
    
    
    
    
    # Exit button
    def do():
        win.current["calls"][call]["var"] = False 
        
        
    UI_elements.roundrect(layer, win, 
        560,
        win.current["h"]-140,
        40,
        40,
        10,
        button=do,
        icon="cancel",
        tip=talk.text("cancel"))
    
    
    x = 110
    y = 170
    width = 500 - 20
    height = win.current["h"]-270
    
    # Search
    
    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", 
        700,
        100,
        win.current["w"]-800,
        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"],
            ["scene", "https://notabug.org/jyamihud/VCStudio/src/master/README.md"]
            ],
        talk.text("license"):[
            ["Markdown", "LICENSE.md"],
            ],
        talk.text("documentation_installation"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Installation+%7C+Version+20.128"],
            ["video", "https://open.lbry.com/@blender-organizer:5/rnd0001-4061:1?r=7YADjAZEbHJg8n4qV5rAuBh5Hca7cZQK"]
            ],
        talk.text("documentation_project_manager"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Project+Manager+%7C+Version+20.128"],
            ["video", "https://open.lbry.com/@blender-organizer:5/Victorious-Children-Studio-Organizer-%28-VCStudio-%29---Project-Manager-Tutorial---Creating-Projects---Scanning-the-OS---Settings:7?r=7YADjAZEbHJg8n4qV5rAuBh5Hca7cZQK"]
            ],
        talk.text("documentation_story_editor"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Story+Editor+%7C+Version+20.128+"]
            ],
        talk.text("documentation_script_writer"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Script+Writer+%7C+Version+20.128"]
            ],
        talk.text("documentation_analytics"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Analytics+%7C+Version+20.128"]
            ],
        talk.text("documentation_assets"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Assets+%7C+Version+20.128"],
            ["update", "https://notabug.org/jyamihud/VCStudio/wiki/Version+20.1211+%28Asset+Manager+Alpha+++Default+Blend+Files++Project%27s+Settings%29"]
            ],
        talk.text("documentation_link_assets"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Linking+Assets+%7C+Version+20.128"],
            ["update", "https://notabug.org/jyamihud/VCStudio/wiki/Version+20.1263+%28+Linking+Assets+++Configuring+Assets+%29"]
            ],
        talk.text("documentation_render"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Rendering+%7C+Version+20.128"],
            ["update", "https://notabug.org/jyamihud/VCStudio/wiki/Version+20.1266+%28+Rendering+Of+Shots+%29"]
            ],
        talk.text("documentation_multiuser"):[
            ["scene", "https://notabug.org/jyamihud/VCStudio/wiki/Documenation+%7C+Multiuser+%7C+Version+21.1"],
            ["update", "https://notabug.org/jyamihud/VCStudio/wiki/Version+21.0"]
            ]
        }
        
    
    # 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"] = ""
            if name != win.current["current_help_selected"]:
                win.current["current_help_selected"] = name
            else:
                win.current["current_help_selected"] = False
        
        UI_elements.roundrect(layer, win, 
            x,
            y+current_Y + win.scroll["help"],
            width,
            40,
            10,
            icon="question",
            button=do)
        
        # And a text. The name of the entry
        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":

                    # Sometimes a language File is provided. let's
                    # look for it.

                    l = win.settings["Language"]

                    if os.path.exists(os.getcwd()+"/"+entry[1][:-3]+"_"+l+".md"):
                        entry[1] = entry[1][:-3]+"_"+l+".md"
                        
                    win.current["mdfs"]["help_markdown"] = entry[1]
                    #win.current["current_help_selected"] = ""

                    continue
                
                # Launch button
                def do():
                    oscalls.Open(entry[-1])
                
                UI_elements.roundrect(layer, win, 
                    x+20,
                    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
                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/VCStudioDevelopmentConference")
        
        
    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
    
    # Report Bug Button
    def do():
        os.system("xdg-open https://notabug.org/jyamihud/VCStudio/issues/new")
        
        
    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