// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/ui/tray_icon_gtk.h"

#include "atom/browser/browser.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/common/application_info.h"
#include "chrome/browser/ui/libgtkui/app_indicator_icon.h"
#include "chrome/browser/ui/libgtkui/gtk_status_icon.h"
#include "ui/gfx/image/image.h"

namespace atom {

namespace {

// Number of app indicators used (used as part of app-indicator id).
int indicators_count;

}  // namespace

TrayIconGtk::TrayIconGtk() {
}

TrayIconGtk::~TrayIconGtk() {
}

void TrayIconGtk::SetImage(const gfx::Image& image) {
  if (icon_) {
    icon_->SetImage(image.AsImageSkia());
    return;
  }

  const auto toolTip = base::UTF8ToUTF16(brightray::GetApplicationName());

  if (libgtkui::AppIndicatorIcon::CouldOpen()) {
    ++indicators_count;
    icon_.reset(new libgtkui::AppIndicatorIcon(
        base::StringPrintf(
            "%s%d", Browser::Get()->GetName().c_str(), indicators_count),
        image.AsImageSkia(),
        toolTip));
  } else {
    icon_.reset(new libgtkui::Gtk2StatusIcon(image.AsImageSkia(), toolTip));
  }
  icon_->set_delegate(this);
}

void TrayIconGtk::SetToolTip(const std::string& tool_tip) {
  icon_->SetToolTip(base::UTF8ToUTF16(tool_tip));
}

void TrayIconGtk::SetContextMenu(AtomMenuModel* menu_model) {
  icon_->UpdatePlatformContextMenu(menu_model);
}

void TrayIconGtk::OnClick() {
  NotifyClicked();
}

bool TrayIconGtk::HasClickAction() {
  return false;
}

// static
TrayIcon* TrayIcon::Create() {
  return new TrayIconGtk;
}

}  // namespace atom
