#include "ubl-settings-video.h"


/**void on_monitor_delete(GtkWidget *self,monitor_data windowd)
 * [EN]
*/
void on_monitor_delete(GtkWidget *,monitor_data *window){
    if(window){
        main_window *widgets = g_object_get_data(G_OBJECT(window->MainFlow),"widgets");
        GtkWidget *target = window->MainFlow;
        gtk_container_remove(GTK_CONTAINER(widgets->MonitorsBox),target);
        const char *target_monitor = gtk_label_get_text(GTK_LABEL(window->NameLabel));
        if (!yon_char_is_empty(target_monitor)){
            yon_config_remove_by_key(XORG_MONITOR((char*)target_monitor));
        }
        free(window);
    }
}

/**void on_monitor_switch(GtkWidget *self,monitor_data *window)
 * [EN]
*/
void on_monitor_switch(GtkWidget *,monitor_data *window){
    if(window){
        const char *name = gtk_label_get_text(GTK_LABEL(window->NameLabel));
        if (yon_char_is_empty(name)) return;
        char *parameter = config(XORG_MONITOR(name));
        int size;
        config_str parsed = yon_char_parse(parameter,&size,",");
        int found = yon_char_parsed_check_exist(parsed,size,"enable");
        if (found != -1){
            free(parsed[found]);
            parsed[found]=yon_char_new("disable");
                gtk_style_context_add_class(gtk_widget_get_style_context(gtk_widget_get_parent(window->MonitorInfoBox)),"displayunsens");
                gtk_widget_set_tooltip_text(window->SwitchButton,ENABLE_TOOLTIP_LABEL);
        } else {
            found = yon_char_parsed_check_exist(parsed,size,"disable");
            if (found !=-1){
                free(parsed[found]);
                parsed[found]=yon_char_new("enable");
            } else {
                parsed = yon_char_parsed_append(parsed,&size,"enable");
            }
                gtk_style_context_remove_class(gtk_widget_get_style_context(gtk_widget_get_parent(window->MonitorInfoBox)),"displayunsens");
                gtk_widget_set_tooltip_text(window->SwitchButton,DISABLE_TOOLTIP_LABEL);
        }

        char *final = yon_char_parsed_to_string(parsed,size,",");
        yon_config_register(XORG_MONITOR(name),XORG_MONITOR_get_command,final);
        yon_char_parsed_free(parsed,size);

    }
}

/**void on_monitor_add(GtkWidget *self,monitor_data *window)
 * [EN]
*/
void on_monitor_add(GtkWidget *,main_window *widgets){
    if(widgets){
        yon_monitor_new(widgets,0);
    }
}

void on_monitor_config_save(GtkWidget *, monitor_edit_window *window){
    int enabled=0;
    char *target=NULL;
    int main_display=0;
    char *resolution = NULL;
    const char *frequency = NULL;
    char *rotate = NULL;
    char *position = NULL;
    int modeline_cvt = 0;
    int cvt_reduced = 0;
    int modeline_gtf = 0;
    int do_not_switch_off = 0;
    enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->EnableCheck));
    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ManualCheck))){
        if (!gtk_combo_box_get_active(GTK_COMBO_BOX(window->PortCombo))){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),MISSING_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->PortCombo);
            return;
        }
        target = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->PortCombo));
    } else {
        target = (char*)gtk_entry_get_text(GTK_ENTRY(window->PortEntry));
        if (yon_char_is_empty(target)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),MISSING_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->PortEntry);
            return;
        }
    } 
        main_display = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->MainCheck));
            char *res_str = yon_char_new(gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->ResolutionCombo)));
            if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->ResolutionCombo))<1&&yon_char_is_empty(res_str)){
                res_str = yon_gtk_entry_get_by_pattern(GTK_ENTRY(window->ResolutionEntry),"^\\d{3,5}x\\d{3,5}");
            }
            if (!yon_char_is_empty(res_str)){
                frequency = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->FrequencyCombo));
                resolution = yon_char_unite(res_str,!yon_char_is_empty(frequency)?"x":NULL,frequency,NULL);
            }
        int rot = gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationCombo));
        rotate = yon_char_append("rotate:",rot == 0?"normal":rot==1?"left":rot==2?"right":"invert");
        int pos = gtk_combo_box_get_active(GTK_COMBO_BOX(window->PositionPosCombo));
        if (pos&&gtk_combo_box_get_active(GTK_COMBO_BOX(window->PositionPortCombo))){
            position = yon_char_append(pos==1?"lo:":pos==2?"ro:":pos==3?"ab:":"be:",gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->PositionPortCombo)));
        }
        modeline_cvt = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ModelineCVTCheck));
        cvt_reduced = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UseCVTReducedCheck));
        modeline_gtf = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ModelineGTFCheck));
        do_not_switch_off = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DoNotSwitchOffCheck));

        int final_size;
        config_str final_parsed=NULL;
        if (resolution)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,resolution);
        if (modeline_cvt)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"cvt");
        if (modeline_gtf)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"gtf");
        if (cvt_reduced)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"reduced");
        if (enabled)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"enable");
        if (!enabled)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"disable");
        if (main_display)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"primary");
        if (do_not_switch_off){
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"dpms");
        }else{
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,"nodpms");
        }
        if (position)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,position);
        if (rotate)
        yon_char_parsed_add_or_create_if_exists(final_parsed,&final_size,rotate);
        char *final_string = yon_char_parsed_to_string(final_parsed,final_size,",");
        yon_config_register(XORG_MONITOR(target),XORG_MONITOR_get_command,final_string);
        on_subwindow_close(window->MainWindow);
    yon_monitor_view_update();
}

