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

  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.

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

//[Headers] You can add your own extra header files here...
//[/Headers]

#include "MainComponent.h"


//[MiscUserDefs] You can add your own user definitions and misc code here...
//[/MiscUserDefs]

//==============================================================================
MainComponent::MainComponent (String const &homeDir, String const &hostIp)
{
    addAndMakeVisible (m_fsuipcStatus = new Label ("FSUIP Status",
                                                   TRANS("Unknown")));
    m_fsuipcStatus->setTooltip (TRANS("FSUIP Status"));
    m_fsuipcStatus->setFont (Font (15.00f, Font::plain));
    m_fsuipcStatus->setJustificationType (Justification::centred);
    m_fsuipcStatus->setEditable (false, false, false);
    m_fsuipcStatus->setColour (Label::backgroundColourId, Colour (0x00000000));
    m_fsuipcStatus->setColour (TextEditor::textColourId, Colours::black);
    m_fsuipcStatus->setColour (TextEditor::backgroundColourId, Colour (0x00000000));

    addAndMakeVisible (m_flightStatus = new Label ("Flight Status",
                                                   TRANS("Unknown")));
    m_flightStatus->setTooltip (TRANS("Flight Status"));
    m_flightStatus->setFont (Font (15.00f, Font::plain));
    m_flightStatus->setJustificationType (Justification::centred);
    m_flightStatus->setEditable (false, false, false);
    m_flightStatus->setColour (TextEditor::textColourId, Colours::black);
    m_flightStatus->setColour (TextEditor::backgroundColourId, Colour (0x00000000));

    addAndMakeVisible (m_cameraAltitude = new Slider ("Camera Altitude"));
    m_cameraAltitude->setTooltip (TRANS("Camera Altitude"));
    m_cameraAltitude->setRange (0.1, 7, 0.01);
    m_cameraAltitude->setSliderStyle (Slider::LinearHorizontal);
    m_cameraAltitude->setTextBoxStyle (Slider::NoTextBox, false, 80, 20);
    m_cameraAltitude->addListener (this);

    addAndMakeVisible (m_CameraTilt = new Slider ("Camera Tilt"));
    m_CameraTilt->setTooltip (TRANS("Camera Tilt"));
    m_CameraTilt->setRange (-90, 90, 5);
    m_CameraTilt->setSliderStyle (Slider::LinearHorizontal);
    m_CameraTilt->setTextBoxStyle (Slider::NoTextBox, false, 80, 20);
    m_CameraTilt->addListener (this);

    addAndMakeVisible (m_cameraAutoHeadingEnable = new ToggleButton ("Camera Auto Heading"));
    m_cameraAutoHeadingEnable->setTooltip (TRANS("Camera Auto Heading"));
    m_cameraAutoHeadingEnable->addListener (this);

    addAndMakeVisible (m_cameraAutoRollEnable = new ToggleButton ("Camera Auto Roll"));
    m_cameraAutoRollEnable->setTooltip (TRANS("Camera Auto Roll"));
    m_cameraAutoRollEnable->addListener (this);

    addAndMakeVisible (m_cameraAutoFlyToView = new ToggleButton ("Camera Auto FlyToView"));
    m_cameraAutoFlyToView->setTooltip (TRANS("Camera Auto FlyToView"));
    m_cameraAutoFlyToView->addListener (this);

    addAndMakeVisible (m_labelCameraAltitude = new Label ("Camera Altitude",
                                                          TRANS("0")));
    m_labelCameraAltitude->setTooltip (TRANS("Camera Altitude in meter"));
    m_labelCameraAltitude->setFont (Font (15.00f, Font::plain));
    m_labelCameraAltitude->setJustificationType (Justification::centredRight);
    m_labelCameraAltitude->setEditable (true, true, false);
    m_labelCameraAltitude->setColour (TextEditor::textColourId, Colours::black);
    m_labelCameraAltitude->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
    m_labelCameraAltitude->addListener (this);

    addAndMakeVisible (m_labelCameraTilt = new Label ("Camera Tilt",
                                                      TRANS("0")));
    m_labelCameraTilt->setTooltip (TRANS("Camera Tilt in degrees"));
    m_labelCameraTilt->setFont (Font (15.00f, Font::plain));
    m_labelCameraTilt->setJustificationType (Justification::centredRight);
    m_labelCameraTilt->setEditable (true, true, false);
    m_labelCameraTilt->setColour (TextEditor::textColourId, Colours::black);
    m_labelCameraTilt->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
    m_labelCameraTilt->addListener (this);

    addAndMakeVisible (m_labelAbout = new Label ("Label About",
                                                 String::empty));
    m_labelAbout->setFont (Font (15.00f, Font::plain));
    m_labelAbout->setJustificationType (Justification::centredLeft);
    m_labelAbout->setEditable (false, false, false);
    m_labelAbout->setColour (TextEditor::textColourId, Colours::black);
    m_labelAbout->setColour (TextEditor::backgroundColourId, Colour (0x00000000));

    addAndMakeVisible (m_labelDumpOffset = new Label ("Label Dump Offset",
                                                      TRANS("0")));
    m_labelDumpOffset->setFont (Font (15.00f, Font::plain));
    m_labelDumpOffset->setJustificationType (Justification::centredRight);
    m_labelDumpOffset->setEditable (true, true, false);
    m_labelDumpOffset->setColour (TextEditor::textColourId, Colours::black);
    m_labelDumpOffset->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
    m_labelDumpOffset->addListener (this);

    addAndMakeVisible (m_textButtonStatisticsLearn = new TextButton ("Button StatisticsLearn"));
    m_textButtonStatisticsLearn->setButtonText (TRANS("Learn"));
    m_textButtonStatisticsLearn->addListener (this);

    addAndMakeVisible (m_textButtonStatisticsEval = new TextButton ("Button StatisticsEval"));
    m_textButtonStatisticsEval->setButtonText (TRANS("Eval"));
    m_textButtonStatisticsEval->addListener (this);


    //[UserPreSize]
#ifndef FS_DEBUG	
	m_labelDumpOffset->setVisible(false);
	m_textButtonStatisticsLearn->setVisible(false);
	m_textButtonStatisticsEval->setVisible(false);
#endif
    //[/UserPreSize]

    setSize (300, 300);


    //[Constructor] You can add your own custom stuff here..
	m_labelAbout->setTooltip(ProjectInfo::projectName + String(", Version ") + ProjectInfo::versionString + String("\n(C) 2015, Jens Ahrensfeld"));
	m_toolTip = new TooltipWindow(this);

	static String workDir(homeDir + String("\\FsTrack"));

	File file(workDir);
	file.createDirectory();

	m_kml = new Kml(workDir.getCharPointer());

	m_fsLink = new FsLink(workDir.getCharPointer());
	m_fsLink->addListener(this);
	m_fsLink->setKml(m_kml);

	m_httpServer = new HttpServer(5001, workDir.getCharPointer());
	m_httpServer->addListener(m_kml);

	m_cameraAltitude->setValue(10000, juce::NotificationType::sendNotification);
	m_CameraTilt->setValue(0, juce::NotificationType::sendNotification);
	m_cameraAutoFlyToView->setToggleState(true, juce::NotificationType::sendNotification);
	m_labelCameraAltitude->setText("1000", juce::NotificationType::sendNotification);
	m_kml->createNetworkLinkFile(workDir + String("\\FsTrack.kml"), hostIp);
	m_fsLink->setUpdateInterval(1.0);
    //[/Constructor]
}

