#include "view_open_browser.h"


View_open_browser* obj_open_browser;
View_about* obj_about;

void wrapper_help_show(GtkWidget *self, char* link, gpointer user_data) {
    if (self && user_data) {}
    obj_open_browser->global_lick_doc = link;
    obj_about->aboutWindows->hide();
    obj_open_browser->temp_help_show();
}

View_open_browser::View_open_browser(/* args */) {
    builder = Gtk::Builder::create_from_file(path_glade);
}

View_open_browser::~View_open_browser() {
}

void View_open_browser::settings() {
    obj_open_browser = this;
    this->get_builder();
    #ifdef WEBKIT_FOUND
        my_web_kit =  WEBKIT_WEB_VIEW( webkit_web_view_new() );
        three = Glib::wrap(GTK_WIDGET(my_web_kit));
        wndWeb->add(*three);
    #endif
    this->lacalization();
    this->event();
}

void View_open_browser::set_obj_about(View_about* obj_view_about) {
    this->obj_view_about = obj_view_about;
    obj_about = obj_view_about;
}

void View_open_browser::show() {
    this->wndShowWeb->show();
}
void View_open_browser::get_builder() {
    #ifdef WEBKIT_FOUND
            builder->get_widget("wndWeb", wndWeb);
    #endif
    builder->get_widget("lblHeadeWndWeb", lblHeadeWndWeb);
    builder->get_widget("lblwebHeaderName", lblwebHeaderName);
    builder->get_widget("lblhelpText", lblhelpText);
    builder->get_widget("lblhelpHeader", lblhelpHeader);
    builder->get_widget("chkAlwaysOpenHelp", chkAlwaysOpenHelp);
    builder->get_widget("btnReadHelp", btnReadHelp);
    builder->get_widget("btnCancelHelp", btnCancelHelp);
    builder->get_widget("wndShowWeb", wndShowWeb);
    
}
void View_open_browser::lacalization() {
    lblwebHeaderName->set_label(name_app);
    lblhelpText->set_text(redirected_documentation);
    btnReadHelp->set_label(read_online);
    btnCancelHelp->set_label(cancel);
    chkAlwaysOpenHelp->set_label(always_redirect);
    lblhelpHeader->set_text(read_documentation_web);
}

void View_open_browser::event() {
    g_signal_connect(G_OBJECT(obj_view_about->aboutWindows->gobj()), "activate-link", G_CALLBACK(wrapper_help_show), NULL);
    btnCancelHelp->signal_clicked().connect([&]() {wndShowWeb->hide();});
    chkAlwaysOpenHelp->signal_toggled().connect([&]() {flag_open_browser = true;});
    btnReadHelp->signal_clicked().connect(sigc::mem_fun(*this, &View_open_browser::open_browser));
}

void View_open_browser::temp_help_show() {
    if (flag_open_browser == true) {
        this->open_browser();
    }
    else {
        wndShowWeb->show_all(); 
    }
}

void View_open_browser::open_browser() {
    #ifdef WEBKIT_FOUND
        if (my_web_kit) {
            gtk_widget_destroy(GTK_WIDGET(my_web_kit));
        }
        my_web_kit =  WEBKIT_WEB_VIEW(webkit_web_view_new());
        three = Glib::wrap(GTK_WIDGET(my_web_kit));
        wndWeb->add(*three);
        webkit_web_view_load_uri(my_web_kit, _(global_lick_doc.c_str()));
        wndWeb->show_all();
        webkit_web_view_load_uri(my_web_kit, _(global_lick_doc.c_str()));
        wndWeb->show_all();
        webkit_web_view_load_uri(my_web_kit, _(global_lick_doc.c_str()));
        wndWeb->show_all();
    #else
        this->template_open_browser(global_lick_doc);
    #endif
    this->wndShowWeb->hide();
}

int View_open_browser::template_open_browser(string str_link_doc) {
    string cmd = cmd_xdg + string(_(str_link_doc.c_str())) + " &";
    string buf = "";
    if (geteuid() == 0) {
        string response_user = getlogin();
        int size_s = std::snprintf(nullptr, 0, cmd_execute, response_user.c_str(), cmd.c_str()) + 1;
        auto size = static_cast<size_t>(size_s);
        std::unique_ptr<char[]> buf(new char[ size ]);
        std::snprintf(buf.get(), size, cmd_execute, response_user.c_str(), cmd.c_str());
        cmd = string(buf.get(), buf.get() + size - 1);
    }
    return system(cmd.c_str());
     
}
void View_open_browser::open_help() {
    global_lick_doc = const_link_doc;
    temp_help_show();
}

View_about* View_open_browser::get_about() {
    return obj_view_about;
}
