#include <glib.h>
#include <glib/gi18n.h>
#include "StateImage.h"
#include <unistd.h>

#include "config.h"
#include "Common.h"
#include "DmGreeterHelper.h"
#include "UserItem.h"

#define AVATAR_DEFAULT_WIDTH (145)
#define AVATAR_DEFAULT_HEIGHT (145)

#define USERITEM_DEFAULT_WIDTH (225)
#define USERITEM_DEFAULT_HEIGHT (225)

#define AVATAR_RESOURCE_DEFAULT          GLOBAL_RES_DIR"/pic/avatar_default.png"
#define AVATAR_RESOURCE_HOVER_OR_PRESSED GLOBAL_RES_DIR"/pic/avatar_pressed_or_hover.png"

enum {
	AVATAR_STATE_NORMAL = 0,
	AVATAR_STATE_PRESSED = 1 << 0,
	AVATAR_STATE_HOVER = 1 << 1
};

UserItem::UserItem()
	: Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0)
	  , m_userName("")
	  , m_displayName("Other")
	  , m_userNameLabel("")
	, m_loginStateLabel("")
	, m_nameArea(Gtk::ORIENTATION_VERTICAL, 0)
	, m_userFace()
	  , m_state(AVATAR_STATE_NORMAL)
	  , m_isLoggedIn(false) {
		  initialize();
	  }

UserItem::UserItem(const Glib::ustring& name)
	: Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0)
	, m_userName(name)
	, m_displayName(name)
	  , m_userNameLabel(name)
	, m_loginStateLabel("")
	, m_nameArea(Gtk::ORIENTATION_VERTICAL, 0)
	, m_userFace()
	  , m_state(AVATAR_STATE_NORMAL)
	  , m_isLoggedIn(false) {
		  initialize();
	  }

UserItem::~UserItem() {
	m_sigUserPressIter.disconnect();
	m_sigUserKeyPressIter.disconnect();
}

void UserItem::initialize() {
	double scaleX = 1.0f;
	double scaleY = 1.0f;

	Common::getPrimaryScaleFactors(scaleX, scaleY);

	m_userNameLabel.set_ellipsize(Pango::ELLIPSIZE_END);

	m_sigUserPressIter = m_userFace.signal_button_press_event().connect(sigc::mem_fun(*this, &UserItem::onUserFacePress));
	m_sigUserKeyPressIter = signal_key_press_event().connect(sigc::mem_fun(*this, &UserItem::onUserFaceKeyPress));

	m_nameArea.set_margin_top(18 * scaleY);

	m_userFace.set_margin_top(18 * scaleY);
	m_userFace.set_size_request(AVATAR_DEFAULT_WIDTH * scaleX, AVATAR_DEFAULT_HEIGHT * scaleY);

	m_nameArea.pack_start(m_userNameLabel, FALSE, TRUE, 0);
	m_nameArea.pack_start(m_loginStateLabel, FALSE, TRUE, 0);

	pack_start(m_userFace, FALSE, FALSE, 0);
	pack_start(m_nameArea, FALSE, TRUE, 0);

	m_userFace.set_halign(Gtk::ALIGN_CENTER);
	m_userNameLabel.set_halign(Gtk::ALIGN_CENTER);
	m_loginStateLabel.set_halign(Gtk::ALIGN_CENTER);

	setUserName(m_userName);
	Common::addCssStylePath(*this, GLOBAL_RES_DIR"/style/cdos-login.css", G_MAXUINT);

	set_size_request(USERITEM_DEFAULT_WIDTH * scaleX, USERITEM_DEFAULT_HEIGHT * scaleY);

	set_can_focus(false);
	set_name("user_item");
}


void UserItem::setUserName(const Glib::ustring& name) {
	bool isNeedTranslate = false;
	if (name.empty()) {
		m_displayName = "Other";
		isNeedTranslate = true;
	} else {
		m_displayName = name;
	}

	m_userName = name;
	Glib::ustring composeStr = Glib::ustring::compose("<span foreground=\"#ffffff\" font_desc=\"20\">\%1</span>",
			Glib::ustring::format((isNeedTranslate ? _(m_displayName.c_str()) : m_displayName)));
	m_userNameLabel.set_markup(composeStr);
	m_userNameLabel.set_use_markup(true);
	m_userNameLabel.set_tooltip_text(name);

	onUserChanged();
}

Glib::ustring UserItem::getUserName() {
	return m_userName;
}

void UserItem::setLoggedIn(bool isLoggedIn) {
	m_isLoggedIn = isLoggedIn;

	if (isLoggedIn) {
		Glib::ustring composeStr = Glib::ustring::compose("<span foreground=\"#ffffff\" font_desc=\"9\">\%1</span>",
				Glib::ustring::format(_("Logged In")));
		m_loginStateLabel.set_markup(composeStr);
		m_loginStateLabel.set_use_markup(true);
	} else {
		m_loginStateLabel.set_label("");
		m_loginStateLabel.set_use_markup(false);
	}
}

void UserItem::resetFocus() {
    m_userFace.grab_focus();
}

void UserItem::set_face_can_focus(bool canFocus) {
    m_userFace.set_can_focus(canFocus);
}

sigc::signal<void, Glib::ustring> UserItem::signalUserItemPress() {
	return m_pressSignal;
}

bool UserItem::onUserFacePress(GdkEventButton* event) {
	signalUserItemPress().emit(m_userName);
	return false;
}

bool UserItem::onUserFaceKeyPress(GdkEventKey* event) {
	if (GDK_KEY_PRESS == event->type) {
		switch (event->keyval) {
		case GDK_KEY_Return:
		case GDK_KEY_KP_Enter:
		case GDK_KEY_space:
			signalUserItemPress().emit(m_userName);
			break;
		}
	}
	return false;
}

void UserItem::onUserChanged() {

	StateImage stateImages;

	Glib::ustring iconFileName = "";
	if (!m_userName.empty()) {
		iconFileName = DmGreeterHelper::inst()->getUserImagePath(m_userName);
	}

	if (!iconFileName.empty() && (0 == access(iconFileName.c_str(), R_OK))) {
		stateImages.setStateImage(StateImage::STATE_NORMAL, iconFileName);
	} else {
		stateImages.setStateImage(StateImage::STATE_NORMAL, AVATAR_RESOURCE_DEFAULT);
		stateImages.setStateImage(StateImage::STATE_PRESSED, AVATAR_RESOURCE_HOVER_OR_PRESSED);
		stateImages.setStateImage(StateImage::STATE_HOVER, AVATAR_RESOURCE_HOVER_OR_PRESSED);
	}

	m_userFace.setStateImage(stateImages);
	m_userFace.set_name("user_avatar");
}
