/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include "RenderArea.h"
#include "Client.h"

#include <QPainter>

RenderArea::RenderArea(QWidget *parent, const Client &client)
	: QWidget(parent), mClient(client)
{
	shape = Polygon;
	antialiased = false;
	transformed = false;
	pixmap.load(":/images/qt-logo.png");
	
	setBackgroundRole(QPalette::Base);
	setAutoFillBackground(true);
	
}

QSize RenderArea::minimumSizeHint() const
{
    return QSize(100, 500);
}

QSize RenderArea::sizeHint() const
{
    return QSize(400, 500);
}

void RenderArea::setShape(Shape shape)
{
    this->shape = shape;
    update();
}

void RenderArea::setPen(const QPen &pen)
{
    this->pen = pen;
    update();
}

void RenderArea::setBrush(const QBrush &brush)
{
    this->brush = brush;
    update();
}

void RenderArea::setAntialiased(bool antialiased)
{
    this->antialiased = antialiased;
    update();
}

void RenderArea::setTransformed(bool transformed)
{
    this->transformed = transformed;
    update();
}

void RenderArea::paintEvent(QPaintEvent * /* event */)
{
	static const QPoint points[4] = {
		QPoint(-10, -10),
		QPoint(-10, 10),
		QPoint(10, 10),
		QPoint(10, -10)
	};
	
	QRect rect(0, 0, 80, 60);
	
	QPainter painter(this);
	painter.setPen(pen);
	painter.setBrush(brush);
	painter.setRenderHint(QPainter::Antialiasing, true);
	//painter.setRenderHint(QPainter::Antialiasing, false);
	
	QPen pen( Qt::black );
	
	painter.save();
	painter.translate(width()/2, height()/2);
	//painter.rotate(60.0);

	const float scale = height()/200.0f;
	painter.scale(scale, scale);
	// now width of the width corresponds to 200 meters!
	if (1) {
		painter.translate(mClient.mPosition.x, -mClient.mPosition.y);
		
		
		QPointF points[4];
		Vector3 a = mClient.mPosition;
		Vector3 b = mClient.mPosition + 5.0f*mClient.mOrientation;
		
		points[0] = QPointF(-a.x, a.y);
		points[1] = QPointF(-b.x, b.y);
		
		pen.setWidth( 1 ); // <<<<<---   This takes no effect
		pen.setColor(Qt::black);
		painter.setPen( pen );
		painter.drawLine(points[0], points[1]);
		
		// circle of 50 m radius
		pen.setColor(Qt::gray);
		painter.setPen( pen );
		painter.drawEllipse(points[0], 50, 50);
		
		pen.setWidth( 3 ); 
		pen.setColor(Qt::red);
		painter.setPen( pen );
		painter.drawPoints(points, 1);

		
		{
			// draw the road
			// current lane
			QPointF points[2*TrafficInfoData::maxNodeCount];
			
			for (int i=0; i < 2*TrafficInfoData::maxNodeCount; i++) {
				Vector3 pos = mClient.mTrafficInfoData.surroundingNodes[i].position;
				points[i] = QPointF(-pos.x, pos.y);
			}

			pen.setWidth( 1 ); // <<<<<---   This takes no effect
			pen.setColor(Qt::green);
			painter.setPen( pen );
			painter.drawPolyline(points, 2*TrafficInfoData::maxNodeCount);

			pen.setWidth( 2 ); 
			pen.setColor(Qt::black);
			painter.setPen( pen );
			painter.drawPoints(points, 2*TrafficInfoData::maxNodeCount);

			// draw the speed limits
			QFont font = painter.font();
			font.setPixelSize(5);
			painter.setFont(font);
			pen.setWidth( 1 ); 

			for (int i=0; i < 2*TrafficInfoData::maxNodeCount; i++) {
				QPointF a = QPointF(points[i].x()+4, points[i].y());
				QString s;
				
				s.sprintf("%3.0f",
									mClient.mTrafficInfoData.surroundingNodes[i].maxSpeed
									);

				pen.setColor(Qt::black);
				painter.setPen( pen );
				painter.drawText(a, s);
				
				pen.setColor(Qt::red);
				painter.setPen( pen );
				painter.drawEllipse(QPointF(points[i].x()+9, points[i].y()-2), 5, 5);

			}
			
			font.setPixelSize(3);
			painter.setFont(font);
			pen.setWidth( 1 ); 

			for (int i=0; i < 2*TrafficInfoData::maxNodeCount; i++) {
				QPointF a = QPointF(points[i].x()+15, points[i].y());
				QString s;
				
				s.sprintf("%3.0f %3.0f",
									mClient.mTrafficInfoData.surroundingNodes[i].vdop,
									mClient.mTrafficInfoData.surroundingNodes[i].vprof
									);

				pen.setColor(Qt::black);
				painter.setPen( pen );
				painter.drawText(a, s);
				
			}

			
			if (1) {
				// adjanced lane
				for (int i=0; i < 2*TrafficInfoData::maxNodeCount; i++) {
					Vector3 pos =
						mClient.mTrafficInfoData.surroundingNodes[2*TrafficInfoData::maxNodeCount + i].position;
					points[i] = QPointF(-pos.x, pos.y);
				}
				
				pen.setWidth( 1 ); // <<<<<---   This takes no effect
				pen.setColor(Qt::blue);
				painter.setPen( pen );
				painter.drawPolyline(points, 2*TrafficInfoData::maxNodeCount);
				
				pen.setWidth( 2 ); 
				pen.setColor(Qt::black);
				painter.setPen( pen );
				painter.drawPoints(points, 2*TrafficInfoData::maxNodeCount);
			}
		}
		{
			// draw the cars
			QPointF points[TrafficInfoData::maxVehicleCount];
			int cars = min(mClient.mTrafficInfoData.vehicleCount, TrafficInfoData::maxVehicleCount);
			for (int i=0; i < cars; i++) {
				Vector3 pos = mClient.mTrafficInfoData.surroundingVehicles[i].position;
				points[i] = QPointF(-pos.x, pos.y);
			}
			pen.setWidth( 3 ); 
			pen.setColor(Qt::darkGray);
			painter.setPen( pen );
			painter.drawPoints(points, cars);

			// draw car distances
			QFont font = painter.font();
			font.setPixelSize(5);
			painter.setFont(font);
			for (int i=0; i < cars; i++) {
				Vector3 pos = mClient.mTrafficInfoData.surroundingVehicles[i].position;
				QPointF a = QPointF(-pos.x+1, pos.y);
				QString s;
				s.sprintf("d=%3.1fm,s=%2.1fkm/h,rs=%2.1fkm/h",
									//									mClient.mTrafficInfoData.surroundingVehicles[i].relativeSpeed*3.6f,
									mClient.mTrafficInfoData.surroundingVehicles[i].distance,
									mClient.mTrafficInfoData.surroundingVehicles[i].velocity.Length()*3.6f,
									mClient.mTrafficInfoData.surroundingVehicles[i].relativeSpeed*3.6f
									);
				painter.drawText(a, s);
			}

			
			
		}
		
		
	}
	
	if (0) {
		pen.setWidth( 2 ); // <<<<<---   This takes no effect
		pen.setColor(Qt::black);
		painter.setPen( pen );
		painter.drawPolyline(points, 4);
		
		pen.setWidth( 5 ); // <<<<<---   This takes no effect
		pen.setColor(Qt::red);
		painter.setPen( pen );
		
		painter.drawPoints(points, 4);
		
		painter.drawText(rect,
										 Qt::AlignCenter,
										 tr("Test"));
		
	}
	painter.restore();
	
	//pen.setWidth( 1 ); // <<<<<---   This takes no effect
	painter.setPen(palette().dark().color());
	painter.setBrush(Qt::NoBrush);
	painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
	
	
}
