# THIS FILE IS A PART OF VCStudio
# PYTHON 3

import os

# 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 project_manager import pm_project

from studio import analytics
from studio import story
from studio import studio_gtk
from studio import studio_dialogs

#UI modules
from UI import UI_elements
from UI import UI_color


def layer(win):
    
    # 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, "darker_parts")
    UI_elements.roundrect(layer, win,
        50,
        5, 
        win.current["w"] - 55,
        win.current["h"] - 30,
        30)
    
    # Little verion thing in the bottom corner
    UI_color.set(layer, win, "testing_banner")
    layer.set_font_size(15)
    layer.move_to(win.current["w"]-80, win.current["h"] - 7)
    layer.show_text(str(win.version))
    
    
    # Side bar. First 3. New project / Search Projects / Configure Project.
    
    # New Project
    def do():
        print("New Project")
        win.url = "new_project"
    
    UI_elements.roundrect(layer, win,
        5,
        5, 
        40,
        40,
        10,
        do,
        "new_file",
        talk.text("createnewproject_tooltip"),
        url="project_manager")
    
    
    
    # Search for projects
    def do():
        
        win.url = "scan_projects"
        
    UI_elements.roundrect(layer, win,
        5,
        55, 
        40,
        40,
        10,
        do,
        "search_file",
        talk.text("scanforprojects_tooltip"),
        url="project_manager")
    
    
    # Configure
    if win.current["project"] and pm_project.is_legacy(win.current["project"]):
    
        def do():
            print("configure")
            #GLib.timeout_add(1, studio_gtk.run, win )
            analytics.save(win.current["project"], win.projects[win.current["project"]])
            story.save(win.current["project"], story.get_legacy(win.current["project"]))
            
            # Makinf the set folder
            try:
                os.mkdir(win.current["project"]+"/set/")
            except:
                pass
            
            # Copy the project.data to set
            try:
                f = open(win.current["project"]+"/project.progress")
                t = open(win.current["project"]+"/set/project.progress", "w")
                t.write(f.read())
                t.close()
            except:
                pass
                
        UI_elements.roundrect(layer, win,
            5,
            110, 
            40,
            40,
            10,
            do,
            "configure_file",
            talk.text("convertoldproject_tooltip"),
            url="project_manager")
        
    
    # Side bar. Last 3. Internet things / Updater / Settings
    
    # Internet things
    def do():
        
        # I used to have a specific UI instance for the help layer here. But
        # it proved to be too hard to maintain. So I'm changing it to a different
        # dialog. I'm not going to delete the help layer it self. For curious 
        # poeple.
        
        # win.url = "help_layer"
        
        # The following commands are the replacement. So if you want to see how
        # it used to look. Comment those. So not to launch both in the same time.
        
        def after(win, var):
           pass
        
        studio_dialogs.help(win, "help", after, SEARCH=talk.text("documentation_project_manager"))
    
    UI_elements.roundrect(layer, win,
        5,
        win.current["h"]-150, 
        40,
        40,
        10,
        do,
        "question",
        talk.text("pm_internet_tooltip"),
        url="project_manager")
    
    # Update
    def do():
        win.url = "update_layer"
    
    UI_elements.roundrect(layer, win,
        5,
        win.current["h"]-95, 
        40,
        40,
        10,
        do,
        "update",
        talk.text("Update"),
        url="project_manager")
    
    # I gonna draw a little thingy for if a new update is available
    try:
        if win.update["count"]:
            count = str(win.update["count"])
            
            UI_color.set(layer, win, "node_background")
            UI_elements.roundrect(layer, win,
                30,
                win.current["h"]-100, 
                len(count)*12+6,
                25,
                5)
            layer.fill()
            UI_color.set(layer, win, "text_normal")
            layer.set_font_size(20)
            layer.move_to(33,win.current["h"]-80)
            layer.show_text(count)
            
    except:
        pass
    
    # Settings
    def do():
        win.url = "settings_layer"
    
    UI_elements.roundrect(layer, win,
        5,
        win.current["h"]-45, 
        40,
        40,
        10,
        do,
        "settings",
        talk.text("Settings"),
        url="project_manager")
    
    # Now let's make previews of projects. I think each one will be it's own
    # layer thingy. Just so I could draw things inside them.
    
    # Clipping so it wont draw beyon the frame
    UI_elements.roundrect(layer, win,
        50,
        5, 
        win.current["w"] - 55,
        win.current["h"] - 30,
        30,
        fill=False)
    layer.clip()
    
    # Setting up scroll for Projects
    if "pm_scroll" not in win.current:
        win.current["pm_scroll"] = 0.0
    
    # Setting up tilling
    tileY = 0 
    tileX = 0
    
    if "pm_main" not in win.scroll:
        win.scroll["pm_main"] = 0
    
    for num, project in enumerate(pm_project.get_list()):
        
        
        if tileX > (win.current["w"]-55)-391:
            tileY += 330
            tileX = 0
        
        project_node(layer, win, 60+tileX, 15+tileY+ win.scroll["pm_main"], project)
        
        tileX += 360
    
    UI_elements.scroll_area(layer, win, "pm_main", 
        50,
        5, 
        win.current["w"] - 55,
        win.current["h"] - 30,
        tileY+340,
        bar=True,
        mmb=True,
        url="project_manager"
        )
    
    
    
    return surface
    