MainComponent::~MainComponent()
{
    //[Destructor_pre]. You can add your own custom destruction code here..
	m_toolTip = nullptr;
	m_kml = nullptr;
	m_fsLink = nullptr;
	m_httpServer = nullptr;
    //[/Destructor_pre]

    m_fsuipcStatus = nullptr;
    m_flightStatus = nullptr;
    m_cameraAltitude = nullptr;
    m_CameraTilt = nullptr;
    m_cameraAutoHeadingEnable = nullptr;
    m_cameraAutoRollEnable = nullptr;
    m_cameraAutoFlyToView = nullptr;
    m_labelCameraAltitude = nullptr;
    m_labelCameraTilt = nullptr;
    m_labelAbout = nullptr;
    m_labelDumpOffset = nullptr;
    m_textButtonStatisticsLearn = nullptr;
    m_textButtonStatisticsEval = nullptr;


    //[Destructor]. You can add your own custom destruction code here..
    //[/Destructor]
}

//==============================================================================
void MainComponent::paint (Graphics& g)
{
    //[UserPrePaint] Add your own custom painting code here..
    //[/UserPrePaint]

    g.fillAll (Colours::white);

    g.setColour (Colours::black);
    g.setFont (Font (15.00f, Font::plain));
    g.drawText (TRANS("[m]"),
                257, 32, 23, 24,
                Justification::centredLeft, true);

    g.setColour (Colours::black);
    g.setFont (Font (15.00f, Font::plain));
    g.drawText (CharPointer_UTF8 ("[\xc2\xb0]"),
                257, 64, 23, 24,
                Justification::centredLeft, true);

    //[UserPaint] Add your own custom painting code here..
    //[/UserPaint]
}

