#from bge import logic
import GameLogic
#from os.path import dirname, realpath, join

#BASE_DIR = os.path.join( os.path.dirname( __file__ ), '..' )
mainDir = GameLogic.expandPath("//")

fileName = mainDir + "Settings.dat"
resx = 1280
resy = 720
def readSettings():
    import GameLogic
    import bge
    cont = GameLogic.getCurrentController()
    own = cont.owner
    scenes = bge.logic.getSceneList()
    main_scene = [scene for scene in scenes if scene.name=="main"][0]
    own = main_scene.objects['Empty']
    
    with open(fileName) as reading:
        #data = [line.split()[0] for line in reading]
        data = reading.readlines()
        #print lines[25]
        #print lines[29]
        reading.close()

    #own["val"] = int(data[1])
    #own["bright"] = int(data[1])
    own["playerName"] = data[4]
    #own["audio"] = int(data[3])
    own["level"] = data[5]
    #data6 = data[6]
    #data6.split(None, 1)[0]
    #data5 = data5.strip('\n')
    #print(data6)
    own["resx"] = int(data[7])
    own["resy"] = int(data[8])
    own["framerate"] = int(data[10])
    own["profile"] = int(data[12])
    dict = bge.logic.globalDict #Get the global dictionary
    
    dict['resx'] = own["resx"]
    dict['resy'] = own["resy"]   
    level = data[5]
    level = level.split(None, 1)[0]
    level = str(level)
    own["level"] = level
    dict["level"] = level
    print("reading. settings")
    print("Level: ", level)
    recorder_on = int(data[14])
    print("Game play recorder on: ", recorder_on) 
    #dict = bge.logic.globalDict
    dict['recorder_on'] = recorder_on
    own['recorder_on'] = recorder_on
    dict['recorder_on'] = recorder_on 
    character = data[1]
    dict["character"] = str(character.split(None, 1)[0])
    dict["shirt_color_r"] = float(data[16])
    dict["shirt_color_g"] = float(data[17])
    dict["shirt_color_b"] = float(data[18])
    dict["shoe_color_r"] = float(data[20])
    dict["shoe_color_g"] = float(data[21])
    dict["shoe_color_b"] = float(data[22]) 
    dict["deck_color_r"] = float(data[24])
    dict["deck_color_g"] = float(data[25])
    dict["deck_color_b"] = float(data[26]) 
    dict["shirt_logo"] = int(data[28])  
    dict["bc"] = int(data[30])
    dict["BC_BRIGHTNESS"] = float(data[31])
    dict["BC_CONTRAST"] = float(data[32])  
    dict['hdr'] = int(data[34])
    dict['avgL'] = float(data[35])
    dict['HDRamount'] = float(data[36])
    dict['ao'] = int(data[38])
    dict['onlyAO'] = int(data[39])
    dict['aowidth'] = float(data[40])
    dict['aoradius'] = float(data[41])
    dict['dof_on'] = float(data[43])
    dict['sun_strength'] = float(data[45])
    dict['sun_rot_x'] = float(data[46])
    dict['sun_rot_y'] = float(data[47])            
    dict['shadow_on'] = int(data[48])
    dict['ambient_strength'] = float(data[49]) 
    dict['fullscreen_on'] = int(data[51]) 
    dict['bloom_on'] = int(data[53]) 
    dict['cam_height'] = float(data[55]) 
    dict['focal_length'] = int(data[56])        
    dict['cam_min'] = float(data[57])
    dict['cam_max'] = float(data[58])
    dict['music_player'] = int(data[60]) 
    dict['fxaa'] = int(data[62]) 
    dict['FXAA_SPAN_MAX'] = float(data[63])   
    dict['cch_targetHeight'] = float(data[65])         
    dict["trucks_r"] = float(data[67])
    dict["trucks_g"] = float(data[68])
    dict["trucks_b"] = float(data[69])
    dict["wheel1_r"] = float(data[71])
    dict["wheel1_g"] = float(data[72])
    dict["wheel1_b"] = float(data[73])
    dict["wheel2_r"] = float(data[75])
    dict["wheel2_g"] = float(data[76])
    dict["wheel2_b"] = float(data[77])
    dict["wheel3_r"] = float(data[79])
    dict["wheel3_g"] = float(data[80])
    dict["wheel3_b"] = float(data[81])
    dict["wheel4_r"] = float(data[83])
    dict["wheel4_g"] = float(data[84])
    dict["wheel4_b"] = float(data[85])  
    dict["deck_index"] = int(data[87])                        
    
    #print('bc = ', dict['bc'])        
    #dict['music_player_on'] = int(data[16])  
