#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"

double t,dt;
double xmin,xmax,ymin,ymax;


MainWindow::MainWindow(QWidget *parent) :  QMainWindow(parent),
      ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    xmin=-20;xmax=20;
    ymin=-20;ymax=20;
    R=new repere(this,ui->graphicsView,xmin,xmax,ymin,ymax);
    Rtraj=new repere(this,ui->graphicsViewTraj,25*xmin,5*xmax,15*ymin,15*ymax);
    t=0.0; dt=0.01;
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(Clock()));
    timer->start(10);
    Init();

}


MainWindow::~MainWindow()
{   Rtraj->Save("Rtraj");
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{   Qt::KeyboardModifiers keyMod = QApplication::keyboardModifiers ();
    bool isSHIFT = keyMod.testFlag(Qt::ShiftModifier);
    if (isSHIFT && event->key()==Qt::Key_Z) R->Zoom(1.1);
    if (!isSHIFT && event->key()==Qt::Key_Z) R->Zoom(1/1.1);
    if (isSHIFT && event->key()==Qt::Key_T) dt=dt*1.1;
    if (!isSHIFT && event->key()==Qt::Key_T) dt=dt/1.1;
    if (dt>0 && event->key()==Qt::Key_0) {dt=0; return;};
    if (dt==0 && event->key()==Qt::Key_0) dt=0.01;
}


void MainWindow::Clock()
{       t=t+dt;
        X.push_back(Rob.x); Y.push_back(Rob.y);
        R->Clean();
        double xold=Rob.x;
        double yold=Rob.y;
        Ctr.Clock(Rob.x,Rob.y,Rob.theta,Rob.ell,Rob.psi);
        Rob.Clock(Ctr.deltag,Ctr.dell);
            R->DrawLine(0,0,-100,100*tan(Ctr.zeta),QPen(Qt::red));
            R->DrawLine(0,0,-100,-100*tan(Ctr.zeta),QPen(Qt::red));
            QColor col_voile(Qt::red);
            if (Rob.flag_sail) col_voile=Qt::yellow;
            QColor col_rob(Qt::green);
            if (Ctr.q==0) col_rob=Qt::cyan;
            if (Ctr.q==1) col_rob=Qt::darkCyan;
            if (Ctr.q==2) col_rob=Qt::blue;
            if (Ctr.q==3) col_rob=Qt::magenta;


            R->DrawSailboat(Rob.x,Rob.y,Rob.theta,col_rob,Ctr.deltag,Qt::red,Rob.deltav,col_voile);

            //             Rtraj->DrawBox(Rob.x-1,Rob.x+1,Rob.y-1,Rob.y+1,QPen(Qt::darkGray),QBrush(Qt::NoBrush));
            Rtraj->DrawLine(xold,yold,Rob.x,Rob.y,QPen(Qt::darkGray));
            if (ui->checkBoxWind->isChecked())
                R->DrawArrow(Rob.x+3,Rob.y+3,Rob.a*cos(Rob.psi),Rob.a*sin(Rob.psi),0.2,QColor("Red"));  // vent
            if (ui->checkBoxApparentWind->isChecked())
                R->DrawArrow(Rob.x+3,Rob.y+3,Rob.a_ap*cos(Rob.psi_ap+Rob.theta),Rob.a_ap*sin(Rob.psi_ap+Rob.theta),0.2,QColor("darkRed"));
            if (ui->checkBoxSpeed->isChecked())
                R->DrawArrow(Rob.x,Rob.y,Rob.v*cos(Rob.theta),Rob.v*sin(Rob.theta),0.1,QPen(Qt::black));
            R->DrawArrow(Rob.x,Rob.y,5*cos(Ctr.thetabar),5*sin(Ctr.thetabar),0.1,QPen(Qt::cyan));

            R->DrawText(QString("t=%1 \r\n x=%2 \r\n y=%3 \r\n theta=%4\r\n v=%5").arg(t).arg(Rob.x).arg(Rob.y).arg(Rob.theta).arg(Rob.v),1);
            R->DrawText(QString("fv=%1 \r\n fg=%2 \r\n P=%3 ").arg(Rob.fv).arg(Rob.fg).arg(Rob.E/t),8);
            R->DrawText(QString("Ctr.dell=%1 \r\n Ctr.dell=%2 \r\n Rob.deltav=%3").arg(Ctr.dell).arg(Rob.ell).arg(Rob.deltav),15);
            R->DrawText(QString("q=%1 ").arg(Ctr.q),19);
        if ((Rob.x>R->xmax)|(Rob.x<R->xmin)|(Rob.y>R->ymax)|(Rob.y<R->ymin)) R->Center(Rob.x,Rob.y);
}



void MainWindow::Init()
{   Rob.psi=0.01*2*M_PI*ui->PsiScrollBar->value();
    Rtraj->DrawLine(0,0,-500,500*tan(Ctr.zeta),QPen(Qt::black));
    Rtraj->DrawLine(0,0,-500,-500*tan(Ctr.zeta),QPen(Qt::black));
}



void MainWindow::on_PsiScrollBar_valueChanged(int value){Init();}