void yon_monitor_parse(monitor_edit_window *window, char *string){
    int parsed_size;
    config_str parsed = yon_char_parse(string,&parsed_size,",");
    if (parsed_size>0){
        for (int i=0; i<parsed_size;i++){
            if (!strcmp(parsed[i],"cvt")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->ModelineCVTCheck),1);
            } else if (!strcmp(parsed[i],"gtf")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->ModelineGTFCheck),1);

            } else if (!strcmp(parsed[i],"reduced")||!strcmp(parsed[i],"r")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseCVTReducedCheck),1);

            } else if (!strcmp(parsed[i],"dpms")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DoNotSwitchOffCheck),1);

            } else if (!strcmp(parsed[i],"nodpms")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DoNotSwitchOffCheck),0);

            } else if (!strcmp(parsed[i],"enable")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->EnableCheck),1);

            } else if (!strcmp(parsed[i],"disable")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->EnableCheck),0);

            } else if (!strcmp(parsed[i],"cvt")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->ModelineCVTCheck),1);

            } else if (!strcmp(parsed[i],"primary")){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->MainCheck),1);

            } else if (strstr(parsed[i],"lo:")||strstr(parsed[i],"LeftOf:")){
                yon_char_divide_search(parsed[i],":",-1);
                int target = yon_gtk_combo_box_text_find(window->PositionPortCombo,parsed[i]);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationCombo),target);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->PositionPosCombo),1);

            } else if (strstr(parsed[i],"ro:")||strstr(parsed[i],"RightOf:")){
                yon_char_divide_search(parsed[i],":",-1);
                yon_gtk_combo_box_text_find(window->PositionPortCombo,parsed[i]);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->PositionPosCombo),2);

            } else if (strstr(parsed[i],"ab:")||strstr(parsed[i],"Above:")){
                yon_char_divide_search(parsed[i],":",-1);
                yon_gtk_combo_box_text_find(window->PositionPortCombo,parsed[i]);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->PositionPosCombo),3);

            } else if (strstr(parsed[i],"be:")||strstr(parsed[i],"Below:")){
                yon_char_divide_search(parsed[i],":",-1);
                yon_gtk_combo_box_text_find(window->PositionPortCombo,parsed[i]);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->PositionPosCombo),4);

            } else if (strstr(parsed[i],"rotate:")){
                free(yon_char_divide_search(parsed[i],":",-1));
                if (!strcmp(parsed[i],"normal")){
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationCombo),0);
                } else if (!strcmp(parsed[i],"left")){
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationCombo),1);

                } else if (!strcmp(parsed[i],"right")){
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationCombo),2);

                } else if (!strcmp(parsed[i],"invert")){
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationCombo),3);

                }

            } else if (g_ascii_isdigit(parsed[i][0])&&strstr(parsed[i],"x")){
                if (yon_char_count(parsed[i],"x")>1){
                    int pos = yon_char_find_last(parsed[i],'x');
                    char *res = yon_char_divide(parsed[i],pos);
                    gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->FrequencyCombo),parsed[i]);
                    free(parsed[i]);
                    parsed[i]=res;
                }
                int resolution = gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->ResolutionCombo),parsed[i]);
                if (!resolution){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UnsupportedCheck),1);
                    while(gtk_events_pending()) gtk_main_iteration();
                    gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->ResolutionCombo),parsed[i]);
                }
            }
        }
    }
}

