/* This file is part of GPump, a Pump.io client.
 *
 * GPump (THE SOFTWARE) is made available under the terms and conditions of the
 * GNU Lesser General Public Licence 3.0. A copy of the licence can be read
 * in the file lgpl-3.0.txt in the root of this project.
 */

/** @file
 * UserButton is a custom button that displays the users avatar and name.
 */

const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;

const UserButton = Lang.Class ({
  Name: "UserButton",
  Extends: Gtk.MenuButton,
  Properties: {
    /** @property user_avatar
     *
     */
    "user_avatar": GObject.param_spec_object ("user_avatar",
                                     "avatar",
                                     "A Gtk.Image that is shown in the button",
                                     GObject.Object,
                                     GObject.ParamFlags.READABLE |
                                     GObject.ParamFlags.WRITABLE),
    
    /** @property user_name
     *
     */
    "user_name": GObject.param_spec_string ("user_name",
                                     "name",
                                     "The text to be shown on the button",
                                     "Jone Deo",
                                     GObject.ParamFlags.READABLE |
                                     GObject.ParamFlags.WRITABLE),
    
    /** @property show_avatar_only
     *
     */
    "show_avatar_only": GObject.param_spec_boolean ("show_avatar_only",
                                     "show only avatar",
                                     "When set, only the avatar will be shown" +
                                     "in the user button",
                                     false,
                                     GObject.ParamFlags.READABLE |
                                     GObject.ParamFlags.WRITABLE)
  },
  
  
  _init: function (params) {
    this.parent ();
    
    this.add (this.layout = new Gtk.Box ({
        orientation: Gtk.Orientation.HORIZONTAL
      })
    );
    
    if (params != undefined) {
    
      if (params["user_avatar"] != undefined) {
        this.user_avatar = params["user_avatar"];
      } else {
        this._user_avatar = Gtk.Image.new_from_icon_name (
          "avatar-default-symbolic", null);
      }
      
      if (params["user_name"] != undefined) {
        this._user_name = params["user_name"];
      } else {
        this._user_name = "Jone Deo";
      }
      
      if (params["show_avatar_only"] != undefined) {
        this._show_avatar_only = params["show_avatar_only"];
      } else {
        this._show_avatar_only = false;
      }
      
      if (params["popover"] != undefined) {
        this["popover"] = params["popover"];
      }
     
    } else {
      this._user_avatar = Gtk.Image.new_from_icon_name (
          "avatar-default-symbolic", null);
      this._user_name = "Jone Deo";
      this._show_avatar_only = false;
      this["popover"] = null;
    }
    
    this._rebuild ();
  },
  
  /**
   * Hack to re-build the button, cus we can not use references in JavaScript.
   */
  _rebuild: function () {
    
    this.layout.hide ();
    
    this.layout.foreach (function (widget) {
      widget.destroy ();
    });
    
    this.layout.pack_start (this._user_avatar, false, false, 3);
    
    this.user_avatar["icon_size"] = 4;
    
    
    if (this._show_avatar_only == false) {
      
      this.layout.pack_start (new Gtk.Separator ({
        orientation: Gtk.Orientation.VERTICAL
      }), false, false, 5);
      
      
      this.layout.pack_end (
        this._user_name_widget = new Gtk.Label ({
          label: this._user_name,
          justify: Gtk.Justification.CENTER
        }), true, true, 10
      );
    }
    this.show_all ();
  },
  
  get user_avatar () {
    return this._user_avatar;
  },
  
  set user_avatar (avatar) {
    if (avatar == undefined) {
      print ("balls!");
      this._user_avatar = Gtk.Image.new_from_icon_name (
        "avatar-default-symbolic", null);
    } else {
     print ("Not balls!");
      this._user_avatar = avatar;
      this._rebuild ();
    }
  },
  
  get user_name () {
    return this._user_name;
  },
  
  set user_name (name) {
    this._user_name = name;
    this._rebuild ();
  },
  
  get show_avatar_only () {
    return _show_avatar_only;
  },
  
  set show_avatar_only (show_avatar_only) {
    this._show_avatar_only = show_avatar_only;
    this._rebuild ()
  }
  
});
