/*******************************************************************************
*   @author Tomáš Pospíšil
*   
*   Tento kód byl sestaven na základì tutoriálu, dostupného na adrese:
*   https://docs.juce.com/master/tutorial_playing_sound_files.html a 
*   https://docs.juce.com/master/tutorial_audio_thumbnail.html . 
*   
*  ==============================================================================

   Some of the code is part of the JUCE tutorials.
   Copyright (c) 2020 - Raw Material Software Limited

   The code included in this file is provided under the terms of the ISC license
   http://www.isc.org/downloads/software-support-policy/isc-license. Permission
   To use, copy, modify, and/or distribute this software for any purpose with or
   without fee is hereby granted provided that the above copyright notice and
   this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
   WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
   PURPOSE, ARE DISCLAIMED.

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

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

    This file contains the basic framework code for a JUCE plugin editor.

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

#pragma once

#include <JuceHeader.h>
#include "PluginProcessor.h"

//==============================================================================
/**
*/
class BrowsefileAudioProcessorEditor  : public juce::AudioProcessorEditor, 
                                        public juce::AudioAppComponent, 
                                        public juce::ChangeListener,
                                        public juce::FileDragAndDropTarget,
                                        private juce::Timer,
                                        public juce::Slider

{
public:
    BrowsefileAudioProcessorEditor (BrowsefileAudioProcessor&);
    ~BrowsefileAudioProcessorEditor() override;

    //==============================================================================

    /*
    * Class functions declarations.
    */
    void paint (juce::Graphics&) override;
    void resized() override;
    void prepareToPlay(int samplesPerBlockExpected, double sampleRate)override;
    void getNextAudioBlock(const juce::AudioSourceChannelInfo& bufferToFill)override;
    void releaseResources() override;
    void changeListenerCallback(juce::ChangeBroadcaster* source) override;
    bool isInterestedInFileDrag(const juce::StringArray& files) override;
    void filesDropped(const juce::StringArray& files, int x, int y) override;
    void loadFile(juce::String path);
    void prepareForPlaying(juce::File file);
    void paintIfNoFileLoaded(juce::Graphics& g, const juce::Rectangle<int>& thumbnailBounds);
    void paintIfFileLoaded(juce::Graphics& g, const juce::Rectangle<int>& thumbnailBounds);
    


private:
    BrowsefileAudioProcessor& audioProcessor;
    
    /*
    * Enums for state of audio player to be changed
    */
    enum TranssportState {
        Starting,
        Stoping,
        Stoped,
        Playing
    };

    TranssportState state;
    
    /*
    * Class functions declarations.
    */
    void labelInit();
    void browseButtonClicked();
    void playButtonClicked();
    void stopButtonClicked();
    void stateChanged(TranssportState newState);
    void timerCallback() override;
    void mouseDown(const juce::MouseEvent& event) override;
    
    
    /*
    * Class objects and variables
    */
    juce::Label              infoLabel;
    juce::TextButton         textButton;
    juce::TextButton         playButton;
    juce::TextButton         stopButton;
    
    juce::AudioFormatManager                       formatManager;
    std::unique_ptr<juce::FileChooser>             chooser;
    std::unique_ptr<juce::AudioFormatReaderSource> readerSource;
    
    juce::AudioTransportSource                     transportSource;
    juce::AudioThumbnailCache                      thumbnailCache;
    juce::AudioThumbnail                           thumbnail;


    juce::GroupComponent group;
    juce::Label midiInputLabel;
    juce::ComboBox midiInput;
    juce::GroupComponent group2;

    juce::Label duration;
    juce::Label envelope;
    juce::Label density;
    juce::Label timeOffset;

    juce::Slider delka;
    juce::Slider obalka;
    juce::Slider pocet;
    juce::Slider offset;

    juce::GroupComponent group3;
    juce::ToggleButton kanal1;
    juce::ToggleButton kanal2;
    juce::ToggleButton kanal3;
    juce::ToggleButton kanal4;
    juce::ToggleButton kanal5;
    juce::ToggleButton kanal6;
    juce::ToggleButton kanal7;
    juce::ToggleButton kanal8;

    juce::TextButton playGrain;
    juce::TextButton makeGrain;
    juce::TextButton resetGrain;
    juce::TextButton info;

    juce::GroupComponent group4;
    juce::ToggleButton hvezda;
    juce::ToggleButton kruh;
    juce::ToggleButton kruh2;
    juce::ToggleButton pulkruhL;
    juce::ToggleButton pulkruhR;
    juce::ToggleButton hodiny;
    juce::TextButton showpattern;

    juce::GroupComponent group5;
    juce::TextButton chanel1;
    juce::TextButton chanel2;
    juce::TextButton chanel3;
    juce::TextButton chanel4;
    juce::TextButton chanel5;
    juce::TextButton chanel6;
    juce::TextButton chanel7;
    juce::TextButton chanel8;

    juce::MidiKeyboardState keyboardState;
    juce::MidiKeyboardComponent keyboardComponent;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BrowsefileAudioProcessorEditor)
};