void yon_monitor_port_manual(GtkToggleButton *self,monitor_edit_window *monitors){
    if (gtk_toggle_button_get_active(self)){
        gtk_widget_show(monitors->PortEntry);
        gtk_widget_hide(monitors->PortCombo);
    } else {
        gtk_widget_show(monitors->PortCombo);
        gtk_widget_hide(monitors->PortEntry);
        
    }
}

void on_port_chosen_changed(GtkWidget *, monitor_edit_window *window){
    // dictionary *dict=NULL;
    for (int i=0;i<window->resolutionssize;i++){
        free(window->resolutions);
    }
    window->resolutions=NULL;
    window->resolutionssize=0;
    
        gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->ResolutionCombo));
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->ResolutionCombo),PARAMETER_DEFAULT_LABEL);
        gtk_combo_box_set_active(GTK_COMBO_BOX(window->ResolutionCombo),0);
        gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->PositionPortCombo));
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->PositionPortCombo),PARAMETER_DEFAULT_LABEL);
        gtk_combo_box_set_active(GTK_COMBO_BOX(window->PositionPortCombo),0);
        
        char *port_name = NULL;
        
        port_name = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->PortCombo));
        if (yon_char_is_empty(port_name)){
            port_name = (char*)gtk_entry_get_text(GTK_ENTRY(window->PortEntry));
        }

        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UnsupportedCheck))){
            for (int i=main_config.resolutions_size-1;i>0;i--){
                int size;
                config_str parsed = yon_char_parse(main_config.resolutions_sorted[i],&size,";");
                if (!yon_char_is_empty(parsed[0])){
                    char *cur_res = g_hash_table_lookup(main_config.resolutions,parsed[0]);
                    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->ResolutionCombo),parsed[0],cur_res);
                }
                yon_char_parsed_free(parsed,size);
            }
        } else {
            char *resolutions = g_hash_table_lookup(main_config.ports_table,port_name);
            int size;
            config_str parsed =yon_char_parse(resolutions,&size,",");
            for (int i=0;i<size;i++){
                char *cur_resolution = g_hash_table_lookup(main_config.resolutions,parsed[i]);
                if (!yon_char_is_empty(cur_resolution))
                    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->ResolutionCombo),parsed[i],cur_resolution);
            }
        }
        GList *list = g_hash_table_get_keys(main_config.ports_table);
        GList *iter = list;
        for (;iter;iter=iter->next){
            if (strcmp(port_name,iter->data)){
                    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->PositionPortCombo),iter->data);
            }
        }
}

int yon_string_compare(char **a, char **b){
    gchar *key_a = g_utf8_collate_key(*a, -1);
    gchar *key_b = g_utf8_collate_key(*b, -1);
    gint result = strcmp(key_a, key_b);
    g_free(key_a);
    g_free(key_b);

    return result;
}

