/*
  ==============================================================================

  This is an automatically generated GUI class created by the Introjucer!

  Be careful when adding custom code to these files, as only the code within
  the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  and re-saved.

  Created with Introjucer version: 3.1.0

  ------------------------------------------------------------------------------

  The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions"
  Copyright 2004-13 by Raw Material Software Ltd.

  ==============================================================================
*/

#ifndef __JUCE_HEADER_9002020A4DD09B20__
#define __JUCE_HEADER_9002020A4DD09B20__

//[Headers]     -- You can add your own extra header files here --
#include "JuceHeader.h"
#include "IQPlaneComponent.h"
#include "GraphComponent.h"
#include "LogComponent.h"
#include "ControlComponent.h"
#include "Receiver.h"
#include "Transmitter.h"
#include <radio/Vector.hpp>
#include "RxBuffer.hpp"
//[/Headers]



//==============================================================================
/**
                                                                    //[Comments]
    An auto-generated component, created by the Introjucer.

    Describe your class and how it works here!
                                                                    //[/Comments]
*/
class MainComponent  : public Component,
                       public ChangeListener,
                       public AudioIODeviceCallback,
                       public ReceiverDataListener,
                       public LogHandler,
                       public Timer,
						public Thread
{
public:
    //==============================================================================
    MainComponent ();
    ~MainComponent();

    //==============================================================================
    //[UserMethods]     -- You can add your own custom methods in this section.
    //[/UserMethods]

    void paint (Graphics& g);
    void resized();



private:
    //[UserVariables]   -- You can add your own custom variables in this section.
    CriticalSection m_lock;
	LogComponent m_log;
	ScopedPointer<TextEditor> m_TextEditor;
	ScopedPointer<Receiver> m_pReceiver;
	ScopedPointer<Transmitter> m_pTransmitter;
	ScopedPointer<AudioDeviceManager> m_sharedAudioDeviceManager;
	ScopedPointer<AudioDeviceSelectorComponent> m_AudioDeviceSelectorComponent;
	ScopedPointer<ControlComponent> m_ControlComponent;
	ReceiverInterface *m_pReceiverControl;
	TransmitterInterface *m_pTransmitterControl;
	float m_samplerate;
	RxBuffer<2> m_rxBuf;
 	void timerCallback() override;
	void changeListenerCallback (ChangeBroadcaster*) override;
	void run();
    AudioDeviceManager& getSharedAudioDeviceManager();
	void audioDeviceIOCallback (const float** inputChannelData,
                                int numInputChannels,
                                float** outputChannelData,
                                int numOutputChannels,
                                int numSamples);
	void audioDeviceAboutToStart (AudioIODevice* device);
	void audioDeviceStopped();
	void receiverDataChanged(Receiver *pObj)
	{
		uint32_t i;
		uint32 nSyms;
		sym_err_t *pSoftSyms;

		nSyms = (uint32)dmin((float)128, (float)pObj->getNumSoftSym());
		pSoftSyms = pObj->getSoftSyms();

		m_pIQPlaneComponent->pointAdd(pSoftSyms, nSyms);

		for (i=0; i < 4; i++)
		{
			m_pIQPlaneComponent->trackerPointEnable(i, juce::Colours::red, ((ReceiverInterface*)pObj)->getTracker(i), 15);
		}

	}
	void handleMessage(String msg)
	{
		m_TextEditor->moveCaretToEnd();
		m_TextEditor->insertTextAtCaret (msg + newLine);
	}
    //[/UserVariables]

    //==============================================================================
    ScopedPointer<TabbedComponent> tabbedComponent;
    ScopedPointer<IQPlaneComponent> m_pIQPlaneComponent;
    ScopedPointer<GraphComponent> m_pGraphComponent;


    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

//[EndFile] You can add extra defines here...
//[/EndFile]

#endif   // __JUCE_HEADER_9002020A4DD09B20__