def writeSettings():
    import GameLogic
    import bge
    cont = GameLogic.getCurrentController()
    own = cont.owner
    scenes = bge.logic.getSceneList()
    dict = bge.logic.globalDict
    main_scene = [scene for scene in scenes if scene.name=="main"][0]
    own = main_scene.objects['Empty']
    print("writing called")
    with open(fileName, 'w') as writing:
        writing.write(str("//settings.dat")+"\n")
        writing.write(str(dict["character"])+"\n")
        writing.write(str(own["bright"])+"\n")
        writing.write(str("-\n"))
        writing.write(str("//level (e.g. spine, city, city2)")+"\n")
        writing.write(str(own["level"])+"\n")
        writing.write(str("//resolution")+"\n")
        writing.write(str(dict["resx"])+"\n")
        writing.write(str(dict["resy"])+"\n")
        writing.write(str("//show framerate")+"\n")
        writing.write(str(own["framerate"])+"\n")
        writing.write(str("//show profile")+"\n")
        writing.write(str(own["profile"])+"\n")
        writing.write(str("//Game play recorder off/on (0,1)")+"\n")
        writing.write(str(own["recorder_on"])+"\n")
        writing.write(str("//Shirt color [r,g,b]")+"\n")
        writing.write(str(dict["shirt_color_r"])+"\n") #16
        writing.write(str(dict["shirt_color_g"])+"\n")
        writing.write(str(dict["shirt_color_b"])+"\n")    # 
        writing.write(str("//Shoe color [r,g,b]")+"\n")
        writing.write(str(dict["shoe_color_r"])+"\n") #16
        writing.write(str(dict["shoe_color_g"])+"\n")
        writing.write(str(dict["shoe_color_b"])+"\n")    # 
        writing.write(str("//Deck color [r,g,b]")+"\n")
        writing.write(str(dict["deck_color_r"])+"\n") #24
        writing.write(str(dict["deck_color_g"])+"\n")
        writing.write(str(dict["deck_color_b"])+"\n")    #                           
        writing.write(str("//Shirt Logo")+"\n")
        writing.write(str(dict["shirt_logo"])+"\n") #24
        writing.write(str("//Brightness/Contrast: On, brightness, contrast")+"\n")
        writing.write(str(dict["bc"])+"\n")
        writing.write(str(dict["BC_BRIGHTNESS"])+"\n")
        writing.write(str(dict["BC_CONTRAST"])+"\n")
        writing.write(str("//HDR: On, avgL, amount")+"\n")
        writing.write(str(dict["hdr"])+"\n")
        writing.write(str(dict["avgL"])+"\n")
        writing.write(str(dict["HDRamount"])+"\n") 
        writing.write(str("//AO: On, only, width, radius")+"\n")
        writing.write(str(dict["ao"])+"\n")
        writing.write(str(dict["onlyAO"])+"\n")
        writing.write(str(dict["aowidth"])+"\n")                
        writing.write(str(dict["aoradius"])+"\n") 
        writing.write(str("//dof on")+"\n")
        writing.write(str(dict["dof_on"])+"\n") 
        writing.write(str("//sun settings: strength, rotx, roty, shadow on, ambient strength")+"\n")
        writing.write(str(dict["sun_strength"])+"\n")                           
        writing.write(str(dict["sun_rot_x"])+"\n")                           
        writing.write(str(dict["sun_rot_y"])+"\n")                           
        writing.write(str(dict["shadow_on"])+"\n")                                     
        writing.write(str(dict["ambient_strength"])+"\n") 
        writing.write(str("//fullscreen on (0,1)")+"\n")
        writing.write(str(dict["fullscreen_on"])+"\n") 
        writing.write(str("//bloom on (0,1)")+"\n")
        writing.write(str(dict["bloom_on"])+"\n")  
        writing.write(str("//cam")+"\n")
        writing.write(str(dict["cam_height"])+"\n") 
        writing.write(str(dict["focal_length"])+"\n") 
        writing.write(str(dict["cam_min"])+"\n")                 
        writing.write(str(dict["cam_max"])+"\n") 
        writing.write(str("//music player on / off")+"\n")
        writing.write(str(dict["music_player"])+"\n")
        writing.write(str("//fxaa")+"\n")
        writing.write(str(dict["fxaa"])+"\n") 
        writing.write(str(dict["FXAA_SPAN_MAX"])+"\n")    
        writing.write(str("//camera target height")+"\n")
        writing.write(str(dict["cch_targetHeight"] )+"\n")   
        writing.write(str("//trucks color [r,g,b]")+"\n")
        writing.write(str(dict["trucks_r"])+"\n")
        writing.write(str(dict["trucks_g"])+"\n")
        writing.write(str(dict["trucks_b"])+"\n") 
        writing.write(str("//wheel1 color [r,g,b]")+"\n")
        writing.write(str(dict["wheel1_r"])+"\n")
        writing.write(str(dict["wheel1_g"])+"\n")
        writing.write(str(dict["wheel1_b"])+"\n") 
        writing.write(str("//wheel2 color [r,g,b]")+"\n")
        writing.write(str(dict["wheel2_r"])+"\n")
        writing.write(str(dict["wheel2_g"])+"\n")
        writing.write(str(dict["wheel2_b"])+"\n") 
        writing.write(str("//wheel3 color [r,g,b]")+"\n")
        writing.write(str(dict["wheel3_r"])+"\n")
        writing.write(str(dict["wheel3_g"])+"\n")
        writing.write(str(dict["wheel3_b"])+"\n") 
        writing.write(str("//wheel4 color [r,g,b]")+"\n")
        writing.write(str(dict["wheel4_r"])+"\n")
        writing.write(str(dict["wheel4_g"])+"\n")
        writing.write(str(dict["wheel4_b"])+"\n")                                          
        writing.write(str("//deck index")+"\n")
        writing.write(str(dict["deck_index"])+"\n")        
        
        #dict['ccH_targetHeight']     
        
        #writing.write(str(own["framerate"])+"\n")
        print("writing settings")
        writing.close()
       
       
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 = dict["resx"]
    resy = 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
    #readSettings()

    dict = bge.logic.globalDict #Get the global dictionary
    
#    resx = dict["resx"]
#    resy = dict["resy"]
#    #dict['resx'] = resx
#    #dict['resy'] = 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)
    #objList = scene.objects
    if own["framerate"] == 1:    
        bge.render.showFramerate(1)
    if own["profile"] ==1:    
        bge.render.showProfile(1)   
    #cont = GameLogic.getCurrentController() 
    #own = cont.owner
    #cont = own2.controllers['loadLevel']
    act = own2.actuators["level"]
    #cont.activate(own.actuators["loading"]) 
    #level = own["level"]
    
    dict = bge.logic.globalDict
    level = dict['level']
    #dict['level'] = level
    act.scene = level 
    cont.activate(own2.actuators["level"]) 
    #cont.activate(own2.actuators["stance"])
    dict['npause'] = False 
    
def timer():
    import GameLogic
    import bge    

    dict = bge.logic.globalDict    
    
    if dict['reload_timer'] == 1:
        #restart
        # get current scene
        scene = bge.logic.getCurrentScene()

        # restart the current scene
        #scene.restart()        
            
    if dict['reload_timer'] > 0:
       dict['reload_timer'] -= 1         
    #print(dict['reload_timer'])
    #own['reload_timer'] = dict['reload_timer']   