void MainComponent::resized()
{
    m_fsuipcStatus->setBounds (24, 232, 104, 40);
    m_flightStatus->setBounds (168, 232, 104, 40);
    m_cameraAltitude->setBounds (24, 32, 144, 24);
    m_CameraTilt->setBounds (24, 64, 144, 24);
    m_cameraAutoHeadingEnable->setBounds (24, 112, 176, 24);
    m_cameraAutoRollEnable->setBounds (24, 144, 176, 24);
    m_cameraAutoFlyToView->setBounds (24, 176, 176, 24);
    m_labelCameraAltitude->setBounds (168, 32, 88, 24);
    m_labelCameraTilt->setBounds (168, 64, 88, 24);
    m_labelAbout->setBounds (272, 0, 24, 24);
    m_labelDumpOffset->setBounds (200, 112, 72, 24);
    m_textButtonStatisticsLearn->setBounds (224, 144, 64, 24);
    m_textButtonStatisticsEval->setBounds (224, 176, 64, 24);
    //[UserResized] Add your own custom resize handling here..
    //[/UserResized]
}

void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved)
{
    //[UsersliderValueChanged_Pre]
    //[/UsersliderValueChanged_Pre]

    if (sliderThatWasMoved == m_cameraAltitude)
    {
        //[UserSliderCode_m_cameraAltitude] -- add your slider handling code here..
		String s = String((int)(0.5+std::pow(10.0, sliderThatWasMoved->getValue())));
		m_labelCameraAltitude->setText(s, juce::NotificationType::sendNotification);
        //[/UserSliderCode_m_cameraAltitude]
    }
    else if (sliderThatWasMoved == m_CameraTilt)
    {
        //[UserSliderCode_m_CameraTilt] -- add your slider handling code here..
		String s = String((int)(0.5+sliderThatWasMoved->getValue()));
		m_labelCameraTilt->setText(s, juce::NotificationType::sendNotification);
        //[/UserSliderCode_m_CameraTilt]
    }

    //[UsersliderValueChanged_Post]
    //[/UsersliderValueChanged_Post]
}

void MainComponent::buttonClicked (Button* buttonThatWasClicked)
{
    //[UserbuttonClicked_Pre]
    //[/UserbuttonClicked_Pre]

    if (buttonThatWasClicked == m_cameraAutoHeadingEnable)
    {
        //[UserButtonCode_m_cameraAutoHeadingEnable] -- add your button handler code here..
		m_kml->setCameraAutoHeading(buttonThatWasClicked->getToggleState());
        //[/UserButtonCode_m_cameraAutoHeadingEnable]
    }
    else if (buttonThatWasClicked == m_cameraAutoRollEnable)
    {
        //[UserButtonCode_m_cameraAutoRollEnable] -- add your button handler code here..
		m_kml->setCameraAutoRoll(buttonThatWasClicked->getToggleState());
        //[/UserButtonCode_m_cameraAutoRollEnable]
    }
    else if (buttonThatWasClicked == m_cameraAutoFlyToView)
    {
        //[UserButtonCode_m_cameraAutoFlyToView] -- add your button handler code here..
		m_kml->setCameraFlyToView(buttonThatWasClicked->getToggleState());
        //[/UserButtonCode_m_cameraAutoFlyToView]
    }
    else if (buttonThatWasClicked == m_textButtonStatisticsLearn)
    {
        //[UserButtonCode_m_textButtonStatisticsLearn] -- add your button handler code here..
		m_fsLink->statisticsLearnStart(m_labelDumpOffset->getText().getIntValue());
        //[/UserButtonCode_m_textButtonStatisticsLearn]
    }
    else if (buttonThatWasClicked == m_textButtonStatisticsEval)
    {
        //[UserButtonCode_m_textButtonStatisticsEval] -- add your button handler code here..
		m_fsLink->statisticsEval();
        //[/UserButtonCode_m_textButtonStatisticsEval]
    }

    //[UserbuttonClicked_Post]
    //[/UserbuttonClicked_Post]
}

