/** 
 * @defgroup thresh thresh
 * threshold gui routines
 * @{
 */

#include "main.h"
#include "prefs.h"
#include "source.h"
#include "sensor.h"
#include "gui.h"
#include "output.h"
#include "thresh.h"


/** local info structure */
static struct Thresh
{
    gboolean do_threshold;              /** draw threshold-preview */
}thresh;



/********************* getter/setter for local variables *********************/
/**
 * do_threshold getter
 */
gboolean thresh_get_do()
{
    return thresh.do_threshold;
}

/**
 * do_threshold setter
 */
void thresh_set_do(gboolean t)
{
    thresh.do_threshold = t;
}

/*********************************************************/
void thresh_frame(unsigned char *src, unsigned char *dst,
                     int size, double threshold, unsigned char R,
                     unsigned char G, unsigned char B)
{
    int count;
    /*
     * double per_r, per_g, per_b, sum; double min_r, min_g, min_b, max_r,
     * max_g, max_b, cmp_sum;
     */
    /*
     * calc reference 
     */
    /*
     * cmp_sum = (double) (R+G+B) / 100; min_r = (double) R / cmp_sum; min_g
     * = (double) G / cmp_sum; min_b = (double) B / cmp_sum; max_r = min_r +
     * min_r*threshold; max_g = min_g + min_g*threshold; max_b = min_b +
     * min_b*threshold; min_r -= min_r * threshold; min_g -= min_g *
     * threshold; min_b -= min_b * threshold;
     */
    /*
     * walk all data 
     */
    for(count = 0; count < size; count += 3) {
        if((src[count] < R + (unsigned char) (255 * threshold)) &&
           (src[count] > R - (unsigned char) (255 * threshold)) &&
           (src[count + 1] < G + (unsigned char) (255 * threshold)) &&
           (src[count + 1] > G - (unsigned char) (255 * threshold)) &&
           (src[count + 2] < B + (unsigned char) (255 * threshold)) &&
           (src[count + 2] > B - (unsigned char) (255 * threshold)))

            /*
             * sum = (double) (data[count] + data[count+1] + data[count+2]) / 
             * 100; per_r = (double) data[count] / sum; per_g = (double)
             * data[count+1] / sum; per_b = (double) data[count+2] / sum;
             * 
             * if((per_r <= max_r && per_r >= min_r) && (per_g <= max_g &&
             * per_g >= min_g) && (per_b <= max_b && per_b >= min_b))
             */
        {
            dst[count] = 0xFF;
            dst[count + 1] = 0xFF;
            dst[count + 2] = 0xFF;
        }
        else {
            dst[count] = 0;
            dst[count + 1] = 0;
            dst[count + 2] = 0;
        }
    }
}

/************************** GTK-Callbacks **************************/
/**
 * ColorSelector used to change threshold color
 */
gboolean cb_color_set(GtkWidget * widget)
{
    GdkColor color;

    _VERBOSE2 printf("%s()\n", __func__);
    
    if(src_get() && sensor_get())
    {
        
        /*
         * get color 
         */
        gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), &color);
    
        /*
         * set RGB of current sensor 
         */
        sensor_get()->thr_R = color.red * 255 / 65535;
        sensor_get()->thr_G = color.green * 255 / 65535;
        sensor_get()->thr_B = color.blue * 255 / 65535;
    
        _VERBOSE3 printf("cb_color_set(): R: %d G: %d B: %d\n",
                         sensor_get()->thr_R,
                         sensor_get()->thr_G,
                         sensor_get()->thr_B);
    
        /*
         * redraw 
         */
        src_get()->draw_dst = TRUE;
    }
    else
    {
        _ERROR printf("%s(): NULL-pointer received!\n", __func__);
    }
    return TRUE;
}

/**
 * Preview threshold togglebutton toggled
 */
gboolean cb_do_threshold(GtkWidget * widget)
{
    gboolean state;
    
    _VERBOSE2 printf("%s()\n", __func__);
    
    if(src_get() && sensor_get()) {
    
        state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
        
        /* save state */
        thresh_set_do(state);

        /* disable "laser-detected" icon */
        gtk_widget_hide(glade_xml_get_widget(gui_get_xml(),
                        "image_yes"));
        gtk_widget_show(glade_xml_get_widget(gui_get_xml(),
                        "image_no"));
        
        gtk_widget_set_sensitive(glade_xml_get_widget(gui_get_xml(),
                                "hbox_laserdetected"), state);
        
        /*
         * redraw 
         */
        src_get()->draw_dst = TRUE;
    }
    else
    {
        _ERROR printf("%s(): NULL-pointer received!\n", __func__);
    }
    
    return TRUE;
}

/**
 * Threshold slider moved
 */
gboolean cb_threshold_changed(GtkWidget * widget)
{
    double value = (double) gtk_range_get_value(GTK_RANGE(widget));
    
    
    _VERBOSE2 printf("%s()\n", __func__);
    
    if(src_get())
    {
        
        if(value > 0)
            sensor_get()->thr_threshold = value;
        /*
         * redraw output 
         */
        src_get()->draw_dst = TRUE;
        
        
    }
    
    return TRUE;
}



/************************** Module **************************/
/**
 * initialize threshold-stuff once
 */
int thresh_init()
{
    memset(&thresh, 0, sizeof(struct Thresh));
    
    return 0;
}

/**
 * update threshold-gui stuff
 */
void thresh_gui_update()
{
    GdkColor color;
    
    
    /*
     * set sensor stuff 
     */
    if(sensor_get()) {
 
        
        /*
         * threshold 
         */
        gtk_range_set_value(GTK_RANGE
                            (glade_xml_get_widget
                             (gui_get_xml(), "hscale_threshold")),
                            sensor_get()->thr_threshold);

        /*
         * set threshold color-selector 
         */
        color.red = (sensor_get()->thr_R) *65535 / 255;
        color.green = (sensor_get()->thr_G) *65535 / 255;
        color.blue = (sensor_get()->thr_B) *65535 / 255;

        gtk_color_button_set_color(GTK_COLOR_BUTTON
                                   (glade_xml_get_widget
                                    (gui_get_xml(),
                                     "colorbutton_thresh")), &color);
                                     
        /* disable/enable laser-detected icon */
        gtk_widget_set_sensitive(glade_xml_get_widget(gui_get_xml(),
                        "hbox_laserdetected"), thresh_get_do());
    }    
}

/**
 * initialize threshold-gui
 */
int thresh_gui_init()
{
    /*g_signal_connect(G_OBJECT
                     (glade_xml_get_widget
                      (gui_get_xml(), "togglebutton_threshold")),
                     "toggled", G_CALLBACK(cb_do_threshold), NULL);

    g_signal_connect(G_OBJECT
                     (glade_xml_get_widget
                      (gui_get_xml(), "hscale_threshold")),
                     "value-changed", G_CALLBACK(cb_threshold_changed), NULL);

    g_signal_connect(G_OBJECT
                         (glade_xml_get_widget
                          (gui_get_xml(), "colorbutton_thresh")), "color-set",
                         G_CALLBACK(cb_color_set), NULL);*/
    
    return 0;
}

/** 
 * @} 
 */
