#include "MenuBar.h"


Menu::Menu(const wchar_t* caption)
: Label(0, true)
{
    _caption->setFont(L"Microsoft Sans Serif", L"000000", 13.0f, Text::FONT_STYLE_REGULAR, Text::H_ALIGN_CENTER | Text::V_ALIGN_CENTER);

    setSize(Size(32.0f, 24.0f));
    setCaption(caption);

    _list = new PopupMenu(this);
    _list->setPosition(Point(0.0f, _size.h()));

    connectEvent(Event::MOUSE_ENTER,        this, (EventHandler)&Menu::onMouseEnter);
    connectEvent(Event::MOUSE_LEAVE,        this, (EventHandler)&Menu::onMouseLeave);
    connectEvent(Event::MOUSE_LEFT_DOWN,    this, (EventHandler)&Menu::onMouseLeftDown);
}

Menu::~Menu()
{
}

void Menu::adjustSizeForCaption()
{
    setSize(Size(ceil(_caption->getWidth()) + 10.0f, _size.h()));
}

void Menu::setCaption(const wchar_t* caption)
{
    Label::setCaption(caption);

    if (MenuBar* parent = dynamic_cast<MenuBar*>(_parent))
        parent->adjustMenuPositions();
    else
        adjustSizeForCaption();
}

void Menu::addItem(MenuItem* item)
{
    this->connectEvent(Event::MOUSE_LEFT_CLICK, item, (EventHandler)&Menu::onMenuItemLeftClick);
    _list->addItem(item);
}

bool Menu::isDropDownListShown()
{
    return _list->isVisible();
}

void Menu::showDropDownList(bool showList)
{
    _list->setVisible(showList);
}

void Menu::onMouseEnter(Event& event)
{
    if (MenuBar* parent = dynamic_cast<MenuBar*>(_parent)) {
        if (parent->getOpened()) {
            parent->closeMenus();
            highlight(true);
            _list->setVisible(true);   
        }
    }
}

void Menu::onMouseLeave(Event& event)
{
    bool listHit = _list->isHit(event.mouseX, event.mouseY);

    if (MenuBar* parent = dynamic_cast<MenuBar*>(_parent)) {
        if (parent->getOpened() && (!listHit)) {
            _list->setVisible(false);
        }
    }

    if (listHit)
        highlight(true);
}

void Menu::onMouseLeftDown(Event& event)
{
    if (MenuBar* parent = dynamic_cast<MenuBar*>(_parent)) {
        parent->setOpened(!parent->getOpened());
    }

    _list->setVisible(!_list->isVisible());
}

void Menu::onMenuItemLeftClick(Event& event)
{
    if (MenuBar* menuBar = dynamic_cast<MenuBar*>(_parent)) {
        showDropDownList(false);
        highlight(false);
        menuBar->setOpened(false);
    }
}

MenuBar::MenuBar(Window* parent)
: Window(parent), _opened(false)
{
    if (_parent != 0) {
        Size parentSize = _parent->getSize();

        setPosition(Point(0.0f, 0.0f));
        setSize(Size(parentSize.w(), 24.0f));
    }

    else {
        setPosition(Point(0.0f, 0.0f));
        setSize(Size(256.0f, 24.0f));
    }

    _texture = ResourceManager::instance().loadTexture(L"textures/gui/menubar.dds");
}

MenuBar::~MenuBar()
{
}

void MenuBar::addMenu(Menu* menu)
{
    menu->setParent(this);

    Point position(0.0f, 0.0f);

    for (size_t i = 0; i < _menus.size(); ++i)
        position.x() += _menus[i]->getSize().w();

    menu->setPosition(position);
    menu->adjustSizeForCaption();

    _menus.push_back(menu);
}

void MenuBar::adjustMenuPositions()
{
    Point position(0.0f, 0.0f);

    for (size_t i = 0; i < _menus.size(); ++i) {
        _menus[i]->setPosition(position);
        _menus[i]->adjustSizeForCaption();
        
        position.x() += _menus[i]->getSize().w();
    }
}

bool MenuBar::getOpened()
{
    return _opened;
}

void MenuBar::setOpened(bool opened)
{
    _opened = opened;
}

Menu* MenuBar::getOpenMenu()
{
    for (size_t i = 0; i < _menus.size(); ++i) {
        if (_menus[i]->isDropDownListShown()) {
            return _menus[i];
        }
    }

    return 0;
}

void MenuBar::closeMenus()
{
    for (size_t i = 0; i < _menus.size(); ++i) {
        if (_menus[i]->isDropDownListShown()) {
            _menus[i]->showDropDownList(false);
            _menus[i]->highlight(false);
        }
    }
}

void MenuBar::setSize(Size& size)
{
    Window::setSize(size);

    adjustMenuPositions();
}