#ifndef QGGRIFOBEAMUPFORM_H
#define QGGRIFOBEAMUPFORM_H

#include <QWidget>

#include "qggrifodownloadentitiesform.h"
#include "BupTFTP.h"
//#include "fpgabridgetftp.h"

#include "fpgaflashengine.h"

#include "qggrifobeamupproperties.h"

#include <QFileDialog>
#include <QFileSystemWatcher>
#include <QStringList>
#include <QSettings>
#include <QTimer>
#include <QStatusBar>
#include <QPlainTextEdit>
#include "historydb.h"
#include "flashtarget.h"

namespace Ui {
class QgGrifoBeamUpForm;
}


struct bsk_download_site_t;
struct bsk_download_partition_t;

#include "fgpaprogrammer.h"

class QgGrifoBeamUpForm : public QWidget
{
    Q_OBJECT

public:
    explicit QgGrifoBeamUpForm(QWidget *parent = 0);
    ~QgGrifoBeamUpForm();

    QgGrifoBeamupProperties& props;

    QString default_ip;

    QString ShowTargetInfo(bool popup);

    FpgaFlashProfile profile;

    dbHistory *_dbHistory;

public slots:
    void saveSettings();

    void refreshServers();

    void serverHello(unsigned int ip, int port, const QString& msg);

    void partitionUpdate(unsigned int ip, int port, const QString& msg);

    void stateReady();
    void stateBusy();

    void uploadGo();

    void selectByFile(const QString& fname, QTreeWidgetItem *i=0);

    void selectWatchDir(const QString& dName);

    void showMessage(const QString& msg);

    void statusUpdated(int sts, int progress, const QString& msg);

    void updateGrifoDownloadSite(Qt::CheckState checked);

    void clearTargets();

signals:
    void itemSelected();

private slots:
    void updateWatchedDirectory(const QString& path);
    void updateWatchedFile(const QString& path);

    void updateSelected(const bsk_download_site_t* site, const bsk_download_partition_t*p);

    void addPendingList(const QString& file);

    void on_tbFileInfo_clicked();

    void on_btnStart_clicked(bool checked);

    void on_btnStart_clicked();

    //void on_pushButton_2_clicked();

    //void on_cbFPGA_currentIndexChanged(int index);

    void on_btnReset_clicked();

    void on_btnErase_clicked();

    void on_btnFlashID_clicked();

    void on_btnGetStatus_clicked();

    void on_btnFinish_clicked();

    void on_cbSimulate_clicked(bool checked);

    void on_tbGo_clicked();

    void on_cb_GoldenFlash_stateChanged();

    void on_btnGetTargetInfo_clicked();

    void on_cb_TargetAddress_clicked(bool checked);

    void on_btnGetValue_clicked();

    void on_btnMem_clicked();

    void on_btnFPGA_clicked();

    void on_btnHistory_clicked();

    void on_btnAddHistory_clicked();

    void on_tbRefresh_clicked();

    void on_tbHistory_clicked();

    void on_tbFileSelect_clicked();

    void on_btnWrite_clicked();

    void startEnabled(bool _status);

private:
    Ui::QgGrifoBeamUpForm *ui;

    QgGrifoDownloadEntitiesForm* entitiesForm;

    //FpgaBridgeTftp tftp;
    BupTFTP tftp;


    int state;

    void setState(int s);

    void dragEnterEvent(QDragEnterEvent*event);

    void dropEvent(QDropEvent*);

    QFileDialog fileSelector;
    QFileSystemWatcher fileWatcher;

    QStringList watchedFiles;
    QStringList pendingList;

    QSettings settings;
    QTimer pingTimer;    
    FgpaProgrammer* programmer;

    bool getProfile(unsigned int sectorSize,
                    unsigned int baseOffset);

    QStatusBar *statusBar;

    bool extractInfoFromZip(QString _fileName);

    void programWithProfile(bool checked);

    QString ShowFileInfo(const QString& fname, bool popup=true);

    void addToLog(QString _msg, unsigned int sts=0 );
    void setColorLog(QPlainTextEdit *logtext,Qt::GlobalColor newColor , Qt::GlobalColor newBckColor );

    flashTargetClass *flashTargets;

    void setTargetOnScreen();
};

#endif // QGGRIFOBEAMUPFORM_H
