import inc

from bubble import *
import bubble
import behavior
import collisionDetection

import logging
import logging.handlers
import math
import defensive
import obj

log = defensive.Logging()


if __name__ == '__main__':
    #DatagramHandler
    #ch = logging.handlers.SocketHandler('localhost',logging.handlers.DEFAULT_TCP_LOGGING_PORT)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    log.logger.addHandler(ch)
    log.logger.setLevel(logging.DEBUG)
    #behavior.log.logger.addHandler(ch)
    #behavior.log.logger.setLevel(logging.DEBUG)
    #collisionDetection.log.logger.addHandler(ch)
    #collisionDetection.log.logger.setLevel(logging.DEBUG)
    #bubble.log.logger.addHandler(ch)
    #bubble.log.logger.setLevel(logging.DEBUG)

    import pygBubble
    pig = pygBubble.PygBubble(800,600,60,'roid')
    control = []

    ############

    @log.debug
    def playableBehavior(player,context):
        if (pygBubble.K_LEFT in control):
            player = player.push(-0.2,0)
        if (pygBubble.K_RIGHT in control):
            player = player.push(0.2,0)
        if (pygBubble.K_UP in control):
            player = player.push(0,-0.2)
        if (pygBubble.K_DOWN in control):
            player = player.push(0,0.2)
        return player

    @log.debug
    def shootingBehavior(player,context):
        if (pygBubble.K_SPACE in control):
            link = player.link
            x,y = player.position
            dirx,diry= player.direction
            nb = bullet.translate(x,y)
            nb = nb.push(dirx,diry)
            player = player.updateLink([nb]+list(link))
        return player

    @log.debug
    def spaceshipBehavior(player,context):
        print control
        if (pygBubble.K_LEFT in control):
            player = player.rotate(-0.1)
        if (pygBubble.K_RIGHT in control):
            player = player.rotate(0.1)
        if (pygBubble.K_UP in control):
            dirx,diry = player.dx,player.dy
            player = player.push(dirx*0.7,diry*0.7)
        return player

    @log.debug
    def walkingBehavior(player,context):
        rad = player.getRadian()
        ray = 0
        if (pygBubble.K_a in control):
            rad += math.pi*0.4
            ray = 0.5
        if (pygBubble.K_d in control):
            rad -= math.pi*0.4
            ray = 0.5
        if (pygBubble.K_w in control):
            pass
        if (pygBubble.K_s in control):
            pass
        s = math.sin(rad)
        c = math.cos(rad)
        vx,vy = player.forceVector
        if vx==0 and vy==0:
            return player.push(s*ray,c*ray)
        return player

    bullet = {}
    rocket = {}
    universe = {}
    import json
    with open('./data/planet.json') as data:
        for bubble in bubbleFromStruct(json.load(data)):
            universe=bubble
    with open('./data/bullet.json') as data:
        for bubble in bubbleFromStruct(json.load(data)):
            bullet=bubble
   

    behaviors = {
        'playableBehavior':[spaceshipBehavior],
        'walkingBehavior':[walkingBehavior],
        'shootingBehavior':[shootingBehavior]
    }
    behaviors.update(behavior.suite)
    
    with open('./data/rocket.json') as data:
        for bubble in bubbleFromStruct(json.load(data)):
            rocket=bubble
    ############
    #print rocket
    #print universe
    rocket = rocket.against(universe,behaviors,collisionDetection.suite)
    control = pig.getControlState()
    while(not pygBubble.QUIT in control):
        pig.clearScr()
        ##################
        pig.drawBubble(rocket)
        pig.drawBubble(universe)

        rocket = rocket.against(universe,behaviors,collisionDetection.suite)
        #################
        control = pig.getControlState()
        pig.wait()
