/*******************************************************************************
**
** The source file for the Jahshaka effect module
** The Jahshaka Project
** Copyright (C) 2000-2005 The Jahshaka Project
** Released under the GNU General Public License
**
*******************************************************************************/

#include "effect.h"
#include <glcore.h>
#include <coreobject.h>
#include <qlistview.h>

void GLEffect::updateSliders()
{   
    jtrace->debug("in updateSliders");

    //need to keep track of last layer selected
    //vs current layer selected and layer types
    //as if they havent changed we dont need to hide and reshow the sliders
    //we just need to update values...

    EffectLayer* effect_layer = NULL;

    //hide everything
    hideSliders(10);

    hideAllHeadings();

    if ( activeLayerIsAnEffect() )
    {
        effect_layer = getActiveEffectLayer();
  
        if(effect_layer != NULL && effect_layer->getEffectInfo()->getType() == EffectInfo::JAH_PLUGIN_TYPE)
	    {
            jtrace->debug( "GLAnime","updating plugin slider display");
            //plugins currently have a lot of ui control

            //get the number of the plugin we are dealing with
            int plugFx = effect_layer->getPluginNumber();

            ///////////////////////////////////////////////////////////////
            //first we do the slider labels!
            bool us  = jplugin->vfxPlugins->at(plugFx)->getUseSliders(); //

            if (us)
            {
                int  ns  = jplugin->vfxPlugins->at(plugFx)->getNumberSliders(); //      3;

                for (int is=0; is<ns; is++)
                {
                    setSliderLabelValue(is, jplugin->vfxPlugins->at(plugFx)->getSliderLabel(is));
                }

                showSliders(ns);
            }

            ///////////////////////////////////////////////////////////////
            //first we do the group labels!
            bool ul  = jplugin->vfxPlugins->at(plugFx)->getUseLabels(); //

            if (ul)
            {
                int  nl  = jplugin->vfxPlugins->at(plugFx)->getNumberLabels();

                for (int il=0; il<nl; il++)
                {
                    setHeadingValue(il, jplugin->vfxPlugins->at(plugFx)->getLabel(il));
                }

                showHeadings(nl);
            }
        }
        else
        {
			//this means its a effect but its not a plugin
			//so we use the core set of sliders for now
			//we can pull up a color correction ui or keyer ui here
			//if we want to...
			//setDefaultSliderText();

            showSliders( (int)effect_layer->getSliderNameVector()->size() );
			//setDefaultHeadings();
			//showAllHeadings();
		}

	}
    else
	{
		//we need to differentiate between the world space 
		//and layer space now
		setDefaultSliderText();

		//if its the world layer we only show the first 6
		if (getActiveJahLayer() != camera )
		{
			showSliders(10);
			setDefaultHeadings();
			showAllHeadings();
		}
		else
		{        
			showSliders(5); //was 5
			setDefaultHeadings();
			showHeadings(2);
		}
	}

    //update the rest of the slider/ui data
    updateSliderValues();

    updateUiOptions();

    updateUiSettings();

}

//we override this here since the 'world' is now layer 1

void GLEffect::updateSliderValues()
{
    if ( !multipleLayersSelectedInListView() && getActiveJahLayer() )
    {
        motionNode* motion_node = getActiveJahLayer()->layernodes->m_node;

        /////////////////////////////////////////////////////////////////////////////
        //basic values here
        if (getActiveJahLayer() != camera )
        {
            setSliderValue( 0, (int)motion_node->rx );
            setSliderValue( 1, (int)motion_node->ry );
            setSliderValue( 2, (int)motion_node->rz );

            setSliderValue( 3, (int)motion_node->tx );
            setSliderValue( 4, (int)motion_node->ty );
            setSliderValue( 5, (int)motion_node->tz );

            setSliderValue( 6, getScaleValue(motion_node->sx)  );
            setSliderValue( 7, getScaleValue(motion_node->sy)  );
            setSliderValue( 8, getScaleValue(motion_node->sz)  );
            
            setSliderValue( 9, (int)motion_node->Alpha  );

        }

    }
	
}

void GLEffect::updateUiOptions()
{
	
    ////////////////////////////////////////////////////////////////////////////////////
    // all other ui settings

    //first hide em all
    hideOptions(numUiOptions-1);

    EffectLayer* effect_layer = NULL;

    if ( activeLayerIsAnEffect() )
    {
        effect_layer = getActiveEffectLayer();

        if(effect_layer != NULL && effect_layer->getEffectInfo()->getType() == EffectInfo::JAH_PLUGIN_TYPE)
        {
	        int plugin_number = effect_layer->getPluginNumber();
            int  nb  = jplugin->vfxPlugins->at(plugin_number)->getNumberButtons(); 

            for (int i=0; i<nb; i++)
            {
                JahModuleOption[i]->setText( jplugin->vfxPlugins->at(plugin_number)->getButton(i) );
                JahModuleOption[i]->setChecked( jplugin->vfxPlugins->at(plugin_number)->getOptionStatus(i) );
            //first we update the layer options
           }

            showOptions(nb-1);
        }
    } 
   

}

void GLEffect::updateUiSettings()
{
    VideoRenderSelect->setChecked(core->useimagesize);

    //JahModuleOption[0]->setChecked(getActiveJahLayer()->option1);
    //JahModuleOption[1]->setChecked(getActiveJahLayer()->option2);
    //JahModuleOption[2]->setChecked(getActiveJahLayer()->option3);
    //JahModuleOption[3]->setChecked(getActiveJahLayer()->option4);

}


void GLEffect::setOption1(void)
{
    if ( activeLayerIsAnEffect() )
    {
        parsePluginOption(1);
        updateEffects();
        updateGL();
    }
}

void GLEffect::setOption2(void)
{
    if ( activeLayerIsAnEffect() )
    {
        parsePluginOption(2);
        updateEffects();
        updateGL();
    }
}

void GLEffect::setOption3(void)
{
    if ( activeLayerIsAnEffect() )
    {
        parsePluginOption(3);
        updateEffects();
        updateGL();
    }
}

void GLEffect::setOption4(void)
{
    if ( activeLayerIsAnEffect() )
    {
        parsePluginOption(4);
        updateEffects();
        updateGL();
    }
}

//we use this to disconnect ui form the core
void GLEffect::changeVideoRender()
{
	bool status = VideoRenderSelect->isChecked();

	SetVideoRender(status);
}

void
GLEffect::changeRenderOnlyToSelectedEffect()
{
    bool flag = m_render_only_to_selected_effect_qcheckbox->isChecked();

    setRenderOnlyToSelectedEffect(flag);

    if ( getActiveJahLayer()->getParent() )
    {
        getActiveJahLayer()->getParent()->setEffectsSlidersHaveChanged(true);
    }

    updateGL();
}