void MainComponent::labelTextChanged (Label* labelThatHasChanged)
{
    //[UserlabelTextChanged_Pre]
    //[/UserlabelTextChanged_Pre]

    if (labelThatHasChanged == m_labelCameraAltitude)
    {
        //[UserLabelCode_m_labelCameraAltitude] -- add your label text handling code here..
		m_kml->setCameraAltitude(labelThatHasChanged->getText().getFloatValue());
		if (labelThatHasChanged->getText().getFloatValue() < 1.0)
		{
			labelThatHasChanged->getText("1.0");
		}
		m_cameraAltitude->setValue(std::log10(labelThatHasChanged->getText().getFloatValue()));
        //[/UserLabelCode_m_labelCameraAltitude]
    }
    else if (labelThatHasChanged == m_labelCameraTilt)
    {
        //[UserLabelCode_m_labelCameraTilt] -- add your label text handling code here..
		m_kml->setCameraTilt(labelThatHasChanged->getText().getFloatValue());
		m_CameraTilt->setValue(labelThatHasChanged->getText().getFloatValue());
        //[/UserLabelCode_m_labelCameraTilt]
    }
    else if (labelThatHasChanged == m_labelDumpOffset)
    {
        //[UserLabelCode_m_labelDumpOffset] -- add your label text handling code here..
		m_fsLink->setDumpOffset(labelThatHasChanged->getText().getHexValue32());
        //[/UserLabelCode_m_labelDumpOffset]
    }

    //[UserlabelTextChanged_Post]
    //[/UserlabelTextChanged_Post]
}



//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
void MainComponent::onStateChanged(IFsLink &obj)
{
	m_fsuipcStatus->setText(obj.getLinkState(), juce::sendNotification);
	m_flightStatus->setText(obj.getFlightState(), juce::sendNotification);

	Colour c = juce::Colours::red;
	float alpha = 0.5f;
	if (String(obj.getLinkState()) == String("Connected"))
	{
		c = juce::Colours::blue;
	}
	else if (String(obj.getLinkState()) == String("Paused"))
	{
		c = juce::Colours::orange;
	}
	else if (String(obj.getLinkState()) == String("Recording"))
	{
		c = juce::Colours::limegreen;
	}
	m_fsuipcStatus->setColour (Label::backgroundColourId, c.withAlpha(alpha));

	c = juce::Colours::red;
	if (String(obj.getFlightState()) == String("Ground"))
	{
		c = juce::Colours::brown;
	}
	else if (String(obj.getFlightState()) == String("Touch down"))
	{
		c = juce::Colours::brown;
	}
	else if (String(obj.getFlightState()) == String("Take off"))
	{
		c = juce::Colours::skyblue;
	}
	else if (String(obj.getFlightState()) == String("Enroute"))
	{
		c = juce::Colours::skyblue;
	}
	m_flightStatus->setColour (Label::backgroundColourId, c.withAlpha(alpha));
}

void MainComponent::onDataChanged(IFsLink &obj)
{
	(void)obj;
}

void MainComponent::changeListenerCallback(juce::ChangeBroadcaster *source)
{
}

//[/MiscUserCode]


