import os
import sys

#"pip install pygame-ce"
with open( os.devnull, "w" ) as f:
	oldstdout=sys.stdout; sys.stdout=f; import pygame; sys.stdout=oldstdout

#"pip install PyOpenGL"
from OpenGL.GL import *

from ..game import camera as mod_camera

from ..glbase import gl_font
from ..glbase import gl_misc
from ..glbase import gl_program
from ..glbase import gl_texture

from .. import config
from ..math_util import *

from . import driver
from . import render_advanced



class Textures:
	def __init__( self:Self ):
		#Walls
		self.wall_hedge = Textures.load_from_path( config.MAIN_DIR+"data/texture/wall_hedge.jpg" )
		self.wall_metal = Textures.load_from_path( config.MAIN_DIR+"data/texture/wall_metal.jpg" )

		#Tiles
		tiles_surf = pygame.image.load( config.MAIN_DIR+"data/texture/tiles.png" )
		floor_surf = tiles_surf.subsurface(( 0,0, 128,128 ))
		self.tiles      = Textures.load_from_surf(tiles_surf)
		self.tile_floor = Textures.load_from_surf(floor_surf)

		#"People"
		self.marble_evil = Textures.load_from_path( config.MAIN_DIR+"data/texture/marble_evil.jpg" )
		self.marble_self = Textures.load_from_path( config.MAIN_DIR+"data/texture/marble_self.jpg" )

		#Tile Objects

		#Misc. Objects

	@staticmethod
	def load_from_path( path:str, gl_fmt=GL_RGB8 ):
		surf = pygame.image.load(path)
		return Textures.load_from_surf( surf, gl_fmt )
	@staticmethod
	def load_from_surf( surf:pygame.Surface, gl_fmt=GL_RGB8 ):
		tex = gl_texture.Texture2D.from_surf( surf, gl_fmt )
		tex.set_filters( 2, 0 )
		tex.regen_mips()
		if config.texture_anisotropic > 1.0:
			config.texture_anisotropic = tex.set_anis(config.texture_anisotropic)
		return tex



camera = mod_camera.GameCamera()

textures:Textures|None = None

font:gl_font.Font|None = None



def render_hud( fps:float ):
	res = config.window_res

	def text_black( text:str, pos:tuple[float,float] ):
		font.render( text, pos, ( 0.0,0.0,0.0, 1.0 ) )
	def text_white( text:str, pos:tuple[float,float] ):
		font.render( text, pos, ( 1.0,1.0,1.0, 1.0 ) )

	text_black( f"Score:", (25,res[1]-25) )
	text_black( str(rndint(driver.points)), (100,res[1]-25) )
	text_black( f"Lives:", (25,res[1]-50) )

	text_black( f"Time: {rndint(max(driver.level.time_left,0.0))} s", (25,res[1]-75) )

	text_black( f"\"{driver.level.name}\"", (res[0]//2-100,res[1]-25) )

	if config.render_fps:
		text_black( str(rndint(driver.lives)), (100,res[1]-50) )
		text_black( f"FPS: {rndint(fps)}", (res[0]-100,res[1]-25) )

	def _msg( text:str, rx:int, dx:int,dy:int, with_bg=True ):
		if with_bg:
			if config.render_advanced:
				render_advanced.renderer.draw_quad(
					(res[0]//2,res[1]//2), rx,50, ( 0.0,0.0,0.0, 0.7 )
				)
			else:
				gl_program.Program.use(None)

				glDisable(GL_TEXTURE_2D)
				glColor4f( 0.0,0.0,0.0, 0.7 )
				gl_misc.render_gl2_quad( (res[0]//2,res[1]//2), rx,50 )

				glEnable(GL_TEXTURE_2D)
		text_white( text, (res[0]//2+dx,res[1]//2-5+dy) )

	if   driver.mode == driver.MODE_PAUSED:
		_msg( "Paused", 100, -30,0 )
	elif driver.mode == driver.MODE_LVL_SUCCESS:
		_msg( "Level Complete", 150, -64,20 )
		_msg( f"(time bonus: +{driver.time_bonus})", 150, -100,-20, False )
	elif driver.mode == driver.MODE_LVL_EXPIRED:
		_msg( "Time Expired", 100, -54,0 )
	elif driver.mode == driver.MODE_GAME_LOST:
		_msg( "Game Over", 100, -51,0 )
	elif driver.mode == driver.MODE_GAME_COMPLETED:
		_msg( "Victory", 100, -30,0 )
