import globalVars

from math import *
from util import *
from constants import *

texWidth = SWIDTH * 1.0/maxTexSize
texHeight = SHEIGHT * 1.0/maxTexSize

class Light:
    def __init__(self, origin, direction, base_width=10, fov=30, length=100, color=[1,1,1,1]):
        self.origin = origin
        self.direction = direction
        self.base_width = base_width
        self.fov = fov
        self.length = length
        self.color = color
        self.yMin, self.yMax = 0,0 
        self.xMin, self.xMax = 0,0
        if len(self.color) != 4:
            self.color.append(1)
    
    def setPos(self,x,y):
        self.origin = (x,y)
        
    def setDirection(self,direction):
        self.direction = direction
        
    def rotate(self,x,y,angle):
        xr = x * cos(angle) - y * sin(angle)
        yr = x * sin(angle) + y * cos(angle)
        
        return xr, yr
    
    def getCoords(self):

        x1 = - self.base_width
	y1 = 0.0
	        
	x1, y1 = self.rotate(x1,y1, radians(self.direction))
	
	x1 += self.origin[0]
	y1 += self.origin[1]

	        
	x2 = self.base_width
	y2 = 0.0
	        
	x2, y2 = self.rotate(x2,y2, radians(self.direction))

	x2 += self.origin[0]
	y2 += self.origin[1]
	        
	x3 = self.base_width + self.fov * 1.0
	y3 = self.length *1.0
	        
	x3, y3 = self.rotate(x3,y3, radians(self.direction))

	x3 += self.origin[0]
	y3 += self.origin[1]
	        
	x4 = - self.base_width - self.fov *1.0
	y4 = self.length *1.0
	        
        x4, y4 = self.rotate(x4,y4, radians(self.direction))
        
        x4 += self.origin[0]
	y4 += self.origin[1]
        
        return x1,y1,x2,y2,x3,y3,x4,y4
    
    def getOutside(self, coords):
    
        minItem = coords[0]
        maxItem = coords[0]
        for item in coords:
            if item < minItem:
                minItem = item
            if item > maxItem:
                maxItem = item
            
        return minItem, maxItem

    
    def draw(self):
        x1,y1,x2,y2,x3,y3,x4,y4 = self.getCoords()

        self.xMin, self.xMax = self.getOutside((x1,x2,x3,x4))
        self.yMin, self.yMax = self.getOutside((y1, y2, y3, y4))
        
        self.xEnd = 0.0
        self.yEnd = self.length *1.0
        self.xEnd, self.yEnd = self.rotate(self.xEnd,self.yEnd, radians(self.direction))
        self.xEnd += self.origin[0]
        self.yEnd += self.origin[1]

        setTex(globalVars.bgtex)    
        glLoadIdentity()
	
	self.color[3] = 1
	glBegin(GL_QUADS)
	
	glColor4fv(self.color);
	glTexCoord2f(x1 / SWIDTH*texWidth, (1.0 - y1 / SHEIGHT)*texHeight ); glVertex2f(x1, y1)    # Bottom Left Of The Texture and Quad
	glColor4fv(self.color);
	glTexCoord2f(x2/ SWIDTH*texWidth , (1.0 - y2 / SHEIGHT)*texHeight ); glVertex2f(x2, y2)    # Bottom Right Of The Texture and Quad
	self.color[3] = 0
	glColor4fv(self.color);
	glTexCoord2f(x3/ SWIDTH*texWidth , (1.0 - y3 / SHEIGHT)*texHeight ); glVertex2f( x3, y3)    # Top Right Of The Texture and Quad
	glColor4fv(self.color);
	glTexCoord2f(x4/ SWIDTH*texWidth , (1.0 - y4 / SHEIGHT)*texHeight ); glVertex2f(x4, y4)    # Top Left Of The Texture and Quad
	
        glEnd()