void on_monitor_configure(GtkWidget *,monitor_data *window){
    if(window){
        monitor_edit_window *monitors = new(monitor_edit_window);
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_configuration);
        monitors->MainWindow = yon_gtk_builder_get_widget(builder, "MainWindow");
        monitors->StatusBox = yon_gtk_builder_get_widget(builder, "StatusBox");
        monitors->HeadLabel = yon_gtk_builder_get_widget(builder, "HeadLabel");
        monitors->EnableConfigurationBox = yon_gtk_builder_get_widget(builder, "EnableConfigurationBox");
        monitors->InterfaceConfigurationBox = yon_gtk_builder_get_widget(builder, "InterfaceConfigurationBox");
        monitors->EnableCheck = yon_gtk_builder_get_widget(builder, "EnableCheck");
        monitors->ManualCheck = yon_gtk_builder_get_widget(builder, "ManualCheck");
        monitors->PortCombo = yon_gtk_builder_get_widget(builder, "PortCombo");
        monitors->PortEntry = yon_gtk_builder_get_widget(builder, "PortEntry");
        monitors->MainCheck = yon_gtk_builder_get_widget(builder, "MainCheck");
        monitors->ResolutionCombo = yon_gtk_builder_get_widget(builder, "ResolutionCombo");
        monitors->ResolutionEntry = yon_gtk_builder_get_widget(builder, "ResolutionEntry");
        monitors->UnsupportedCheck = yon_gtk_builder_get_widget(builder, "UnsupportedCheck");
        monitors->FrequencyCombo = yon_gtk_builder_get_widget(builder, "FrequencyCombo");
        monitors->RotationCombo = yon_gtk_builder_get_widget(builder, "RotationCombo");
        monitors->PositionPosCombo = yon_gtk_builder_get_widget(builder, "PositionPosCombo");
        monitors->PositionPortCombo = yon_gtk_builder_get_widget(builder, "PositionPortCombo");
        monitors->DoNotSwitchOffCheck = yon_gtk_builder_get_widget(builder, "DoNotSwitchOffCheck");
        monitors->ModelineCVTCheck = yon_gtk_builder_get_widget(builder, "ModelineCVTCheck");
        monitors->UseCVTReducedCheck = yon_gtk_builder_get_widget(builder, "UseCVTReducedCheck");
        monitors->ModelineGTFCheck = yon_gtk_builder_get_widget(builder, "ModelineGTFCheck");
        monitors->ParameterLineCheck = yon_gtk_builder_get_widget(builder, "ParameterLineCheck");

        monitors->ParameterLineEntry = yon_gtk_builder_get_widget(builder, "ParameterLineEntry");
        monitors->CancelButton = yon_gtk_builder_get_widget(builder, "CancelButton");
        monitors->SaveButton = yon_gtk_builder_get_widget(builder, "SaveButton");
        monitors->resolutions = NULL;
        monitors->resolutionssize=0;

        yon_entry_set_pattern(GTK_ENTRY(monitors->ResolutionEntry),"^\\d{3,5}x\\d{3,5}\\s.*");
        char *title;
        char *cur_port = (char*)gtk_label_get_text(GTK_LABEL(window->NameLabel));
        if (!yon_char_is_empty(cur_port)){
            title = MONITOR_TITLE_LABEL(cur_port);
        } else {
            title=TITLE_LABEL;
        }

        gtk_label_set_text(GTK_LABEL(monitors->HeadLabel),title);

        yon_gtk_entry_block_restricted_symbols(GTK_ENTRY(monitors->PortEntry));

        g_signal_connect(G_OBJECT(monitors->ManualCheck),"toggled",G_CALLBACK(yon_monitor_port_manual),monitors);
        g_signal_connect(G_OBJECT(monitors->CancelButton), "clicked", G_CALLBACK(on_subwindow_close), NULL);
        g_signal_connect(G_OBJECT(monitors->SaveButton), "clicked", G_CALLBACK(on_monitor_config_save), monitors);
        g_signal_connect(G_OBJECT(monitors->ParameterLineCheck), "toggled", G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed), monitors->InterfaceConfigurationBox);
        g_signal_connect(G_OBJECT(monitors->ParameterLineCheck), "toggled", G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button), monitors->ParameterLineEntry);
        g_signal_connect(G_OBJECT(monitors->UnsupportedCheck), "toggled", G_CALLBACK(on_resolutions_unsupported_show),monitors);
        g_signal_connect(G_OBJECT(monitors->PortCombo), "changed", G_CALLBACK(on_port_chosen_changed),monitors);
        yon_gtk_window_setup(GTK_WINDOW(monitors->MainWindow),GTK_WINDOW(main_config.widgets->Window),title,"com.ublinux.ubl-settings-video","VieoConfigWindow");

        char *target = (char*)gtk_label_get_text(GTK_LABEL(window->NameLabel));

        monitors->curport=NULL;
        int config_size;
        config_str config_ports = yon_config_get_all_by_key(XORG_PARAMETER,&config_size);
        for (int i=0;i<config_size;i++){
            yon_char_divide_search_self(config_ports[i],"=",-1);
            free(yon_char_divide(config_ports[i],strlen(XORG_PARAMETER)));
            config_ports[i][strlen(config_ports[i])-1]='\0';
        }
        guint ports_size;
        config_str ports = (config_str)g_hash_table_get_keys_as_array(main_config.ports_table,&ports_size);
        // for (guint i=0;i<ports_size;i++){
            qsort(ports,ports_size,sizeof(*ports),(int(*)(const void*,const void*))yon_string_compare);
        // }
        for (guint i=0;i<ports_size;i++){
            int found = yon_char_parsed_check_exist(config_ports,config_size,ports[i]);
            if (found==-1||(!yon_char_is_empty(target)&&!strcmp(target,ports[i]))||yon_config_get_status(XORG_MONITOR(ports[i]))==-1){
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(monitors->PortCombo),ports[i],ports[i]);

            }
        }
        if (gtk_combo_box_get_active(GTK_COMBO_BOX(monitors->PortCombo))){
            for (guint i=0;i<g_hash_table_size(main_config.ports_table);i++){
                if (strcmp(ports[i],gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(monitors->PortCombo))))
                    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(monitors->PositionPortCombo),ports[i],ports[i]);
            }
        }

        if (!yon_char_is_empty(target)&&g_hash_table_contains(main_config.ports_table,target)){
            gtk_widget_set_sensitive(monitors->PortCombo,0);
            gtk_widget_set_sensitive(monitors->PortEntry,0);
            gtk_widget_set_sensitive(monitors->ManualCheck,0);
            gtk_combo_box_set_active_id(GTK_COMBO_BOX(monitors->PortCombo),target);
            char *config_string = yon_config_get_by_key(XORG_MONITOR(target));
            if (!yon_char_is_empty(config_string)){
                yon_monitor_parse(monitors,config_string);
            }
        } else if (!yon_char_is_empty(target)){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(monitors->ManualCheck),1);
            gtk_widget_set_sensitive(monitors->PortCombo,0);
            gtk_widget_set_sensitive(monitors->PortEntry,0);
            gtk_widget_set_sensitive(monitors->ManualCheck,0);
            gtk_entry_set_text(GTK_ENTRY(monitors->PortEntry),target);
        }
        
        on_port_chosen_changed(NULL,monitors);
        char *resolutions = NULL;
        if (!yon_char_is_empty(target)){
            resolutions = g_hash_table_lookup(main_config.ports_table,target);
        }
        if (yon_char_is_empty(resolutions)&&!yon_char_is_empty(target)){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(monitors->UnsupportedCheck),1);
        }
        while(gtk_events_pending()) gtk_main_iteration();

        char *parameter = config(XORG_MONITOR(target));
        {
            int size;
            config_str parsed = yon_char_parse(parameter,&size,",");
            if (size){
                char *current = yon_char_new(parsed[0]);
                char *resolution = NULL;
                if (g_regex_match_simple("^\\d+x\\d+x\\d+",current,0,0)){
                    int last = yon_char_find_last(current,'x');
                    resolution = yon_char_divide(current,last);
                    gtk_combo_box_set_active_id(GTK_COMBO_BOX(monitors->FrequencyCombo),current);

                } else if (g_regex_match_simple("^\\d+x\\d+",current,0,0)){
                    resolution = current;
                }

                if (!yon_char_is_empty(resolution)&&!yon_char_is_empty(target)&&!yon_char_is_empty(resolutions)&&!strstr(resolutions,resolution)){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(monitors->UnsupportedCheck),1);
                    while(gtk_events_pending()) gtk_main_iteration();

                }

                if (!yon_char_is_empty(resolution)){
                    if (!gtk_combo_box_set_active_id(GTK_COMBO_BOX(monitors->ResolutionCombo),resolution)){
                        gtk_entry_set_text(GTK_ENTRY(monitors->ResolutionEntry),resolution);
                    }
                }else{
                    gtk_combo_box_set_active(GTK_COMBO_BOX(monitors->ResolutionCombo),0);
                }
            }
        }
        gtk_widget_show(monitors->MainWindow);
    }
}

