# THIS IS A SOURCE CODE FILE FROM I'M NOT EVEN HUMAN THE GAME.
# IT COULD BE USED IN A DIFFERENT PIECE OF SOFTWARE ( LIKE A
# DIFFERENT GAME ), BUT IT WAS ORIGINALLY WRITTEN FOR I'M NOT
# EVEN HUMAN THE GAME.

# THE DEVELOPERS OF THE GAME ARE : (C) J.Y.AMIHUD, AYYZEE AND 
# OTHER CONTRIBUTORS. THIS AND OTHER FILES IN THIS GAME,
# UNLESS SPECIFICALLY NOTED, COULD BE USED UNDER THE TERMS OF
# GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER VERSION.

import os
import json

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

from modules import ui

def layer(game):

    # Setting up a cairo layer
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                                 game.current['w'],
                                 game.current['h'])
    layer = cairo.Context(surface)
    layer.set_antialias(cairo.ANTIALIAS_NONE)


    
    ui.color(game, layer, "#FFFFFF")
    ui.text(game, layer, "Settings",
            int(game.current["w"]/2), 5,
            align="center")

    def do():
        game.scene = "main_menu"
    
    ui.button(game, layer,
              5, 
              5 , 
              int(game.current["w"] / 3 ) - 30 ,
              13,
              menu="settings",
              string="< Back",
              func=do)

    # Setting up the scroller
    if "settings" not in game.scroll:
        game.scroll["settings"] = 0

    current_Y = 0
    layer.rectangle(4,20,
                game.current["w"]-10,
                game.current["h"]-25)
    layer.clip()


<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
    # WORLD

    if "settings_world_active" not in game.current:
        game.current["settings_world_active"] = False

    wicon = "right"
    if game.current["settings_world_active"]:
        wicon = "down"
        
    def do():
        game.current["settings_world_active"] = not game.current["settings_world_active"]
        game.menus = {}
<<<<<<< HEAD
=======
=======
    # World
    def do():
        print("world")
>>>>>>> 4cee21d (made game red)
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
        
    ui.button(game, layer,
              5,
              current_Y+25, 
              int(game.current["w"]) - 11 ,
              13,
              menu="settings",
              scroll="settings",
<<<<<<< HEAD
              icon=wicon,
              string="World: "+game.worlds[game.settings["world"]]["title"],
              func=do)
    current_Y += 15

    # List of worlds
    if game.current["settings_world_active"]:

        for world in game.worlds:
            def do():
                game.settings["world"] = world
                game.current["settings_world_active"] = False
                game.menus = {}
                save_settings(game)

            wicon = "radio_false"
            if game.settings["world"] == world:
                wicon = "radio_true"
                
            ui.button(game, layer,
                      15,
                      current_Y+25, 
                      int(game.current["w"]) - 36 ,
                      13,
                      menu="settings",
                      scroll="settings",
                      icon=wicon,
                      string=str(game.worlds[world]["title"]),
=======
<<<<<<< HEAD
              icon=wicon,
=======
>>>>>>> 4cee21d (made game red)
              string="World: I'm Not Even Human",
              func=do)
    current_Y += 15

<<<<<<< HEAD
    # List of worlds
    if game.current["settings_world_active"]:

        for world in os.listdir(os.getcwd()+"/assets/worlds"):
            def do():
                print(world)
        
            ui.button(game, layer,
                      15,
                      current_Y+25, 
                      int(game.current["w"]) - 37 ,
                      13,
                      menu="settings",
                      scroll="settings",
                      icon="radio_false",
                      string=str(world),
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
                      func=do)

            # Editor
            def do():
<<<<<<< HEAD
                game.settings["world"] = world
                game.current["settings_world_active"] = False
                game.menus = {}
                save_settings(game)

                game.scene = "editor"
=======
                print("edit")
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
        
            ui.button(game, layer,
                      game.current["w"]-19,
                      current_Y+25, 
                      13,
                      13,
                      menu="settings",
                      scroll="settings",
                      icon="edit",
                      func=do)
            
            current_Y += 15
<<<<<<< HEAD
    
    # PIXELS PER PIXEL
    
