import pygame
from pygame.locals import *

from obj import Object, Rect
from locals import *
#from map import Tile, get_screen_pos, WALLWIDTH, WALLHEIGHT
import map

class RenderWall:
    def __init__(self, drawpos, surface, drawrect):
        self.drawpos = drawpos
        self.surface = surface
        self.drawrect = drawrect

    def draw(self, screen, offset):
        realdrawpos = [self.drawpos[0] + offset[0], self.drawpos[1] + offset[1]]
        screen.blit(self.surface, realdrawpos, self.drawrect)

class RenderObject:
    def __init__(self, rect, obj):
        self.rect = rect
        self.obj = obj

    def draw(self, screen, offset):
        self.obj.draw(screen, offset)

    def isbehind(self, ro):
        #if self.rect.right + self.rect.bottom < ro.rect.right + ro.rect.bottom:
        #    return True
        #return False
        if self.rect.right < ro.rect.left:
            return True
        if self.rect.bottom < ro.rect.top:
            return True
        return False
        #elif self.rect.right + self.rect.bottom < ro.rect.right + ro.rect.bottom:
        #    return True
        #return False
        if self.rect.left > ro.rect.right:
            return False
        if self.rect.top > ro.rect.bottom:
            return False

# The rest are overlap situations... the render order is not obvious then,
# but I'm trying to come up with sane values that look correct
        #elif self.rect.right < ro.rect.right and self.rect.bottom < ro.rect.bottom:
        #    return True
        #elif self.rect.right > ro.rect.right and self.rect.bottom > ro.rect.bottom:
        #    return True
# If the rectangles overlap like this,
# it's not really obvious which one is on the bottom
#  /\/\ 
# / /\ \
# \ \/ /
#  \/\/
        #if ro.rect.height == 0:
        #print "self:", self.rect.left, self.rect.top, self.rect.right, self.rect.bottom
        #print "ro:", ro.rect.left, ro.rect.top, ro.rect.right, ro.rect.bottom
        return False

class Renderer:
    def __init__(self):
        self.renderlist = []

    def add_walls(self, tile, pos):
        if tile.leftwall and tile.leftwall < 8:
            rect = Rect(pos[0], pos[1], 0.0, 1.0)
            drawpos = map.get_screen_pos((0,0), pos) # the 0,0 is the offset
            drawpos[0] -= WALLWIDTH
            drawpos[1] -= WALLHEIGHT - TILEHEIGHT/2
            if (Variables.psychomode):
               surface = map.Tile.wallsets[5]
            else:
               surface = map.Tile.wallsets[tile.tileset]
            drawrect = ((tile.leftwall - 1) * WALLWIDTH, 0, WALLWIDTH, WALLHEIGHT)
            rw = RenderWall(drawpos, surface, drawrect)
            ro = RenderObject(rect, rw)
            #self.add_renderobject(ro)
            self.renderlist.append(ro)
        if tile.rightwall and tile.rightwall < 8:
            rect = Rect(pos[0], pos[1], 1.0, 0.0)
            drawpos = map.get_screen_pos((0,0), pos)
            drawpos[1] -= WALLHEIGHT - TILEHEIGHT/2
            if (Variables.psychomode):
               surface = map.Tile.wallsets[5]
            else:
               surface = map.Tile.wallsets[tile.tileset]
            drawrect = ((tile.rightwall + 7 - 1) * WALLWIDTH, 0, WALLWIDTH, WALLHEIGHT)
            rw = RenderWall(drawpos, surface, drawrect)
            ro = RenderObject(rect, rw)
            #self.add_renderobject(ro)
            self.renderlist.append(ro)

    def add_object(self, obj):
        ro = RenderObject(obj.get_rect(), obj)
        self.add_renderobject(ro)

    def add_renderobject(self, ro):
        if not ro.isbehind(self.renderlist[-1]):
            self.renderlist.append(ro)
        for i in range(len(self.renderlist) - 2 , -1, -1):
            behind = ro.isbehind(self.renderlist[i])
            if behind == False:
                #if ro.isbehind(self.renderlist[i]):
                #if i != len(self.renderlist) - 1:
                #    if not ro.isbehind(self.renderlist[i+1]):
                #        temp = self.renderlist[i]
                #        self.renderlist[i] = self.renderlist[i+1]
                #        self.renderlist[i+1] = self.renderlist[i]
                #        continue
                self.renderlist.insert(i+1, ro)
                return

        self.renderlist.insert(0, ro)

    def draw(self, screen, offset):
        for ro in self.renderlist:
            ro.draw(screen, offset)

