/*! \brief Called when the Disconnect button has been pressed */
bool ApplicationViewWidget::onDisconnect()
{
    if(manager.busy()){
        return false;
    }


    std::vector<int> MIDs;
    for(int i=0;i<ui->connectionList->topLevelItemCount();i++){
        QTreeWidgetItem *it = ui->connectionList->topLevelItem(i);
        if(it->isSelected()){
            MIDs.push_back(it->text(1).toInt());
            manager.updateConnection(it->text(1).toInt(),
                                     it->text(3).toLatin1().data(),
                                     it->text(4).toLatin1().data(),
                                     it->text(5).toLatin1().data());

            it->setText(2,"waiting");
            it->setIcon(0,QIcon(":/images/progress_ico.png"));
            it->setTextColor(2,QColor("#000000"));

        }
    }


    manager.safeDisconnect(MIDs);
    yarp::os::Time::delay(0.1);
    selectAllConnections(false);
    return true;
}
/*! \brief Refresh all*/
void ApplicationViewWidget::refresh()
{
    selectAllConnections(true);
    selectAllModule(true);
    selectAllResources(true);

    onRefresh();
}
/*! \brief Create the context menu for the connections tree. */
void ApplicationViewWidget::createConnectionsViewContextMenu()
{
    connContex = new QMenu(ui->connectionList);
    connSubMenu =  new QMenu("Inspect",connContex);

    connConnectAction = new QAction("Connect",connContex);
    connDisconnectAction = new QAction("Disconnect",connContex);
    connSeparatorAction = new QAction(connContex);
    connSeparatorAction->setSeparator(true);
    connRefreshAction = new QAction("Refresh Status",connContex);
    connSelectAllAction = new QAction("Select All Connections",connContex);
    conn1SeparatorAction = new QAction(connContex);
    conn1SeparatorAction->setSeparator(connContex);

    connYARPViewAction = new QAction("yarpview",connSubMenu);
    connYARPReadAction = new QAction("yarpread",connSubMenu);
    connYARPHearAction = new QAction("yarphear",connSubMenu);
    connYARPScopeAction = new QAction("yarpscope",connSubMenu);

    connSelectAllAction->setIcon(QIcon(":/select-all22.svg"));
    connRefreshAction->setIcon(QIcon(":/refresh22.svg"));
    connConnectAction->setIcon(QIcon(":/connect22.svg"));
    connDisconnectAction->setIcon(QIcon(":/disconnect22.svg"));

    connect(connConnectAction,SIGNAL(triggered()),this,SLOT(onConnect()));
    connect(connDisconnectAction,SIGNAL(triggered()),this,SLOT(onDisconnect()));
    connect(connRefreshAction,SIGNAL(triggered()),this,SLOT(onRefresh()));
    connect(connSelectAllAction,SIGNAL(triggered()),this,SLOT(selectAllConnections()));

    connect(connYARPViewAction,SIGNAL(triggered()),this,SLOT(onYARPView()));
    connect(connYARPReadAction,SIGNAL(triggered()),this,SLOT(onYARPRead()));
    connect(connYARPHearAction,SIGNAL(triggered()),this,SLOT(onYARPHear()));
    connect(connYARPScopeAction,SIGNAL(triggered()),this,SLOT(onYARPScope()));

    connContex->addAction(connConnectAction);
    connContex->addAction(connDisconnectAction);
    connContex->addAction(connSeparatorAction);
    connContex->addAction(connRefreshAction);
    connContex->addAction(connSelectAllAction);
    connContex->addAction(conn1SeparatorAction);
    connContex->addMenu(connSubMenu);

    connSubMenu->addAction(connYARPViewAction);
    connSubMenu->addAction(connYARPReadAction);
    connSubMenu->addAction(connYARPHearAction);
    connSubMenu->addAction(connYARPScopeAction);


    ui->connectionList->setContextMenu(connContex);

}
/*! \brief Called when the Refresh button has been pressed */
bool ApplicationViewWidget::onRefresh()
{
    if(manager.busy()){
        return false;
    }

    std::vector<int> modulesIDs;
    std::vector<int> connectionsIDs;
    std::vector<int> resourcesIDs;

    for(int i=0;i<ui->moduleList->topLevelItemCount();i++){
        QTreeWidgetItem *it = ui->moduleList->topLevelItem(i);
        if(it->isSelected()){
            modulesIDs.push_back(it->text(1).toInt());
            it->setText(2,"waiting");
            it->setIcon(0,QIcon(":/images/progress_ico.png"));
            it->setTextColor(2,QColor("#000000"));
        }
    }


    for(int i=0;i<ui->connectionList->topLevelItemCount();i++){
        QTreeWidgetItem *it = ui->connectionList->topLevelItem(i);
        if(it->isSelected()){
            connectionsIDs.push_back(it->text(1).toInt());
            it->setText(2,"waiting");
            it->setIcon(0,QIcon(":/images/progress_ico.png"));
            it->setTextColor(2,QColor("#000000"));
        }
    }

    for(int i=0;i<ui->resourcesList->topLevelItemCount();i++){
        QTreeWidgetItem *it = ui->resourcesList->topLevelItem(i);
        if(it->isSelected()){
            resourcesIDs.push_back(it->text(1).toInt());
            it->setText(3,"waiting");
            it->setIcon(0,QIcon(":/images/progress_ico.png"));
            it->setTextColor(3,QColor("#000000"));
        }
    }

    manager.safeRefresh(modulesIDs,
                        connectionsIDs,
                        resourcesIDs);
    yarp::os::Time::delay(0.1);
    selectAllConnections(false);
    selectAllModule(false);
    selectAllResources(false);
}
/*! \brief Disconnect all modules in the application to their ports using connections list*/
void ApplicationViewWidget::disconnectConnectionSet()
{
    selectAllConnections(true);
    onDisconnect();
}
/*! \brief Select all element in the widget (modules, connections, resources)
*/
void ApplicationViewWidget::selectAll()
{
    selectAllConnections(true);
    selectAllModule(true);
    selectAllResources(true);
}
/*! \brief Select all connections */
void ApplicationViewWidget::selectAllConnections()
{
    selectAllConnections(true);
}