#include "libublsettingsui-gtk3.h"

#define service_start_icon_name "com.ublinux.libublsettingsui-gtk3.play-symbolic"
#define service_stop_icon_name "com.ublinux.libublsettingsui-gtk3.stop-symbolic"

#define service_start_status_icon_name "com.ublinux.libublsettingsui-gtk3.active-symbolic"
#define service_stop_status_icon_name "com.ublinux.libublsettingsui-gtk3.inactive-symbolic"

void __on_closed(GtkWidget *, service_window *window){
    free(window->name);
    free(window->start_command);
    free(window->stop_command);
    free(window->enable_command);
    free(window->disable_command);
    free(window->info_command);
    free(window->check_active_command);
    free(window->check_enable_command);
    free(window);
    gtk_main_quit();
}

void __on_service_update_clicked(GtkWidget *, service_window *window);

void __on_service_start_clicked(GtkWidget *, service_window *window){
        if (system(yon_debug_output("%s\n",window->start_command))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
        };
    if (window->check_enable_command&&window->check_active_command){
        __on_service_update_clicked(NULL,window);
    }

}

void __on_service_stop_clicked(GtkWidget *, service_window *window){
    if (system(yon_debug_output("%s\n",window->stop_command))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
        };
    if (window->check_enable_command&&window->check_active_command){
        __on_service_update_clicked(NULL,window);
    }

}

void __on_service_enable_clicked(GtkWidget *, service_window *window){
        if (system(yon_debug_output("%s\n",window->enable_command))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
        };
    if (window->check_enable_command&&window->check_active_command){
        __on_service_update_clicked(NULL,window);
    }

}

void __on_service_disable_clicked(GtkWidget *, service_window *window){
    if (system(yon_debug_output("%s\n",window->disable_command))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
        };
    if (window->check_enable_command&&window->check_active_command){
        __on_service_update_clicked(NULL,window);
    }
}

void __on_service_restart_clicked(GtkWidget *, service_window *window){


    if (system(yon_debug_output("%s\n",window->stop_command))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
        };
    if (system(yon_debug_output("%s\n",window->start_command))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
        };
    if (window->check_enable_command&&window->check_active_command){
        __on_service_update_clicked(NULL,window);
    }
}

void __on_service_status_clicked(GtkWidget *, service_window *window){
    char *success_string = _SERVICE_ACTIVE_STATUS_LABEL(window->name);
    char *fail_string = _SERVICE_INACTIVE_STATUS_LABEL(window->name);

    yon_terminal_window_update_button_launch(GTK_WINDOW(window->MainWindow),window->info_command,success_string,fail_string);

}

void __on_service_update_clicked(GtkWidget *, service_window *window){
    if (!system(yon_debug_output("%s\n",window->check_enable_command))){
        gtk_label_set_text(GTK_LABEL(window->EnableLabel),yon_char_get_localised_from_lib(_SERVICE_ENABLE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->EnableImage),service_start_status_icon_name,GTK_ICON_SIZE_BUTTON);
    } else {
        gtk_label_set_text(GTK_LABEL(window->EnableLabel),yon_char_get_localised_from_lib(_SERVICE_DISABLE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->EnableImage),service_stop_status_icon_name,GTK_ICON_SIZE_BUTTON);

    }
    if (!system(yon_debug_output("%s\n",window->check_active_command))){
        gtk_label_set_text(GTK_LABEL(window->ActiveLabel),yon_char_get_localised_from_lib(_SERVICE_ACTIVE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->ActiveImage),service_start_status_icon_name,GTK_ICON_SIZE_BUTTON);

    } else {
        gtk_label_set_text(GTK_LABEL(window->ActiveLabel),yon_char_get_localised_from_lib(_SERVICE_INACTIVE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->ActiveImage),service_stop_status_icon_name,GTK_ICON_SIZE_BUTTON);

    }
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(SUCCESS_LABEL),5,BACKGROUND_IMAGE_SUCCESS_TYPE);
}

