#include "saveroutedialog.h"
#include "qdebug.h"
#include "ui_saveroutedialog.h"

#include <QMessageBox>
#include <QLineEdit>

#include "routeglobalvariant.h"


saveRouteDialog::saveRouteDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::saveRouteDialog)
{
    ui->setupUi(this);
    this->setWindowTitle(u8"选择航路特征和航线号");
    this->resize(500,300);
    this->setWindowFlags(Qt::WindowCloseButtonHint); //关闭右上角帮助(?)按钮。
//    auto lineEdit = ui->currentSpinBox->findChild<QLineEdit*>();
//    ui->currentSpinBox;
//    ui->currentSpinBox->findChild<QPushButton*>()
//             QPushButton *button = ui->currentSpinBox->findChild<QPushButton *>("up-button");

//            QList<QAbstractButton *> allPButtons = ui->currentSpinBox->findChildren<QAbstractButton *>();
//    allPButtons[1]->setEnabled(false);

    ui->importState->setText(u8"未导入");

    ui->currentSpinBox->setMinimum(1);
//        ui->currentSpinBox->setMaximum(1);
//    ui->currentSpinBox->setStyleSheet("QSpinBox#up-button{color: red}");

    ui->routeNumber->setMinimum(1);
    ui->routeNumber->setMaximum(9999);
    ui->routeHeight->setMinimum(0);
    ui->routeHeight->setMaximum(9999.99);


//    ui->routeHeightText->setText("500");
//    ui->routeNumber->setText("1");
    addRouteTypeItem();

    lastRoute = ui->currentSpinBox->value();

    int currentRoute = ui->currentSpinBox->value();
    routeClosedState.insert(currentRoute,ui->isCloseRouteCheckBox->isChecked());
    routeHeight.insert(currentRoute,ui->routeHeight->value());
    routeNumber.insert(currentRoute,ui->routeNumber->value());
    routeType.insert(currentRoute,ui->routeType->currentIndex()+1);
}



saveRouteDialog::~saveRouteDialog()
{
    delete ui;
}

bool saveRouteDialog::isCloseRoute()
{
    return ui->isCloseRouteCheckBox->isChecked();
}

QMap<int,double> saveRouteDialog::getRouteHeight()
{
    return routeHeight;
}

QMap<int,int> saveRouteDialog::getRouteNumber()
{
    return routeNumber;
}

QMap<int,int> saveRouteDialog::getRouteType()
{
    return routeType;
//    if(ui->routeType->currentText()=="飞行航线")
//    {
//        return 1;
//    }
//    if(ui->routeType->currentText()=="应急航线")
//    {
//        return 2;
//    }
//    if(ui->routeType->currentText()=="回收航线")
//    {
//        return 3;
//    }
//    if(ui->routeType->currentText()=="围栏航线")
//    {
//        return 4;
//    }
//    if(ui->routeType->currentText()=="跑道航线")
//    {
//        return 5;
//    }

}

QMap<int, int> saveRouteDialog::getRouteClosedState()
{
    return routeClosedState;
}

QString saveRouteDialog::getRouteText()
{
    return ui->routeType->currentText();
}

void saveRouteDialog::setMaxValue(int max)
{
    ui->currentSpinBox->setMaximum(max);
}

void saveRouteDialog::setDrawMode(int i)
{
    this->drawMode = i;// 1表示开启
}

void saveRouteDialog::setMapType(int i)
{
    this->mapType = i;
}

//设置高度控件不可见
void saveRouteDialog::setHeightVisible(bool b)
{
    if(b==false)
    {
        ui->routeHeight->setVisible(false);
        ui->label->setVisible(false);
    }
}


void saveRouteDialog::closeEvent(QCloseEvent *event)
{
    this->isClickedCloseBtn = true;
}

//QAbstractSpinBox::StepEnabled saveRouteDialog::stepEnabled(int value)
//{
//    if(value == ui->currentSpinBox->maximum())
//    {
//        return (QAbstractSpinBox::StepDownEnabled);
//    }
//    else if(value == ui->currentSpinBox->minimum())
//    {
//        return (QAbstractSpinBox::StepUpEnabled);
//    }
//    else
//    {
//        return (QAbstractSpinBox::StepUpEnabled|QAbstractSpinBox::StepDownEnabled);
//    }
//}


void saveRouteDialog::addRouteTypeItem()
{
    RouteGlobalVariant routeGVar;
    QMap<int,QString> routeTypeText = routeGVar.getRouteType();
    for(int i = 1; i <= routeTypeText.count();i++)
    {
        ui->routeType->addItem(routeTypeText[i]);
    }
//    ui->routeType->addItem(u8"飞行航线");
//    ui->routeType->addItem(u8"应急航线");
//    ui->routeType->addItem(u8"回收航线");
//    ui->routeType->addItem(u8"围栏航线");
//    ui->routeType->addItem(u8"通场航线");
    //    ui->routeType->addItem(u8"跑道航线");
}

