#include <glib.h>
#include <glib/gi18n.h>

#include "config.h"
#include "Common.h"
#include "PowerButton.h"

#define POPOVER_ITEM_MARKUP_FORMAT "<span foreground=\"#ffffff\" font_desc=\"12\">\%1</span>"

PowerButton::PowerButton()
    : ImageButton()
    , m_popover()
    , m_popMainLyt(Gtk::ORIENTATION_VERTICAL, 0)
    , m_powerOff()
    , m_reboot()
    , m_suspend() {

    double scaleX = 1.0f;
    double scaleY = 1.0f;
    Common::getPrimaryScaleFactors(scaleX, scaleY);

    m_powerLbl.set_markup(Glib::ustring::compose(POPOVER_ITEM_MARKUP_FORMAT,
    	                                                    Glib::ustring::format(_("Poweroff"))));

    m_rebootLbl.set_markup(Glib::ustring::compose(POPOVER_ITEM_MARKUP_FORMAT,
    	                                                    Glib::ustring::format(_("Restart"))));

    m_suspendLbl.set_markup(Glib::ustring::compose(POPOVER_ITEM_MARKUP_FORMAT,
    	                                                    Glib::ustring::format(_("Suspend"))));

    m_powerOff.add(m_powerLbl);
    m_reboot.add(m_rebootLbl);
    m_suspend.add(m_suspendLbl);
    m_popMainLyt.pack_start(m_reboot, false, true);
    m_popMainLyt.pack_start(m_suspend, false, true);
    m_popMainLyt.pack_start(m_powerOff, false, true);
    m_popover.add(m_popMainLyt);
    m_popover.set_relative_to(*this);
    m_popover.set_position(Gtk::POS_TOP);

    m_powerLbl.set_name("cdos_popover_label");
    m_rebootLbl.set_name("cdos_popover_label");
    m_suspendLbl.set_name("cdos_popover_label");

    m_powerOff.set_name("cdos_popover_item");
    m_reboot.set_name("cdos_popover_item");
    m_suspend.set_name("cdos_popover_item");
    m_popover.set_name("cdos_popover");

    m_powerOff.set_relief(Gtk::RELIEF_NONE);
    m_reboot.set_relief(Gtk::RELIEF_NONE);
    m_suspend.set_relief(Gtk::RELIEF_NONE);

    m_sigPoweroffIter = m_powerOff.signal_clicked().connect(sigc::mem_fun(*this, &PowerButton::onPowerOffClicked));
    m_sigRebootIter = m_reboot.signal_clicked().connect(sigc::mem_fun(*this, &PowerButton::onRebootClicked));
    m_sigSuspIter = m_suspend.signal_clicked().connect(sigc::mem_fun(*this, &PowerButton::onSuspendClicked));

    m_sigBtnPressIter = signal_button_press_event().connect(sigc::mem_fun(*this, &PowerButton::onButtonEvent));
    m_sigBtnKeyPressIter = signal_key_press_event().connect(sigc::mem_fun(*this, &PowerButton::onKeyEvent));

    StateImage stateImages;
    stateImages.setStateImage(StateImage::STATE_NORMAL, GLOBAL_RES_DIR"/icon/shutdown_default.png");
    stateImages.setStateImage(StateImage::STATE_PRESSED, GLOBAL_RES_DIR"/icon/shutdown_pressed_or_hover.png");
    stateImages.setStateImage(StateImage::STATE_HOVER, GLOBAL_RES_DIR"/icon/shutdown_pressed_or_hover.png");

    setStateImage(stateImages);

    set_size_request((int)(37 * scaleX), (int)(37 * scaleY));
    Common::addCssStylePath(m_popover, GLOBAL_RES_DIR"/style/cdos-login.css", G_MAXUINT);
}

PowerButton::~PowerButton() {
    m_sigPoweroffIter.disconnect();
    m_sigRebootIter.disconnect();
    m_sigSuspIter.disconnect();

    m_sigBtnPressIter.disconnect();
    m_sigBtnKeyPressIter.disconnect();
}

void PowerButton::requestPopover() {
    if (!m_popover.is_visible()) {
        m_popMainLyt.show_all();
        m_popover.popup();
    }
}

sigc::signal<void> PowerButton::signalPoweroffClicked() {
    return m_signalPoweroff;
}

sigc::signal<void> PowerButton::signalRebootClicked() {
    return m_signalReboot;
}

sigc::signal<void> PowerButton::signalSuspendClicked() {
    return m_signalSuspend;
}

void PowerButton::onPowerOffClicked() {
    m_signalPoweroff.emit();
}

void PowerButton::onRebootClicked() {
    m_signalReboot.emit();
}

void PowerButton::onSuspendClicked() {
    m_popover.popdown();
    m_signalSuspend.emit();
}

bool PowerButton::onButtonEvent(GdkEventButton* event) {
    if (1 == event->button) {
        if (m_popover.is_visible()) {
            m_popover.popdown();
        } else {
            m_popMainLyt.show_all();
        	   m_popover.popup();
        }
    } else {
    	   if (m_popover.is_visible()) {
            m_popover.popdown();
        }
    }
    return true;
}

bool PowerButton::onKeyEvent(GdkEventKey* event) {
    if (GDK_KEY_PRESS == event->type) {
        switch (event->keyval) {
        case GDK_KEY_Return:
		case GDK_KEY_KP_Enter:
		case GDK_KEY_space:
            if (!m_popover.is_visible()) {
                m_popMainLyt.show_all();
                m_popover.popup();
            }
            break;
        case GDK_KEY_Escape:
            if (m_popover.is_visible()) {
                m_popover.popdown();
            }
            break;
        }
    }
    return true;
}