/* MSHV RadioAndNetW for Log program
 * Copyright 2015 Hrisimir Hristov, LZ2HV
 * (Edited by Harper Innes, VK1TTY - to remove Gendered Language and Replace with Non-Gendered language)  NOTE:May be used under the terms of the GNU General Public License (GPL)
 */
#ifndef RADIONETW_H
#define RADIONETW_H

#include "../../config.h"
#include <QObject>
#include <QString>
//#include <QHostAddress>
//#include <QHostInfo>
#include <QQueue>
#include <QHash>

class MessageClient;
class QTimer;
class QHostInfo; //HV is here QtGui

#include "../hvqthloc.h"
#define QT_NO_DEBUG_STREAM // remove qdebug from tcp socet
#include <QTcpSocket>

//explicit PSK_Reporter(MessageClient *, QObject *parent = nullptr);
class HvTcpClient : public QObject
{
    Q_OBJECT
public:
    explicit HvTcpClient(QObject *parent = nullptr);

    void DisconectOnly();
    bool writeData(QString);

signals:
    void ConectionInfo(QString);

public slots:
    void Connect(QString host,QString port,QString login_name);
    void SetData(QString data);

private slots:
    void connectToHost();
    void readTelnet();
    void connected_s();
    void disconnected_s();

private:
    HvQthLoc THvQthLoc;
    bool tray_once_agen;
    bool is_pfx(QString s);
    QString FindBaseCallRemAllSlash(QString str);
    //bool f_conect;
    //QTimer *t_tcp_connect;
    QString s_tcphost;
    QString s_tcpport;
    QString s_tcpdata;
    QString s_login_name;
    QTcpSocket *socket;
    QString itcp_call_err;

protected:

};

#include <QDialog>
#include <QComboBox>
#include "../hvinle.h"
class SpotDialog : public QDialog
{
    Q_OBJECT
public:
    SpotDialog(bool,QWidget * parent = 0 );
    ~SpotDialog();

    void setAllEdit(QStringList list,int id,int offset);//id 0=psk 1=dx_spot  10=empty
    QStringList getAllEdit();
    bool send_spot_result;
    void SetTcpconInfo(QString);
    void SetDistUnit(bool f_);
    void SetLocFromDB(QString);
    void SetFont(QFont);

signals:
    void EmitReconnect();
    void FindLocFromDB(QString);

private:
    int s_f_offset;
    QString s_mode;
    bool block_loc;
    bool f_km_mi;
    QString CalcDistance(QString myl, QString theirl);
    HvQthLoc THvQthLoc;
    QLabel *l_tcpcon_info;
    QPushButton *b_recon_host;
    QString s_end_info;
    HvInLe *le_my_call;
    HvInLe *le_my_loc;
    HvInLe *le_freq;
    HvInLe *le_dx_call;
    HvInLe *le_dx_loc;
    QComboBox *Cb_prop;
    QStringList id_prop;
    QLineEdit *le_remarks;
    QPushButton *b_send_spot;
    QPushButton *b_cancel_spot;
    QLabel *l_loc_from_db;
    //bool
    //QPushButton *pb_lookup_loc;

private slots:
    void CheckForWalidSpot(QString);
    void LookupLocFromDb();
    void SendSpot();
    void CancelSpot();
    void InfoChanged(QString);
    void PropChanged(QString);//for set fokus
    void Check(QString);
    //void InfoChanged2(QString);
    //void MyLocChang(QString);
    void FreqEntered();
    void DxCallEntered();
    //void DxLocEntered();
    void RemarksEntered();

};

#include <QWidget>
#include <QDialog>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QGroupBox>
#include <QLabel>
#include <QCheckBox>
#include <QFile>
#include <QMessageBox>
#include <QSslSocket>
#include <QUdpSocket>
#include "radlist.h"
#include "editradinfo.h"
#include "bcnlistw.h"
#include "pskreporterudptcp.h"
class RadioAndNetW : public QWidget
{
    Q_OBJECT
public:
    RadioAndNetW(QString path,bool,int,int,QWidget *parent = 0);
    ~RadioAndNetW();

