/* vokoscreenNG - A desktop recorder
 * Copyright (C) 2017-2024 Volker Kohaupt
 *
 * Author:
 *      Volker Kohaupt <vkohaupt@volkoh.de>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * --End_License--
 */

#include "global.h"
#include "QvkCameraController_wl.h"
#include "QvkCameraWatcher_wl.h"
#include "QvkCameraSingle_wl.h"

#include "ui_QvkCameraSingle_wl.h"

#include <QDebug>
#include <QList>
#include <QCheckBox>

/*
 *
 *            QvkCameraController_wl
 *    (Im Controller wird der Watcher und die
 *     Instanzen jeder Camera mit Surface gemanagt)
 *                     |
 *         ------------------------
 *         |                      |
 * QvkCameraWatcher_wl   QvkCameraSingle_wl
 *                       (Im Singel wird eine einzelne Camera und
 *                        und ein einzelnes Surface gemanagt)
 *                                |
 *                       QvkCameraSurface_wl
 *
 */

QvkCameraController_wl::QvkCameraController_wl( Ui_formMainWindow_wl *ui_surface )
{
    ui = ui_surface;
    ui->help_camera_camera->hide();
}


QvkCameraController_wl::~QvkCameraController_wl()
{
}


void QvkCameraController_wl::init()
{
    QvkCameraWatcher_wl *vkCameraWatcher_wl = new QvkCameraWatcher_wl;
    connect(vkCameraWatcher_wl,
            &QvkCameraWatcher_wl::signal_cameraChanged,
            this,
            [this](QString device){slot_camera_added_or_removed(device);}
    );

    vkCameraWatcher_wl->init();
}


void QvkCameraController_wl::slot_camera_added_or_removed( QString device )
{
    // Varibale device enthält zum Beispiel folgenden Inhalt "/dev/video1:::UVC Camera (046d:0809):::added"

    if(device.contains("added")){
        vkCameraSingle_wl = new QvkCameraSingle_wl;
        vkCameraSingle_wl->set_GUIui(ui);
        vkCameraSingle_wl->set_init(device);
        vkCameraSingle_wl->set_PixelFormatOnComboBox(device.section(":::", 0, 0));
        ui->verticalLayout_3->addWidget(vkCameraSingle_wl);
        emit signal_forSystrayCameraAdded(vkCameraSingle_wl->ui->checkBoxCameraOnOff);

        // Kameras werden in der GUI disabled oder enabled
        connect( vkCameraSingle_wl,
                 &QvkCameraSingle_wl::signal_checkBoxCameraOnOff,
                 this,
                 [=](bool checked){slot_disableEnableCameras(checked);}
        );

        // Kamera On Off wird von der GUI weitergeleitet an Systray
        connect( vkCameraSingle_wl,
                 &QvkCameraSingle_wl::signal_forSystrayCameraOnOff,
                 this,
                 [=](QCheckBox *checkBox){ emit signal_forSystrayCameraOnOff(checkBox);}
        );

   }

    if(device.contains("removed")){
        // Camera ist aktiv und wurde entfernt
        QList<QCheckBox *> listCheckBox = ui->centralwidget->findChildren<QCheckBox *>();
        for ( int i = 0; i < listCheckBox.count(); i++ ) {
            QCheckBox *checkBox = listCheckBox.at(i);
            QString str_1 = checkBox->objectName();
            QString str_2 = "checkBoxCameraVideoID_" + device.section(":::", 0, 0);
            if ( str_1 == str_2 ){
                if(checkBox->isChecked() == true){
                    checkBox->click();
                }
                break;
            }
        }

        QList<QvkCameraSingle_wl *> listCameraSingle = ui->centralwidget->findChildren<QvkCameraSingle_wl *>();
        for(int i = 0; i < listCameraSingle.count(); i++){
            QvkCameraSingle_wl *cameraSingle = listCameraSingle.at(i);
            if(cameraSingle->objectName() == QString("cameraSingleVideoID_" + device.section(":::", 0, 0 ))){
                delete cameraSingle;
            }
        }
        emit signal_forSystrayCameraRemoved(device);
    }
}


void QvkCameraController_wl::slot_disableEnableCameras(bool checked)
{
    // Disable all other cameras
    if ( checked == true ){
        QList<QCheckBox *> listCheckBox = ui->centralwidget->findChildren<QCheckBox *>();
        for ( int i = 0; i < listCheckBox.count(); i++ ) {
            QCheckBox *checkBox = listCheckBox.at(i);
            if ( checkBox->objectName().contains("checkBoxCameraVideoID_") ){
                if ( checkBox->isChecked() == false ){
                    checkBox->parentWidget()->setDisabled(true);
                }
            }
        }
    }

    // Enable all cameras
    if ( checked == false ){
        QList<QCheckBox *> listCheckBox = ui->centralwidget->findChildren<QCheckBox *>();
        for ( int i = 0; i < listCheckBox.count(); i++ ) {
            QCheckBox *checkBox = listCheckBox.at(i);
            if ( checkBox->objectName().contains("checkBoxCameraVideoID_") ){
                if ( checkBox->isChecked() == false ){
                    checkBox->parentWidget()->setEnabled(true);
                }
            }
        }
    }
}
