import bge
import GameLogic
import configobj
from configobj import ConfigObj

mainDir = GameLogic.expandPath("//")

fileName = mainDir + "config.ini"

config = ConfigObj(fileName, interpolation=True)
dict = bge.logic.globalDict

print('###############start game##########')
dict['music_player'] = 1

def to_dict(section, key):
    dict = bge.logic.globalDict
    value = config[key]
    if isinstance(value, str):
        if value.isdigit():
            value = int(value)
        else:
            try:
                value = float(value)
            except:
                pass
    dict[key] = value  

def readSettings():
    config.walk(to_dict)     

def from_dict(section, key):
    dict = bge.logic.globalDict
    config[key] = dict[key]

def writeSettings():
    config.walk(from_dict)  
    config.write()    
          
def setres():
    import GameLogic
    import bge
    scenes = bge.logic.getSceneList()
    main_scene = [scene for scene in scenes if scene.name=="main"][0]
    own = main_scene.objects['Empty']
    cont = GameLogic.getCurrentController()
    own2 = cont.owner
    dict = bge.logic.globalDict 
    resx = int(dict["resx"])
    resy = int(dict["resy"])
    fullscreen = bge.render.getFullScreen()
    print("fullscreen = ", fullscreen)
    if fullscreen != True:
        if dict['fullscreen_on'] == 1:
            bge.render.setFullScreen(True)
    if fullscreen == True:
        if dict['fullscreen_on'] == 0:
            bge.render.setFullScreen(False) 
            
    bge.render.setWindowSize(resx, resy)         
    print("resolution = ", resx, resy)       
        
def loadlevel():
    import GameLogic
    import bge
    scenes = bge.logic.getSceneList()
    main_scene = [scene for scene in scenes if scene.name=="main"][0]
    own = main_scene.objects['Empty']
    cont = GameLogic.getCurrentController()
    own2 = cont.owner

    dict = bge.logic.globalDict #Get the global dictionary
    
    if own["framerate"] == 1:    
        bge.render.showFramerate(1)
    if own["profile"] ==1:    
        bge.render.showProfile(1)   
    act = own2.actuators["level"]
    dict = bge.logic.globalDict
    level = dict['level']
    print('loading level', level)
    act.scene = level 
    cont.activate(own2.actuators["level"]) 
    dict['npause'] = False 
    
def timer():
    import GameLogic
    import bge    

    dict = bge.logic.globalDict    
    
    if dict['reload_timer'] == 1:
        scene = bge.logic.getCurrentScene()
            
    if dict['reload_timer'] > 0:
       dict['reload_timer'] -= 1         
