/** \file
 *
 *  Contains the Waterlinked class declaration.
 *
 *  Waterlinked.h should only be included by Waterlinked.cpp
 *  Other classes should include WaterlinkedIF.h
 *
 *  Copyright (c) 2023 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef WATERLINKED_H
#define WATERLINKED_H

#include "WaterlinkedIF.h"

#include "component/SyncComponent.h"
#include "io/LoadControl.h"
#include "io/UartStream.h"
#include "logger/Logger.h"

#include "data/Point3D.h"
#include "data/Point6D.h"
#include "data/Matrix3x3.h"

class UniversalDataReader;
class UniversalDataWriter;


#define UART_BUFSIZE 10240L

class Waterlinked : public SyncSensorComponent
{
public:

    Waterlinked( const Module* module );

    virtual ~Waterlinked();

    virtual void run();
    virtual RunState start();
    virtual RunState starting();
    virtual RunState pause();
    virtual RunState paused();
    virtual RunState resume();
    virtual RunState resuming();
    virtual RunState runnable();

    virtual RunState resetting()
    {
        return stop();
    }

    virtual RunState stop();
    virtual RunState stopping();
    virtual RunState stopped();
    void uninitialize();
    void logVoltageAndCurrent();
    /// Should return [myNamespace]::SIMULATE_HARDWARE, or [myNamespace]::POWER, etc
    virtual ConfigURI getConfigURI( ConfigOption configOption ) const;

private:
    Waterlinked( const Waterlinked& old );

    // Component interfaces
    LoadControl loadControl_;
    UartStream uart_;

    // scan configuraiton settings
    bool readConfig( void );

    bool checkTimeouts( void );
    bool simPD6( void );
    bool readPD6( void );
    bool parsePD6( void );
    void processPD6( void );
    void writeData( void );
    void setGroundVelWritersInvalid( bool invalid );
    bool okayToPowerOn( void );

    bool hasPD6_;

    Timespan dvlTimeout_;
    Timespan dvlFailTimeout_; // How long we can go without valid data
    Timespan powerOnTimeout_; // How long it takes for the DVL to begin sending data
    Timestamp startTime_, dvlFailTime_, bottomLockTime_;

    UniversalDataReader* depthReader_;
    DataReader* power24vConverterDataReader_;
    ConfigReader* surfaceThresholdCfgReader_;

    UniversalDataWriter* altitudeWriter_;

    // Vehicle referenced bottom-track velocity data
    UniversalBlobWriter* velocityWrtGroundWriter_;
    UniversalDataWriter* xVelWrtGroundWriter_;
    UniversalDataWriter* yVelWrtGroundWriter_;
    UniversalDataWriter* zVelWrtGroundWriter_;

    DataWriter* veloInstFlagWriter_;

    bool isDataRequested();

    bool dvlTimeoutArmed_;
    char deviceResponse_[UART_BUFSIZE];

    int status_;

    // Bottom track variables
    Point3D veloInst_;
    double veloInstX_;
    double veloInstY_;
    double veloInstZ_;
    double veloError_;
    int veloInstFlag_;

    double altEstimate_;

    float surfaceThreshold_;

    int bitError_;

    bool debug_;
};
#endif /*Waterlinked_H*/