    //void SetFileOpen();
    void CloseBcnList();
    //void CreateDialogSpot(QWidget*);
    //void StartEmptySpotDialog();
    void SetLocalStation(QString myCall,QString myLoc,QString band,int con_id);
    void AddRemoteStation(QString theirCall,QString theirLoc,int frq,QString mode,int snr,int id,QString pwidth);
    void SetLocFromDB(QString);
    void SetDistUnit(bool);
    void SetModeForFreqFromMode(int i);
    void SetFreqGlobal(QString);
    void SetFont(QFont f);
    void SendDecodTxt(QString tim,int sn,QString dt,int frq,QString msg);///*,QString mod*/
    void SetAuto(bool);
    void SetTx(bool);
    void SetDxParm(QString,QString,QString);
    void SetUdpDecClr();
    void FindFreqRadList(int id,QString &sfrq);
    void SetTxMsg(QString);
    void StartEmptySpotDialog();
    void ShowBcnList();
    void FindFreqLocFromBcnList(QString theirCall,int id,QString &theirLoc,QString &sfrq);
    void SaveSettings();

    bool GetUdpBroadLoggedQso()
    {
        return cb_udp_broad_log_qso->isChecked();
    };
    bool GetUdpBroadDecod()
    {
        return cb_udp_broad_decod->isChecked();
    };
    /*bool GetUdpBroadLoggedAdif()
    {
        return cb_udp_broad_log_adif->isChecked();
    };
    bool GetTcpBroadLoggedAdif()
    {
        return cb_tcp_broad_log_adif->isChecked();
    };
    bool GetClubLogAdif()
    {
        return cb_clublog->isChecked();
    };
    bool GetUdp2BroadLoggedAdif()
    {
        return cb_udp2adif->isChecked();
    };
    bool GetQRZLogAdif()
    {
        return cb_qrzlog->isChecked();
    };*/
    bool GetLogAdifAll()
    {
        bool res = false;
        if 	(cb_udp_broad_log_adif->isChecked() ||
             cb_tcp_broad_log_adif->isChecked() ||
             cb_clublog->isChecked() ||
             cb_udp2adif->isChecked() ||
             cb_qrzlog->isChecked()) res = true;
        return res;
    }
    void GetFtFr(qint32 *a);
    QWidget *GetRadListW()
    {
        return TRadListW;
    };

signals:
    void FindLocFromDB(QString);
    void EmitUdpBroadLoggedAll(bool,bool);
    void EmitUdpCmdDl(QStringList);
    void EmitUdpCmdStop(bool);
    void EmitOpenRadNetWToRecon();
    void EmitUploadClubLogInfo(QString);

public slots:
    void ConDiscon();
    void TCPServPortChanged(QString);
    //void SetLocFromDB(QString);
    //void ReadHttpRequest(QNetworkReply*);
    //void readTelnet();
    //void displayError(QAbstractSocket::SocketError socketError);
    //void ConectToTelnetServer();
    //void FindFreqRadList(int id,QString &sfrq);
    //void FindFreqLocFromBcnList(QString theirCall,int id,QString &theirLoc,QString &sfrq);
    void SendLoggedQSO(QStringList ls);
    void SendAdifRecord(QString);
    void replayDecodes();
    void networkErrorUDPBrodcast(QString const&);
    void SetUploadSelected(QByteArray);

private:
    QWidget *w_parent;
    int s_cont_id;
    bool f_mods_accept_cmd;
    QString s_mode;
    QString s_smode;
    //int prev_pos_dec;
    int id_activ_upd;
    int pos_dec;
    int pos_upd;
    QString s_dx_call;
    QString s_report;
    QString s_dx_grid;
    bool s_auto;
    bool s_tx;
    QString s_tx_msg;
    void SendStatus(int);

    QString FREQ_GLOBAL;
    QString s_mode_str_for_ferq;
    HvQthLoc THvQthLoc;
    bool tcp_conect_stat;
    void SetGlobalTcpStat(QString);
    bool global_tcp_stat;

    HvTcpClient *TelnetClient;
    //QTcpSocket *tcpSocket;
    //QString currentFortune;
    //quint16 blockSize;
    //void ConectToTelnetServer();
    //QHttp *http_post;
    //QNetworkAccessManager *networkManager;
    //void SendHttpMsg(QString my_c, QString dx_c, QString freq QString info);
    SpotDialog *TSpotDialog;
    HvBcnListW *THvBcnListW;
    //QList<QStringList> Bcnlist;
    //void ReadBcnList(QString);

    QWidget *TRadListW;
    HvRadList *THvRadList;
    EditRadInfo *TEditRadInfo;
    QString s_band;//vremenno
    QString sr_path;
    //void SaveSettings();
    bool isFindId(QString id,QString line,QString &res);
    void ReadSettings();

    PSKReporter *psk_Reporter;

    QCheckBox *cb_start_stop_psk_rpt;
    QCheckBox *cb_udp_tcp_psk_rpt;
    QLineEdit *UDPServer;
    QLineEdit *UDPPort;
    QLabel *l_con_info;
    QLabel *l_tcpcon_info;