def project_node(layer, win, x, y, project):
    
    # This function will draw a project to a given place.
    node_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, win.current['w'],
                                                           win.current['h'])
    node = cairo.Context(node_surface)
    node.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
    
    
    
    
    
    # Before we gonna do clip. Let's put here the logic of the node.
    def do():
        print(project)
        win.current["project"] = project
    
    Legacytip = ""
    nameonly = project[project.rfind("/")+1:]
    timefraction = 0.0
    projectfraction = 0.0
    
    if pm_project.is_legacy(project): 
        Legacytip = "\nLegacy (Blender-Organizer)"
        
        # Getting info about the project. For now only Legacy. Since nothing is
        # written for the new stuff.
        
        try:
            if project not in win.projects:
                win.projects[project] = analytics.get_legacy(project)
            
            nameonly = win.projects[project]["name"]
            timefraction = win.projects[project]["timepassed"]
            projectfraction = win.projects[project]["fraction"]
        except:
            pass
    
    else:
        try:
            if project not in win.projects:
                win.projects[project] = analytics.load(project)
            
            nameonly = win.projects[project]["name"]
            timefraction = win.projects[project]["timepassed"]
            projectfraction = win.projects[project]["fraction"]
        except:
            pass
    
            
    node.set_line_width(10)
    UI_elements.roundrect(node, win,
        x-5,
        y-5, 
        350+10,
        320+10,
        20+5,
        button=do,
        fill=False,
        tip=project+Legacytip,
        url="project_manager")
    node.stroke()
    
    # If project is selected
    if win.current["project"] == project and win.previous["project"] == project:
        UI_color.set(node, win, "button_active")
        UI_elements.roundrect(node, win,
            x-5,
            y-5, 
            350+10,
            320+10,
            20+5,
            button=False,
            fill=False
            )
        node.stroke()
        
        def do():
            pm_project.load(project, win)
            #Gtk.main_quit()  # Here I might do some kind a setting later
            
        UI_elements.roundrect(node, win,
            x-5,
            y-5, 
            350+10,
            320+10,
            20+5,
            button=do,
            fill=False,
            url="project_manager"
            )
        
        # Enter keys
        if win.url == "project_manager":
            if 65293 in win.current["keys"] or 65421 in win.current["keys"]:
                do()
                win.current["keys"].remove(65293)
                win.current["keys"].remove(65421)
    
    # This next roundrect will both be the backdrop of the node and both will 
    # clip the node content. All folowing graphics will be drawn clipped to the
    # current roundrect.
    
    
    UI_color.set(node, win, "node_background")
    UI_elements.roundrect(node, win,
        x,
        y, 
        350,
        320,
        20)
    
    # Clip
    UI_elements.roundrect(node, win,
        x,
        y, 
        350,
        320,
        20,
        fill=False)
    node.clip()
    
    if os.path.exists(project+"/set/banner.png"):
        UI_elements.image(node, win, project+"/set/banner.png",
        x,y,350,320)
    elif os.path.exists(project+"/py_data/banner.png"):
        UI_elements.image(node, win, project+"/py_data/banner.png",
        x,y,350,320)
    else:
        UI_elements.image(node, win, "icon.png",
        x,y,350,320)
   
    # Top Banner thingy
    if pm_project.is_legacy(project): 
        UI_color.set(node, win, "node_badfile")
    else:
        UI_color.set(node, win, "node_blendfile")
    
    node.rectangle(x,y,350,40)
    node.fill()
    
    # Name of the project
    
    UI_color.set(node, win, "text_normal")
    node.set_font_size(20)
    node.move_to(x+175-len(nameonly)*12/2,y+25)
    node.show_text(nameonly)
    
    # Bottom widget part
    UI_color.set(node, win, "node_background")
    node.rectangle(x,y+250,350,100)
    node.fill()
    
    # Finally the progress bar HELL YEAH. (I had to write 3 hard parsing 
    # algorythms only to read percentage from the project)
    
    #Background
    UI_color.set(node, win, "progress_background")
    UI_elements.roundrect(node, win,
        x+20,
        y+280, 
        310,
        10,
        10)
    
    #Time-passed 
    UI_color.set(node, win, "progress_time")
    UI_elements.roundrect(node, win,
        x+20,
        y+280, 
        310*timefraction,
        10,
        10)
    
    #Done 
    UI_color.set(node, win, "progress_active")
    UI_elements.roundrect(node, win,
        x+20,
        y+280, 
        310*projectfraction,
        10,
        10)
    
    # Drawing the Node on the main layer.
    layer.set_source_surface(node_surface, 0,0)
    layer.paint()