void saveRouteDialog::resetState()
{
    //状态重置
    ui->routeHeight->setValue(0);
    ui->routeType->setCurrentIndex(0);
    ui->routeNumber->setValue(1);
    ui->isCloseRouteCheckBox->setChecked(false);
    ui->importState->setText(u8"未导入");
}

void saveRouteDialog::historyState(int arg1)
{
    //载入历史状态
    ui->routeHeight->setValue(routeHeight[arg1]);
    ui->routeType->setCurrentIndex(routeType[arg1]-1);
    ui->routeNumber->setValue(routeNumber[arg1]);
    ui->isCloseRouteCheckBox->setChecked(routeClosedState[arg1]);
    ui->importState->setText(u8"已导入");
}

void saveRouteDialog::on_cancelRoute_clicked()
{
    if(drawMode)
    {
        this->reject();
        return;
    }

    this->close();
}


void saveRouteDialog::on_isCloseRouteCheckBox_stateChanged(int arg1)
{
    if(arg1)
    {
        ui->isCloseRouteCheckBox->setChecked(true);
    }
    else
    {
        ui->isCloseRouteCheckBox->setChecked(false);
    }
}


void saveRouteDialog::on_importBtn_clicked()
{
    int currentRouteID = ui->currentSpinBox->value();

    int type = ui->routeType->currentIndex()+1;
    double height = ui->routeHeight->value();
    int number = ui->routeNumber->value();
    int closedState = ui->isCloseRouteCheckBox->isChecked();


    if(drawMode)
    {//仅用于兼容二维航线绘制模式
        routeClosedState.insert(currentRouteID,closedState);
        routeHeight.insert(currentRouteID,height);
        routeNumber.insert(currentRouteID,number);
        routeType.insert(currentRouteID,type);
        this->accept();
        return;
    }
    /*以下用于导入外部航线文件*/
    // 发送信号添加当前航线
    emit importRoute(currentRouteID,type,number,height,closedState);
    // 接收航线添加成功的值,并修改当前航线导入状态
    if(addRouteState)
    {
        //保存当前航线信息,用于历史回溯
        routeClosedState.insert(currentRouteID,closedState);
        routeHeight.insert(currentRouteID,height);
        routeNumber.insert(currentRouteID,number);
        routeType.insert(currentRouteID,type);
        routeSaveState.insert(currentRouteID,1);
        saveRouteID.insert(ui->currentSpinBox->value());

        ui->importState->setText(u8"已导入");

        if(currentRouteID==ui->currentSpinBox->maximum())
        {
            this->accept();
        }
        else
        {
            lastRoute = ui->currentSpinBox->value();
            //发出信号高亮下一条几何对象
            emit highLightGeometry(lastRoute+1,lastRoute);

            ui->currentSpinBox->stepBy(1);
        }
    }
    else
    {
        QMessageBox::warning(this, "Warning",u8"航线导入失败!");
    }

}


void saveRouteDialog::on_currentSpinBox_valueChanged(int arg1)
{
    if(arg1==lastRoute) return;

   /*判断当前航线是否已导入*/
    if(!saveRouteID.contains(lastRoute))
    {
        int ret = QMessageBox::warning(this, u8"提示",
                                        (u8"当前航线未导入!\n"
                                           u8"是否继续?"),
                                        QMessageBox::Yes | QMessageBox::No,
                                        QMessageBox::No);
        if(ret==QMessageBox::No)
        {
            ui->currentSpinBox->setValue(lastRoute);
        }
        else
        {
            /*当前航线已导入,判断下一航线是否是已导入航线*/
            //发出信号高亮几何对象
            emit highLightGeometry(arg1,lastRoute);
            if(saveRouteID.contains(arg1))
            {
                //载入历史状态
                historyState(arg1);
            }
            else
            {
                //状态重置
                resetState();
            }
            //更新历史记录值
            lastRoute = ui->currentSpinBox->value();
        }
    }
    else
    {
        //发出信号高亮几何对象
        emit highLightGeometry(arg1,lastRoute);
        if(saveRouteID.contains(arg1))
        {
            //载入历史状态
            historyState(arg1);
        }
        else
        {
            //状态重置
            resetState();
        }
        //更新历史记录值
        lastRoute = ui->currentSpinBox->value();
    }

}

void saveRouteDialog::getAddRouteState(int addState)
{
    this->addRouteState = addState;
}