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

    reprodukce.h
    Created: 11 Mar 2023 3:29:41pm
    Author:  Tomáš Pospíšil

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


/*
* This file handles visuals of this project. The main goal is to fill the array
* pole with numbers of channels. With the timerCallback the varaible on the certain 
* index of this array is read and based on this certain TextButton representing the
* channel its background is changed. 
*/

#pragma once

#include "obsluhaGranuli.h"

class visualization : public handleGrain {
public:
    /*
    * Variables of this class. ToggleButtons are for reproduction algorithm determination.
    * TextButtons represents 8 channels of the reproduction system. Bool variables handles
    * the coordination between multiple states.
    */
    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;

    bool showPat = false;
    bool first = false;
    int pole[10];
    int last;

    /*
    * Function calls the setNeeded function based on the ToggleButton that is toggled.
    */
    void showPattern() {
        if (hodiny.getToggleState() == true || (hvezda.getToggleState() == true) || (kruh.getToggleState() == true)) {
            setNeeded(8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false);
        }
        else if (pulkruhL.getToggleState() == true) {
            setNeeded(9, 5, 3, 2, 1, 0, 7, 0, 0, 0, 0, 0, true);
        }
        else if (pulkruhR.getToggleState() == true) {
            setNeeded(9, 5, 7, 0, 1, 2, 3, 0, 0, 0, 0, 0, true);
        }
        else if (kruh2.getToggleState() == true) {
            setNeeded(17, 9, 5, 6, 7, 0, 1, 2, 3, 4, 5, 0, true);
        }
        else
            return;
    }


    /*
    * Function reads the pole array and handles the repainting of the TextButtons.
    * This repainting is based on array indexes incrementation.
    */
    void setNeeded(int fst, int sec, int n,int j,int d, int t,int c, int p, int s,int se, int o, int de, bool state) {
        if (true) {
            for (int i = 0; i < 8; i++) {
                odbarviButton(pole[i]);
            }
        }
        
        
        if (last > fst) {
            if(state)
                odbarviButton(pole[last - (sec+1)]);
            else
                odbarviButton(pole[last - 1]);
            showPat = false;
            return;
        }
        else {
            if (last >= sec) {
                naplnPole(n, j, d, t, c, p, s, se, o, de);
                obarviButton(pole[last - sec], 179, 49, 0);
            }
            else
                obarviButton(pole[last], 255, 153, 51);
            if (last > 0 && last < sec)
                odbarviButton(pole[last - 1]);
            else if (last > sec) {
                odbarviButton(pole[last - (sec+1)]);
            }
            else {
                last++;
                return;
            }
            last++;
        }
    }

    /*
    * Function that stores parameters from function's head into pole array.
    */
    void naplnPole(int j, int d, int t, int c, int p, int s, int se, int o, int dev, int des) {
        pole[0] = j;
        pole[1] = d;
        pole[2] = t;
        pole[3] = c;
        pole[4] = p;
        pole[5] = s;
        pole[6] = se;
        pole[7] = o;
        pole[8] = dev;
        pole[9] = des;
    }

    /*
    * Function, that repaints certain TextButton representing channel. Parameters of this function
    * are the number of channel and the RGB nubers.
    * 
    * @param cislo      The number of channel, which colour is meant to be repainted.
    * @param r          The number for RED colour
    * @param g          The number for GREEN colour
    * @param b          The number for BLUE colour
    */
    void obarviButton(int cislo,int r, int g, int b) {
        switch (cislo)
        {
        case 1:
            chanel1.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r,g,b));
            break;
        case 2:
            chanel2.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        case 3:
            chanel3.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        case 4:
            chanel4.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        case 5:
            chanel5.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        case 6:
            chanel6.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        case 7:
            chanel7.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        case 0:
            chanel8.setColour(juce::TextButton::buttonColourId, juce::Colour::fromRGB(r, g, b));
            break;
        default:
            break;
        }
    }


    /*
    * Function, that sets certain TextButton representing the channel to its initial state.
    * 
    * @param cislo      The number of channel, which is suppoused to be repainted. 
    * */
    void odbarviButton(int cislo) {
        switch (cislo)
        {
        case 1:
            chanel1.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 2:
            chanel2.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 3:
            chanel3.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 4:
            chanel4.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 5:
            chanel5.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 6:
            chanel6.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 7:
            chanel7.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        case 0:
            chanel8.setColour(juce::TextButton::buttonColourId, juce::Colours::darkgrey);
            break;
        default:
            break;
        }
    }


    /*
    * This function fills the pole array with expected values. It also starts the timer with
    * the goal to change the colour of the TextButtons representing the channels in time.
    * 
    * @param time       Time in miliseconds. It is the length of the Timer cycle.
    */
    void showPatternClicked(int time) {        
        startTimer(time);
        last = 0;
        if (hvezda.getToggleState() == true) {
            naplnPole(1, 4, 7, 2, 5, 0, 3, 6, 1, 0);
        }
        else if (kruh.getToggleState() == true) {
            naplnPole(1, 2, 3, 4, 5, 6, 7, 0, 1, 0);
        }
        else if (hodiny.getToggleState() == true) {
            naplnPole(2, 6, 4, 0, 2, 6, 4, 0, 2, 0);
        }
        else if (pulkruhL.getToggleState() == true) {
            naplnPole(7, 0, 1, 2, 3, 0, 0, 0, 0, 0);
        }
        else if (pulkruhR.getToggleState() == true) {
            naplnPole(3, 2, 1, 0, 7, 7, 0, 1, 2, 3);
        }
        else if (kruh2.getToggleState() == true) {
            naplnPole(5, 4, 3, 2, 1, 0, 7, 6, 5, 0);
            last = 0;
        }
        else
            return;
        showPat = true;
    }
};
