/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

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.Collision
import org.drools.games.pong.CollisionType

import java.lang.Math

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" agenda-group "Collision" when
    not Collision()
    ball : Ball( y <=  pconf.boundedTop )
    or
    ball : Ball( y + width >=  pconf.boundedBottom )
then
    modify( ball ) { dy = 0 - ball.dy };
    insert( new Collision( 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" agenda-group "Collision" when
    eval( bat.y < ball.y+20 &&
          bat.y > ball.y+10 )
then
    modify( ball ) { dy = -1, speed = 6 };
    insert( new Collision( bat.player, CollisionType.BatTop ) );
end

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

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

rule "Ball Bat Collision Middle" extends "Abstract Ball Bat Collision" agenda-group "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" agenda-group "Collision"  when
    eval( bat.y + 42 < ball.y+(ball.width/2) &&
          bat.y + 80 >= ball.y+(ball.width/2) )
    if ( ball.dy > 0 ) do[increase]
    if ( ball.dy < 0 && ball.speed > 1 ) do[decrease]
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 ) );
then[increase]
    modify( ball ) { speed = ball.speed + 1 };
then[decrease]
    modify( ball ) { speed = ball.speed - 1 };
end


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