    QString s_myCall;
    QString s_myLoc;

    QPushButton *pb_re_conect;

    //bool block_emit_tcp;
    void ReadTelnetList(QString path);
    QLineEdit *TCPServer;
    QLineEdit *TCPPort;
    QPushButton *pb_tcp_conect;
    QComboBox *Cb_telnet;

    //void FindFreqLocFromBcnList(QString theirCall,int id,QString &theirLoc,QString &sfrq);
    //void FindFreqRadList(int id,QString &sfrq);
    void StartSpotDialod(QStringList,int f_offset,int id);
    QString RemBegEndWSpaces(QString str);

    MessageClient *m_messageClientBroad;
    QLabel *l_udp_broad_info;
    QCheckBox *cb_udp_broad_log_qso;
    QCheckBox *cb_udp_broad_log_adif;
    QCheckBox *cb_udp_broad_decod;
    QLineEdit *UDPServerBroad;
    QLineEdit *UDPPortBroad;
    QPushButton *pb_re_conect_udp_broad;
    QTimer *decod_upd_timer;

    QUdpSocket *socet_udp2_broad;
    QCheckBox *cb_udp2adif;
    QLineEdit *udp2_Server;
    QLineEdit *udp2_Port;

    QTcpSocket *socet_tcp_broad;
    QLineEdit *TCPServerBroad;
    QLineEdit *TCPPortBroad;
    QCheckBox *cb_tcp_broad_log_adif;
    QPushButton *b_reset_default_freqs_cont;

    bool cl_send_file;
    bool cl_err_flag;
    bool cl_once_data_err_msg;
    int cl_pos_ul_writ;
    int cl_pos_ul_read;
    int cl_is_act_send_check;
    QTimer *cl_upd_timer;
    QString file_cl_error;
    QSslSocket *socet_tcp_clublog;
    //QLabel *l_tcp_clublog_info;
    QLineEdit *LeClubLogServer;
    QLineEdit *LeClubLogPort;
    QLineEdit *LeClubLogPost0;
    QLineEdit *LeClubLogPost1;
    QCheckBox *cb_clublog;
    QLineEdit *LeClubLogMail;
    QLineEdit *LeClubLogPass;
    QLabel *LClubLogCall;
    void RefreshUdpOrTcpBroadLoggedAll();
    bool isUnsafe(QChar compareChar);
    QString decToHex(QChar num, int radix);
    QString convert(QChar val);
    QString URLEncode(QString strEncode);
    void CheckClubLogError(int,int);
    void StartStopClubLog(bool);
    void SendClubLogAdif(QString);

    int qrz_pos_ul_writ;
    int qrz_pos_ul_read;
    int qrz_is_act_send_check;
    bool qrz_once_data_err_msg;
    QString qrz_error_str;
    bool qrz_err_flag;
    QTimer *qrz_upd_timer;
    QSslSocket *socet_tcp_qrzlog;
    QLineEdit *LeQRZLogServer;
    QLineEdit *LeQRZLogPort;
    QLineEdit *LeQRZLogApi;
    QLineEdit *LeQRZLogPost;
    QCheckBox *cb_qrzlog;
    void CheckQRZLogError();
    void StartStopQRZLog(bool);
    void SendQRZLogAdif(QString);

private slots:
    void StartStopTCPBroad(bool);
    void DecodUpdTimer();
    //void OpenRadNetWToRecon();
    void CbTelnetChanged(QString);
    void ServTextChanged(QString);
    void PortTextChanged(QString);
    void SetConectionInfo(QString);
    void SetConectionTcpInfo(QString);
    void StInfoChanged(QStringList,int);
    void Reconnect();
    void StartStopReport(bool);
    void UdpTcpChangedPsk(bool);
    void StartStopUdpBroad(bool);
    void ReconnectUdpBroad();
    void UDPSrvPortBroadChanged(QString);
    void ConectionInfoBroad(QString);
    void SetDefaultFreqsActType(int id);
    void SetDefaultFreqsActTypeBut();
    void ResetDefaultFreqsBut();
    void SetDefaultFreqs(bool f);
    void set_reply_clr(QStringList);
    void set_halt_tx(bool);
    //void connected_clublog();
    //void disconnected_clublog();
    void readClubLog();
    void cb_clublog_toggled();
    void UplClubLogAdif();
    void StartStopUdp2Broad(bool);
  	//void connected_qrzlog();
  	//void disconnected_qrzlog();
    void cb_qrzlog_toggled();
    void readQRZLog();
    void UplQRZLogAdif();

};

#endif