service_window *yon_service_window_new(char *name, char *start_command,
                                        char *stop_command, char *enable_command,
                                        char *disable_command, char *info_command,
                                        char *check_active_command, char *check_enable_command){
    service_window *window = new(service_window);
    memset(window,0,sizeof(service_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(ui_glade_path_service);
    window->MainWindow = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");

    window->UpdateButton = yon_gtk_builder_get_widget(builder,"UpdateButton");
    window->StatusButton = yon_gtk_builder_get_widget(builder,"StatusButton");

    window->ActiveLabel = yon_gtk_builder_get_widget(builder,"ActiveLabel");
    window->ActiveImage = yon_gtk_builder_get_widget(builder,"ActiveImage");
    window->EnableLabel = yon_gtk_builder_get_widget(builder,"EnableLabel");
    window->EnableImage = yon_gtk_builder_get_widget(builder,"EnableImage");

    window->RestartButton = yon_gtk_builder_get_widget(builder,"RestartButton");
    window->StartButton = yon_gtk_builder_get_widget(builder,"StartButton");
    window->StopButton = yon_gtk_builder_get_widget(builder,"StopButton");
    window->EnableButton = yon_gtk_builder_get_widget(builder,"EnableButton");
    window->DisableButton = yon_gtk_builder_get_widget(builder,"DisableButton");

    window->name = yon_char_new(name);
    window->start_command = yon_char_new(start_command);
    window->stop_command = yon_char_new(stop_command);
    window->enable_command = yon_char_new(enable_command);
    window->disable_command = yon_char_new(disable_command);
    window->info_command = yon_char_new(info_command);
    window->check_active_command = yon_char_new(check_active_command);
    window->check_enable_command = yon_char_new(check_enable_command);

        char *head_label = (_SERVICE_HEAD_LABEL(window->name));
        char *title_label = (_SERVICE_TITLE_LABEL(window->name));
    gtk_label_set_text(GTK_LABEL(window->HeadLabel),head_label);
    gtk_label_set_text(GTK_LABEL(window->TitleLabel),title_label);

    yon_gtk_window_setup(GTK_WINDOW(window->MainWindow),NULL,_SERVICE_HEAD_LABEL(window->name),yon_dull_icon_path,NULL);

    g_signal_connect(G_OBJECT(window->MainWindow),"destroy",G_CALLBACK(__on_closed),window);
    if (window->start_command){
        g_signal_connect(G_OBJECT(window->StartButton),"clicked",G_CALLBACK(__on_service_start_clicked),window);
    }
    if (window->stop_command){
        g_signal_connect(G_OBJECT(window->StopButton),"clicked",G_CALLBACK(__on_service_stop_clicked),window);
    }
    if (window->stop_command&&window->start_command){
        g_signal_connect(G_OBJECT(window->RestartButton),"clicked",G_CALLBACK(__on_service_restart_clicked),window);
    }
    if (window->info_command){
        g_signal_connect(G_OBJECT(window->StatusButton),"clicked",G_CALLBACK(__on_service_status_clicked),window);
    }
    if (window->check_enable_command&&window->check_active_command){
        g_signal_connect(G_OBJECT(window->UpdateButton),"clicked",G_CALLBACK(__on_service_update_clicked),window);
    }
    if (window->enable_command){
        g_signal_connect(G_OBJECT(window->EnableButton),"clicked",G_CALLBACK(__on_service_enable_clicked),window);
    }
    if (window->disable_command){
        g_signal_connect(G_OBJECT(window->DisableButton),"clicked",G_CALLBACK(__on_service_disable_clicked),window);
    }
    
    
    if (!system(window->check_enable_command)){
        gtk_label_set_text(GTK_LABEL(window->EnableLabel),yon_char_get_localised_from_lib(_SERVICE_ENABLE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->EnableImage),service_start_status_icon_name,GTK_ICON_SIZE_BUTTON);
    } else {
        gtk_label_set_text(GTK_LABEL(window->EnableLabel),yon_char_get_localised_from_lib(_SERVICE_DISABLE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->EnableImage),service_stop_status_icon_name,GTK_ICON_SIZE_BUTTON);

    }
    if (!system(window->check_active_command)){
        gtk_label_set_text(GTK_LABEL(window->ActiveLabel),yon_char_get_localised_from_lib(_SERVICE_ACTIVE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->ActiveImage),service_start_status_icon_name,GTK_ICON_SIZE_BUTTON);

    } else {
        gtk_label_set_text(GTK_LABEL(window->ActiveLabel),yon_char_get_localised_from_lib(_SERVICE_INACTIVE_LABEL));
        gtk_image_set_from_icon_name(GTK_IMAGE(window->ActiveImage),service_stop_status_icon_name,GTK_ICON_SIZE_BUTTON);

    }
    return window;
}

void yon_service_window_run(service_window *window){
    if (window){
        gtk_widget_show(window->MainWindow);
        gtk_main();
    }
}