package org.drools.games.pong

import org.drools.games.pong.Ball
import org.drools.games.pong.Bat
import org.drools.games.pong.Player
import org.drools.games.pong.PlayerId
import org.drools.games.pong.PongGame
import org.drools.games.pong.PointWin
import org.drools.games.pong.KeyPressed
import org.drools.games.pong.Collision
import org.drools.games.pong.CollisionType

import java.lang.Math

import org.drools.runtime.StatefulKnowledgeSession
import org.drools.logger.KnowledgeRuntimeLogger

import java.awt.Color
import java.awt.Graphics
import java.awt.event.KeyEvent

import java.util.List

global PongConfiguration pconf

dialect "mvel"  

rule "Ball Side Line Collision" when
    not Collision()
    ball : Ball( y <=  pconf.padding + pconf.sideLineWidth )
    or
    ball : Ball( y + width >=  pconf.tableHeight - pconf.padding - pconf.sideLineWidth )
then   
    modify( ball ) { dy = 0 - ball.dy };
    insert( new Collision( (Player) null, CollisionType.SideLineTop ) );
end

rule "Abstract Ball Bat Collision" when
    ball : Ball() @watch( x, y )
    not Collision()
    bat : Bat( x + width >= ball.x, playerId == PlayerId.PlayerOne )
          or
    bat : Bat( x < ball.x + ball.width, playerId == PlayerId.PlayerTwo )
then
end

rule "Ball Bat Collision Top"  extends "Abstract Ball Bat Collision" when    
    eval( bat.y < ball.y+20 && 
          bat.y > ball.y+10 )
then 
    modify( ball ) { dy = -6 };
    insert( new Collision( bat.player, CollisionType.BatTop ) );    
end

rule "Ball Bat Collision Bottom"  extends "Abstract Ball Bat Collision" when    
    eval( bat.y+80 < ball.y+10 && 
          bat.y+80 > ball.y )
then 
    modify( ball ) { dy = 6 };
    insert( new Collision( bat.player, CollisionType.BatBottom ) );    
end

rule "Ball Bat Collision Upper"  extends "Abstract Ball Bat Collision" when    
    eval( bat.y <= ball.y+(ball.width/2) && 
          bat.y + 38 > ball.y+(ball.width/2) )
then
    m = Math.ceil(((bat.y + 38) - (ball.y + (ball.width/2)))/10.0 ); 
    modify( ball ) { dx = 0 - ball.dx, dy = 0 - m };
    insert( new Collision( bat.player, CollisionType.BatUpper ) );    
end

rule "Ball Bat Collision Middle" extends "Abstract Ball Bat Collision" when    
    eval( bat.y + 38 <= ball.y+(ball.width/2) &&
          bat.y + 42 >= ball.y+(ball.width/2) )
then
    modify( ball ) { dx = 0 - ball.dx, dy = 0 };
    insert( new Collision( bat.player, CollisionType.BatMiddle ) );    
end

rule "Ball Bat Collision Lower" extends "Abstract Ball Bat Collision" when    
    eval( bat.y + 42 < ball.y+(ball.width/2) &&
          bat.y + 80 >= ball.y+(ball.width/2) )
then
    m = Math.ceil( ( (ball.y + (ball.width/2)) - (bat.y+42))/10.0 ); 
    modify( ball ) { dx = 0 - ball.dx, dy = m };
    insert( new Collision( bat.player, CollisionType.BatLower ) );    
end

/**
 * This retracts the Collision once we've moved into a rectangle where we know no further collision is possible
 */
rule "Retract Collision" when
    c    : Collision()
    ball : Ball() @watch( x, y )
    // use eval to avoid wasted alpha nodes evaluations
    eval( ball.x > pconf.padding  + pconf.batWidth &&
          ball.x < pconf.tableWidth - pconf.padding  - pconf.batWidth - ball.width &&
          ball.y > pconf.padding  +  pconf.sideLineWidth + pconf.batWidth &&
          ball.y < pconf.tableHeight - pconf.padding -  pconf.sideLineWidth - ball.width ) 
then
    retract( c );
end

rule "Increase Ball Speed"  when
    c : Collision( )
    ball : Ball( speed-3 >= 20 ) @watch( !speed) // but make sure it never goes below 20
then
    modify( ball ) { speed = ball.speed - 3 };
end