####################################
#                                  #
#        COPYRIGHT NOTICE          #
#                                  #
# This file is a part of Victori-  #
# ous Children Studio Organizer.   #
# Or simply VCStudio. Copyright    #
# of J.Y.Amihud. But don't be sad  #
# because I released the entire    #
# project under a GNU GPL license. #
# You may use Version 3 or later.  #
# See www.gnu.org/licenses if your #
# copy has no License file. Please #
# note. Ones I used the GPL v2 for #
# it. It's no longer the case.     #
#                                  #
####################################

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

#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, "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, 
        win.current["w"]/2-250,
        100,
        500,
        win.current["h"]-200,
        10)
    
    # Exit button
    def do():
        win.url = "project_manager" 
        win.textactive = ""
        
        
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2+210,
        win.current["h"]-140,
        40,
        40,
        10,
        button=do,
        icon="cancel",
        tip=talk.text("cancel"))
    
      
        
    # Clipping everything
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2-250,
        100,
        500,
        win.current["h"]-260,
        10,
        fill=False)
    layer.clip()
    
    clip = [
        win.current["w"]/2-250,
        100,
        500,
        win.current["h"]-260]
    
    # Setting up the scroll
    if "pm_settings" not in win.scroll:
        win.scroll["pm_settings"] = 0
    
    current_Y = 0 # The max scroll value
    
    # Username 
    
    UI_elements.image(layer, win, "settings/themes/"\
        +win.settings["Theme"]+"/icons/user.png", 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"], 
        40,
        40)
    
    UI_elements.text(layer, win, "Username_setting",
        win.current["w"]/2-190,
        110 + current_Y + win.scroll["pm_settings"],
        420,
        40,
        set_text=win.settings["Username"],
        tip=talk.text("username"))
    
    if win.text["Username_setting"]["text"] != win.settings["Username"]:
        def do():
            win.settings["Username"] = win.text["Username_setting"]["text"]
            settings.write("Username", win.settings["Username"])
            
        UI_elements.roundrect(layer, win, 
            win.current["w"]/2-240+430,
            110 + current_Y + win.scroll["pm_settings"],
            40,
            40,
            10,
            button=do,
            icon="ok",
            tip=talk.text("checked"))
    
    
    current_Y += 50
    
    
    # Preparing lists.
    if "settings_lists" not in win.current:
        
        win.current["settings_lists"] = {}
        win.current["settings_lists"]["languages"] = settings.list_languages()
        win.current["settings_lists"]["languages_open"] = False
        win.current["settings_lists"]["themes"] = os.listdir(os.getcwd()+"/settings/themes")
        win.current["settings_lists"]["themes_open"] = False
    
    
    # Languages
    def do():
        win.current["settings_lists"]["languages_open"] = \
        not win.current["settings_lists"]["languages_open"]
        
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"],
        450,
        40,
        10,
        button=do,
        icon="font",
        tip=talk.text("change_language"))
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(win.current["w"]/2-180,
        current_Y + win.scroll["pm_settings"] + 135)
    layer.show_text(win.settings["Language"])
    
    current_Y += 50
    
    if win.current["settings_lists"]["languages_open"]:
        
        
        for lang in win.current["settings_lists"]["languages"]:
            if lang != win.settings["Language"]:
                
                UI_color.set(layer, win, "button_active")
                layer.move_to(win.current["w"]/2-227, 102 + current_Y + win.scroll["pm_settings"])
                layer.line_to(win.current["w"]/2-227, 152 + current_Y + win.scroll["pm_settings"])
                layer.stroke()
                
                def do():
                    win.settings["Language"] = lang
                    settings.write("Language", lang)
                    win.current["settings_lists"]["languages_open"] = False
                
                UI_elements.roundrect(layer, win, 
                    win.current["w"]/2-220,
                    110 + current_Y + win.scroll["pm_settings"],
                    430,
                    40,
                    10,
                    button=do,
                    icon="font",
                    tip=talk.text("set_language")+lang)
                
                
                UI_color.set(layer, win, "text_normal")
                layer.set_font_size(20)
                layer.move_to(win.current["w"]/2-150,
                    current_Y + win.scroll["pm_settings"] + 135)
                layer.show_text(lang)
                
                current_Y += 50
    
    
    
    # Themes. Finally we have some themes to choose from. And a good place to 
    # change them so the user would not need to do it manually. I think it's
    # important.
    
    # So there is a folder in the VCStudio installation called. settings/themes
    # which contains all of the themes. So let's look through it.
    
    # Themes
    def do():
        win.current["settings_lists"]["themes_open"] = \
        not win.current["settings_lists"]["themes_open"]
        
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"],
        450,
        40,
        10,
        button=do,
        icon="theme",
        tip=talk.text("change_theme"))
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(win.current["w"]/2-180,
        current_Y + win.scroll["pm_settings"] + 135)
    layer.show_text(win.settings["Theme"])
    
    current_Y += 50
    
    if win.current["settings_lists"]["themes_open"]:
        
        
        for lang in win.current["settings_lists"]["themes"]:
            if lang != win.settings["Theme"]:
                
                UI_color.set(layer, win, "button_active")
                layer.move_to(win.current["w"]/2-227, 102 + current_Y + win.scroll["pm_settings"])
                layer.line_to(win.current["w"]/2-227, 152 + current_Y + win.scroll["pm_settings"])
                layer.stroke()
                
                def do():
                    win.settings["Theme"] = lang
                    settings.write("Theme", lang)
                    win.current["settings_lists"]["themes_open"] = False
                    win.color = UI_color.get_table()
                
                UI_elements.roundrect(layer, win, 
                    win.current["w"]/2-220,
                    110 + current_Y + win.scroll["pm_settings"],
                    430,
                    40,
                    10,
                    button=do,
                    icon="theme",
                    tip=talk.text("set_theme")+lang)
                
                
                UI_color.set(layer, win, "text_normal")
                layer.set_font_size(20)
                layer.move_to(win.current["w"]/2-150,
                    current_Y + win.scroll["pm_settings"] + 135)
                layer.show_text(lang)
                
                current_Y += 50
    
    
    
    # BLUR
    
    blur_ok = "unchecked"
    if win.settings["Blur"]:
        blur_ok = "checked"
                
    def do():
        win.settings["Blur"] = not win.settings["Blur"]
        settings.write("Blur", win.settings["Blur"])
    
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"],
        450,
        40,
        10,
        button=do,
        icon=blur_ok,
        tip=talk.text("Blur"))
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(win.current["w"]/2-180,
        current_Y + win.scroll["pm_settings"] + 135)
    layer.show_text(talk.text("Blur"))
    
    current_Y += 50
    
    if win.settings["Blur"]:
        ablur_ok = "unchecked"            
        if win.settings["Auto_De-Blur"]:
            ablur_ok = "checked"
        
        def do():
            win.settings["Auto_De-Blur"] = not win.settings["Auto_De-Blur"]
            settings.write("Auto_De-Blur", win.settings["Auto_De-Blur"])
        
        UI_elements.roundrect(layer, win, 
            win.current["w"]/2-240,
            110 + current_Y + win.scroll["pm_settings"],
            450,
            40,
            10,
            button=do,
            icon=ablur_ok,
            tip=talk.text("Auto_De-Blur"))
        
        UI_color.set(layer, win, "text_normal")
        layer.set_font_size(20)
        layer.move_to(win.current["w"]/2-180,
            current_Y + win.scroll["pm_settings"] + 135)
        layer.show_text(talk.text("Auto_De-Blur"))

        current_Y += 50

        # Pixelated Blur

        ablur_ok = "unchecked"            
        if win.settings["PixelBlur"]:
            ablur_ok = "checked"
        
        def do():
            win.settings["PixelBlur"] = not win.settings["PixelBlur"]
            settings.write("PixelBlur", win.settings["PixelBlur"])
        
        UI_elements.roundrect(layer, win, 
            win.current["w"]/2-240,
            110 + current_Y + win.scroll["pm_settings"],
            450,
            40,
            10,
            button=do,
            icon=ablur_ok,
            tip=talk.text("PixelBlur"))
        
        UI_color.set(layer, win, "text_normal")
        layer.set_font_size(20)
        layer.move_to(win.current["w"]/2-180,
            current_Y + win.scroll["pm_settings"] + 135)
        layer.show_text(talk.text("PixelBlur"))
    
    current_Y += 50
    
    
    # ROUNDNESS
    
    # The roundness setting is a setting that will define how round the UI will
    # look. Potentially it can grow beyond 1.0, but will introduce weird rendering
    # artifacts. If you wan to do that be free to edit the Setting file manually.
    
    # I'm going to use the Scroller for this setting. But first we need an icon. And
    # I think the opengl icon is quite good for this.
    
    UI_elements.image(layer, win, "settings/themes/"\
        +win.settings["Theme"]+"/icons/opengl.png", 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"], 
        40,
        40)
        
    # Then i'm going to hack my way to it. I'm going to use a scroll for a scrooled
    # window as the slider. Creativity.
    
    maxround = 1000 # I need it do be a higher number then from 0 to 1 since we want
                    # to imulate the scroll
    
    if "roundness_setting" not in win.scroll:
        win.scroll["roundness_setting"] = 0 - win.settings["Roundness"] * 549
    
    
    if (0 - win.settings["Roundness"] * 549) != win.scroll["roundness_setting"]:
        win.settings["Roundness"]  = (win.scroll["roundness_setting"] / 549)*-1
        settings.write("Roundness", win.settings["Roundness"])
        
    UI_elements.scroll_area(layer, win, "roundness_setting", 
        int(win.current["w"]/2-200),
        110 + current_Y + win.scroll["pm_settings"]+33,
        450,
        0,
        maxround,
        bar=True,
        sideways=True,
        url="settings_layer"
        )
    
    
    current_Y += 50

    # AUTO DOWNLOAD IMAGES
    
    autod_ok = "unchecked"
    if win.settings["auto_download_images"]:
        autod_ok  = "checked"
                
    def do():
        win.settings["auto_download_images"] = not win.settings["auto_download_images"]
        settings.write("auto_download_images", win.settings["auto_download_images"])
    
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"],
        450,
        40,
        10,
        button=do,
        icon=autod_ok,
        tip=talk.text("auto_download_images"))
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(win.current["w"]/2-180,
        current_Y + win.scroll["pm_settings"] + 135)
    layer.show_text(talk.text("auto_download_images"))

    current_Y += 50
    
    # AUTO CHECK FOR UPDATES

    autod_ok = "unchecked"
    if win.settings["check-for-updates"]:
        autod_ok  = "checked"
                
    def do():
        win.settings["check-for-updates"] = not win.settings["check-for-updates"]
        settings.write("check-for-updates", win.settings["check-for-updates"])
    
    UI_elements.roundrect(layer, win, 
        win.current["w"]/2-240,
        110 + current_Y + win.scroll["pm_settings"],
        450,
        40,
        10,
        button=do,
        icon=autod_ok,
        tip=talk.text("check-for-updates"))
    
    UI_color.set(layer, win, "text_normal")
    layer.set_font_size(20)
    layer.move_to(win.current["w"]/2-180,
        current_Y + win.scroll["pm_settings"] + 135)
    layer.show_text(talk.text("check-for-updates"))

    
    UI_elements.scroll_area(layer, win, "pm_settings", 
        int(win.current["w"]/2-250),
        100,
        500,
        win.current["h"]-260,
        current_Y,
        bar=True,
        mmb=True,
        url="settings_layer"
        )
    
    
    return surface
