/**
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 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.jboss.webbeans.environment.se.example.roadmap.renderer;

import org.jboss.webbeans.environment.se.example.roadmapmodel.Road;
import org.jboss.webbeans.environment.se.example.roadmapmodel.RoadMap;
import org.jboss.webbeans.environment.se.example.roadmapmodel.RoadMapConstructor;
import org.jboss.webbeans.environment.se.processing.frame.FrameControl;
import org.jboss.webbeans.log.Log;
import org.jboss.webbeans.log.Logging;

import processing.core.PApplet;

import javax.inject.Current;
import org.jboss.webbeans.environment.se.example.roadmapmodel.Intersection;
import org.jboss.webbeans.environment.se.example.roadmapmodel.Orientation;

/**
 *
 * @author Peter Royle
 */
public class RoadMapPApplet
        extends PApplet
{

    public static final int ROAD_WIDTH = 8;
    private static final int INTERSECTION_HEIGHT = 10;
    private @Current
    RoadMapConstructor roadMapConstructor;
    private @Current
    RoadMap roadMap;
    private @Current
    FrameControl frameControl;
    Log log = Logging.getLog( RoadMapPApplet.class );

    @Override
    public void setup()
    {
        size( roadMap.getWidth(),
                roadMap.getHeight() );
        background( 0 );
        stroke( 100);
        fill( 100);
        //noLoop();
        log.debug( "setup found " + roadMap.getRoads().size() + " roads" );
    }

    @Override
    public void draw()
    {
        drawFrame();
        frameControl.endFrame();
    }

    private void drawFrame()
    {
//        if (mousePressed)
//        {
//            line( mouseX, mouseY, pmouseX, pmouseY );
//        }
//        log.debug( "DRAWING FRAME" );
//        log.debug( "setup found " + roadMap.getRoads().size() + " roads" );

        for (Road road : roadMap.getRoads())
        {
//            line( road.getX1(),
//                    road.getY1(),
//                    road.getX2(),
//                    road.getY2() );

            if (Orientation.HORIZONTAL.equals( road.getOrientation() ))
            {
                rect( road.getX1(), road.getY1() - (ROAD_WIDTH / 2), road.getLength(), ROAD_WIDTH );
            } else
            {
                rect( road.getX1() - (ROAD_WIDTH / 2), road.getY1(), ROAD_WIDTH, road.getLength() );
            }

        }

        for (Intersection inter : roadMap.getIntersections())
        {
            ellipse( inter.getX(), inter.getY(), INTERSECTION_HEIGHT, INTERSECTION_HEIGHT );
        }
    }
}