=======
=======
    # Pixels Per Pixels
    ui.color(game, layer, "#FF0000")
    ui.text(game, layer, "Pixels size: ",
            5,
            current_Y+28+game.scroll["settings"])

    def do():
        game.settings["pixels"] += 1
        
    ui.button(game, layer,
              game.current["w"]-75,
              current_Y+25, 
              13,
              13,
              menu="settings",
              scroll="settings",
              string="+",
              func=do)

    ui.color(game, layer, "#FF0000")
    ui.text(game, layer, str(game.settings["pixels"]),
            game.current["w"]-50,
            current_Y+28+game.scroll["settings"])
    
    def do():
        game.settings["pixels"] -= 1
        if game.settings["pixels"] < 1:
            game.settings["pixels"] = 1
        
    ui.button(game, layer,
              game.current["w"]-19,
              current_Y+25, 
              13,
              13,
              menu="settings",
              scroll="settings",
              string="-",
              func=do)
    
    current_Y += 15
>>>>>>> 4cee21d (made game red)
    
    # PIXELS PER PIXEL
    
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
    ui.color(game, layer, "#00FF00")
    ui.text(game, layer, "Pixels size: ",
            5,
            current_Y+28+game.scroll["settings"])

    def do():
        game.settings["pixels"] += 1
<<<<<<< HEAD
        save_settings(game)
=======
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
        
    ui.button(game, layer,
              game.current["w"]-75,
              current_Y+25, 
              13,
              13,
              menu="settings",
              scroll="settings",
              string="+",
              func=do)

    ui.color(game, layer, "#00FF00")
    ui.text(game, layer, str(game.settings["pixels"]),
            game.current["w"]-50,
            current_Y+28+game.scroll["settings"])
    
    def do():
        game.settings["pixels"] -= 1
        if game.settings["pixels"] < 1:
            game.settings["pixels"] = 1
<<<<<<< HEAD
        save_settings(game)
=======
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
        
    ui.button(game, layer,
              game.current["w"]-19,
              current_Y+25, 
              13,
              13,
              menu="settings",
              scroll="settings",
              string="-",
              func=do)
    

    current_Y += 15

    # FULLSCREEN
    
    ficon = "fullscreen"
    if game.settings["fullscreen"]:
        ficon = "unfullscreen"
    
    def do():
        game.settings["fullscreen"] = not game.settings["fullscreen"]
<<<<<<< HEAD
        save_settings(game)
=======
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
        
    ui.button(game, layer,
              5,
              current_Y+25, 
<<<<<<< HEAD
              int(game.current["w"]/2) - 5,
=======
              int(game.current["w"]/2) - 10,
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
              13,
              menu="settings",
              scroll="settings",
              icon=ficon,
              string="Fullscreen",
              func=do)

    # TESTING MODE

    dicon = "false"
    if game.current["testing"]:
        dicon = "true"
    
    def do():
        game.current["testing"] = not game.current["testing"]
        
    ui.button(game, layer,
<<<<<<< HEAD
              int(game.current["w"]/2) + 2,
              current_Y+25, 
              int(game.current["w"]/2) - 8,
=======
              int(game.current["w"]/2) + 4,
              current_Y+25, 
              int(game.current["w"]/2) - 10,
>>>>>>> 4f59bf84d5c1dc67fcc03c78488f20af217ceab7
              13,
              menu="settings",
              scroll="settings",
              icon=dicon,
              string="Debug Mode",
              func=do)
    

    current_Y += 15
    
    
    ui.scroll_area(game, layer, "settings", 5,20,
                   game.current["w"]-10,
                   game.current["h"]-25,
                   current_Y)

    # Navigating the menus
    ui.button_navigate(game, "settings")
    
            
    return surface

def update_worlds(game):

    """This function updates the selection of playable worlds."""

    game.worlds = {}
    for world in os.listdir(os.getcwd()+"/assets/worlds"):
        try:
            with open("assets/worlds/"+world+"/world.json") as f:
                metadata = json.load(f)
        except:
            metadata = {"title":world}

        game.worlds[world] = metadata

def get_settings_folder(folder="ineh/"):

    try:
        data_dir = os.environ["XDG_DATA_HOME"] + "/" + folder
    except:
        data_dir = os.path.expanduser("~/.local/share/"+folder)

    try:
        os.makedirs(data_dir)
    except:
        pass

    return data_dir
        
def save_settings(game):

    """This function saves the settings"""

    with open(get_settings_folder()+"config.json", 'w') as f:
        json.dump(game.settings, f, indent=4, sort_keys=True)

def load_settings(game):

    """This function loads settings, or creates the settings
       from defautls"""

    defaults = {
        "pixels":4,    # How big are pixels on the screen
        "fullscreen":False, # Whether the games is fullscreen
        "world":"ImNotEvenHuman"} # What wold you are playing
    try:
        with open(get_settings_folder()+"config.json") as f:
            game.settings = json.load(f)
    except:
        game.settings = defaults
    
