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

global PongConfiguration pconf

dialect "mvel"  


rule "Move Ball" timer(expr: ball.speed, ball.speed) when
    ui : PongUI( ready == true ) 
    td : TableDirty( )    
    ball : Ball() @watch(speed)    
then    
    tableG = ui.getTablePanel().getTableG();

    // restore current background gfx        
    copyX = 0 - (pconf.tableWidth - ball.x);
    copyY = ball.y;
    tableG.copyArea( pconf.tableWidth, 0,
                     ball.width, ball.width,
                     copyX,copyY );    
    
    modify( ball ) { x += ball.dx, y += ball.dy };
    
    // draw ball in new position, while preserving it's background
    copyX = pconf.tableWidth - ball.x;
    copyY = 0 - ball.y;         
    tableG.copyArea( ball.x, ball.y,
                     ball.width, ball.width,
                     copyX, copyY );       
                   	   
    tableG.setColor( Color.WHITE ); // background                      
    tableG.fillOval( ball.x, ball.y, ball.width, ball.width );
    modify( td ) { dirty = true };    
           
end

rule "Move Bat Up" timer(expr: bat.speed, bat.speed) no-loop when
    ui : PongUI(  ready == true )
    ( KeyPressed( keyText == "A" ) and bat : Bat( playerId == PlayerId.PlayerOne, y > pconf.padding + pconf.sideLineWidth )
      or
      KeyPressed( keyText == "K" ) and bat : Bat( playerId == PlayerId.PlayerTwo, y > pconf.padding + pconf.sideLineWidth ) )	
    td : TableDirty( )      
then    
    tableG = ui.getTablePanel().getTableG();
    tableG.setColor( Color.BLACK ); // background
    tableG.fillRect( bat.x, bat.y, bat.width, bat.height );
     
    modify( bat ) { y = bat.y - 2 };
    tableG.setColor( Color.WHITE ); // background
    tableG.fillRect( bat.x, bat.y, bat.width, bat.height );  
    modify( td ) { dirty = true };
end

rule "Move Bat Down" timer(expr: bat.speed, bat.speed) no-loop when
    ui : PongUI(  ready == true )
    ( KeyPressed( keyText == "Z" ) and bat : Bat( playerId == PlayerId.PlayerOne, y < pconf.tableHeight - pconf.padding - pconf.sideLineWidth - pconf.batHeight )
      or
      KeyPressed( keyText == "M" ) and bat : Bat( playerId == PlayerId.PlayerTwo, y < pconf.tableHeight - pconf.padding - pconf.sideLineWidth - pconf.batHeight ) )
    td : TableDirty( )      
then
    tableG = ui.getTablePanel().getTableG();
    tableG.setColor( Color.BLACK ); // background
    tableG.fillRect( bat.x, bat.y, bat.width, bat.height );
    
    modify( bat ) { y = bat.y + 2 };
    tableG.setColor( Color.WHITE ); // background
    tableG.fillRect( bat.x, bat.y, bat.width, bat.height );  
    modify( td ) { dirty = true };
end