// void yon_monitor_view_dictionary_destroy(void *window){
//     monitor_data *monitor=(monitor_data*)window;
//     gtk_widget_destroy(monitor->ScreenOverlay);
// }

/**void yon_monitor_view_update()
 * [EN]
*/
void yon_monitor_view_update(){
    GList *list = gtk_container_get_children(GTK_CONTAINER(main_config.widgets->MonitorsBox));
    for (guint i=0;i<g_list_length(list);i++){
        gtk_widget_destroy(GTK_WIDGET(g_list_nth_data(list,i)));
    }
    g_list_free(list);

    yon_monitor_new(main_config.widgets,1);

    int size;
    config_str parameters = yon_config_get_all_by_key(XORG_PARAMETER,&size);
    for (int i=0;i<size;i++){
        char *current = yon_char_divide_search(parameters[i],"=",-1);
        if (yon_config_get_status(current)>-1){
            monitor_data *window = yon_monitor_new(main_config.widgets,0);
            gtk_widget_set_sensitive(window->SwitchButton,1);
            free(yon_char_divide(current,strlen(XORG_PARAMETER)));
            yon_char_remove_last_symbol(current,']');
            gtk_label_set_text(GTK_LABEL(window->NameLabel),current);
            int parsed_size;
            config_str parsed = yon_char_parse(parameters[i],&parsed_size,",");
            if (parsed_size>0){
                if (strstr(parsed[0],"x")){
                    gtk_label_set_text(GTK_LABEL(window->ResoulutionLabel),parsed[0]);
                }
            }
            if (yon_char_parsed_check_exist(parsed,parsed_size,"enabled")>-1){
                gtk_widget_set_tooltip_text(window->SwitchButton,ENABLE_TOOLTIP_LABEL);
            } else {
                gtk_widget_set_tooltip_text(window->SwitchButton,DISABLE_TOOLTIP_LABEL);

            }
            if (yon_char_parsed_check_exist(parsed,parsed_size,"enable")>-1){
                gtk_style_context_remove_class(gtk_widget_get_style_context(gtk_widget_get_parent(window->MonitorInfoBox)),"displayunsens");
            } else {
                gtk_style_context_add_class(gtk_widget_get_style_context(gtk_widget_get_parent(window->MonitorInfoBox)),"displayunsens");
            }
            yon_char_parsed_free(parsed,parsed_size);
        }
    }
}