//==============================================================================
#if 0
/*  -- Introjucer information section --

    This is where the Introjucer stores the metadata that describe this GUI layout, so
    make changes in here at your peril!

BEGIN_JUCER_METADATA

<JUCER_COMPONENT documentType="Component" className="MainComponent" componentName=""
                 parentClasses="public Component, public IFsLinkListener, public ChangeListener"
                 constructorParams="String const &amp;homeDir, String const &amp;hostIp"
                 variableInitialisers="" snapPixels="8" snapActive="1" snapShown="1"
                 overlayOpacity="0.330" fixedSize="1" initialWidth="300" initialHeight="300">
  <BACKGROUND backgroundColour="ffffffff">
    <TEXT pos="257 32 23 24" fill="solid: ff000000" hasStroke="0" text="[m]"
          fontname="Default font" fontsize="15" bold="0" italic="0" justification="33"/>
    <TEXT pos="257 64 23 24" fill="solid: ff000000" hasStroke="0" text="[&#176;]"
          fontname="Default font" fontsize="15" bold="0" italic="0" justification="33"/>
  </BACKGROUND>
  <LABEL name="FSUIP Status" id="a891503ff0f699d7" memberName="m_fsuipcStatus"
         virtualName="" explicitFocusOrder="0" pos="24 232 104 40" tooltip="FSUIP Status"
         bkgCol="0" edTextCol="ff000000" edBkgCol="0" labelText="Unknown"
         editableSingleClick="0" editableDoubleClick="0" focusDiscardsChanges="0"
         fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
  <LABEL name="Flight Status" id="2a046c84c0638c1f" memberName="m_flightStatus"
         virtualName="" explicitFocusOrder="0" pos="168 232 104 40" tooltip="Flight Status"
         edTextCol="ff000000" edBkgCol="0" labelText="Unknown" editableSingleClick="0"
         editableDoubleClick="0" focusDiscardsChanges="0" fontname="Default font"
         fontsize="15" bold="0" italic="0" justification="36"/>
  <SLIDER name="Camera Altitude" id="3352cbc6aebbcc9e" memberName="m_cameraAltitude"
          virtualName="" explicitFocusOrder="0" pos="24 32 144 24" tooltip="Camera Altitude"
          min="0.10000000000000001" max="7" int="0.01" style="LinearHorizontal"
          textBoxPos="NoTextBox" textBoxEditable="1" textBoxWidth="80"
          textBoxHeight="20" skewFactor="1"/>
  <SLIDER name="Camera Tilt" id="4993af2027f11be1" memberName="m_CameraTilt"
          virtualName="" explicitFocusOrder="0" pos="24 64 144 24" tooltip="Camera Tilt"
          min="-90" max="90" int="5" style="LinearHorizontal" textBoxPos="NoTextBox"
          textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
  <TOGGLEBUTTON name="Camera Auto Heading" id="97a6fc7c399da63d" memberName="m_cameraAutoHeadingEnable"
                virtualName="" explicitFocusOrder="0" pos="24 112 176 24" tooltip="Camera Auto Heading"
                buttonText="Camera Auto Heading" connectedEdges="0" needsCallback="1"
                radioGroupId="0" state="0"/>
  <TOGGLEBUTTON name="Camera Auto Roll" id="cc847f94257e9212" memberName="m_cameraAutoRollEnable"
                virtualName="" explicitFocusOrder="0" pos="24 144 176 24" tooltip="Camera Auto Roll"
                buttonText="Camera Auto Roll" connectedEdges="0" needsCallback="1"
                radioGroupId="0" state="0"/>
  <TOGGLEBUTTON name="Camera Auto FlyToView" id="7c8f89a74ece3ccb" memberName="m_cameraAutoFlyToView"
                virtualName="" explicitFocusOrder="0" pos="24 176 176 24" tooltip="Camera Auto FlyToView"
                buttonText="Camera Auto FlyToView" connectedEdges="0" needsCallback="1"
                radioGroupId="0" state="0"/>
  <LABEL name="Camera Altitude" id="22b0982961c62c7f" memberName="m_labelCameraAltitude"
         virtualName="" explicitFocusOrder="0" pos="168 32 88 24" tooltip="Camera Altitude in meter"
         edTextCol="ff000000" edBkgCol="0" labelText="0" editableSingleClick="1"
         editableDoubleClick="1" focusDiscardsChanges="0" fontname="Default font"
         fontsize="15" bold="0" italic="0" justification="34"/>
  <LABEL name="Camera Tilt" id="a2fedc79e61668d0" memberName="m_labelCameraTilt"
         virtualName="" explicitFocusOrder="0" pos="168 64 88 24" tooltip="Camera Tilt in degrees"
         edTextCol="ff000000" edBkgCol="0" labelText="0" editableSingleClick="1"
         editableDoubleClick="1" focusDiscardsChanges="0" fontname="Default font"
         fontsize="15" bold="0" italic="0" justification="34"/>
  <LABEL name="Label About" id="a9e9b977874f4753" memberName="m_labelAbout"
         virtualName="" explicitFocusOrder="0" pos="272 0 24 24" edTextCol="ff000000"
         edBkgCol="0" labelText="" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <LABEL name="Label Dump Offset" id="8a8ce24b4c3ef02b" memberName="m_labelDumpOffset"
         virtualName="" explicitFocusOrder="0" pos="200 112 72 24" edTextCol="ff000000"
         edBkgCol="0" labelText="0" editableSingleClick="1" editableDoubleClick="1"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="34"/>
  <TEXTBUTTON name="Button StatisticsLearn" id="11fb0e356e3867b1" memberName="m_textButtonStatisticsLearn"
              virtualName="" explicitFocusOrder="0" pos="224 144 64 24" buttonText="Learn"
              connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  <TEXTBUTTON name="Button StatisticsEval" id="6acf7f6a370fc3af" memberName="m_textButtonStatisticsEval"
              virtualName="" explicitFocusOrder="0" pos="224 176 64 24" buttonText="Eval"
              connectedEdges="0" needsCallback="1" radioGroupId="0"/>
</JUCER_COMPONENT>

END_JUCER_METADATA
*/
#endif


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