monitor_data *yon_monitor_new(main_window *widgets,int dull){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_monitor);
    monitor_data *window = new(monitor_data);
    window->MainFlow=yon_gtk_builder_get_widget(builder,"MainFlow");
    // window->ScreenOverlay=yon_gtk_builder_get_widget(builder,"ScreenOverlay");
    // window->MonitorImage=yon_gtk_builder_get_widget(builder,"MonitorImage");
    window->MonitorInfoBox=yon_gtk_builder_get_widget(builder,"MonitorInfoBox");
    window->NameLabel=yon_gtk_builder_get_widget(builder,"NameLabel");
    window->ResoulutionLabel=yon_gtk_builder_get_widget(builder,"ResoulutionLabel");
    window->DeleteButton=yon_gtk_builder_get_widget(builder,"DeleteButton");
    window->SwitchButton=yon_gtk_builder_get_widget(builder,"SwitchButton");
    window->ConfigureButton=yon_gtk_builder_get_widget(builder,"ConfigureButton");
    window->AddButton=yon_gtk_builder_get_widget(builder,"AddButton");
    window->target_port=NULL;
    window->target_resolution=NULL;

    g_object_set_data(G_OBJECT(window->MainFlow),"monitor_data",window);
    g_object_set_data(G_OBJECT(window->MainFlow),"widgets",widgets);
    g_object_set_data(G_OBJECT(window->DeleteButton),"monitor_data",window);
    g_object_set_data(G_OBJECT(window->AddButton),"monitor_data",window);
    g_object_set_data(G_OBJECT(window->SwitchButton),"monitor_data",window);
    g_object_set_data(G_OBJECT(window->ConfigureButton),"monitor_data",window);
    
    // gtk_overlay_add_overlay(GTK_OVERLAY(window->ScreenOverlay),window->MonitorInfoBox);
        gtk_style_context_add_class(gtk_widget_get_style_context(gtk_widget_get_parent(window->MonitorInfoBox)),"displayunsens");
    if (!dull){
        gtk_widget_hide(window->AddButton);
        gtk_widget_set_sensitive(window->SwitchButton,0);

        g_signal_connect(G_OBJECT(window->ConfigureButton), "clicked", G_CALLBACK(on_monitor_configure), window);
        g_signal_connect(G_OBJECT(window->DeleteButton), "clicked", G_CALLBACK(on_monitor_delete), window);
        g_signal_connect(G_OBJECT(window->SwitchButton), "clicked", G_CALLBACK(on_monitor_switch), window);
        
    } else {
        gtk_widget_show(window->AddButton);
        gtk_widget_hide(window->DeleteButton);
        gtk_widget_hide(window->SwitchButton);
        gtk_widget_hide(window->ConfigureButton);
        g_signal_connect(G_OBJECT(window->AddButton), "clicked", G_CALLBACK(on_monitor_add), widgets);
    }
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->MonitorsBox));
    gtk_flow_box_insert(GTK_FLOW_BOX(widgets->MonitorsBox),window->MainFlow,g_list_length(list)-1);
    g_list_free(list);
    return window;
}
