const Lang = imports.lang;

const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const GLib = imports.gi.GLib;
const St = imports.gi.St;

const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;

const Util = imports.misc.util;
const Self = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Self.imports.utils;

let TIMER_MINUTES = 0;
let TIMER_HOURS = 0;

let panelEntry;

function init() {
}

function enable() {
  panelEntry = new WallpaperChangerEntry();
  Main.panel.addToStatusArea("wallpaper-changer-menu", panelEntry);
}

function disable() {
  panelEntry.destroy();
}

const WallpaperChangerEntry = new Lang.Class({
  Extends: PanelMenu.Button,
  Name: "WallpaperChangerEntry",

  _init: function () {
    this.parent(0, "WallpaperChangerEntry");

    this.settings = Utils.getSettings();
    this._applySettings();
    this.settings.connect('changed', Lang.bind(this, this._applySettings));

    const icon = new St.Icon({
      icon_name: 'preferences-desktop-wallpaper-symbolic',
      style_class: 'system-status-icon'
    });
    this.actor.add_child(icon);

    // Construct items
    const nextItem = new PopupMenu.PopupMenuItem('Next Wallpaper');
    const settingsMenuItem = new PopupMenu.PopupMenuItem('Settings');

    // Add items to menu
    this.menu.addMenuItem(nextItem);
    this.menu.addMenuItem(settingsMenuItem);

    // Bind events
    settingsMenuItem.connect('activate', Lang.bind(this, this._openSettings));
    nextItem.connect('activate', Lang.bind(this, function () {
      this.provider.next(this._setWallpaper);
    }));
  },

  _openSettings: function () {
    Util.spawn(["gnome-shell-extension-prefs", Self.uuid]);
  },

  _applySettings: function () {
    this.provider = Utils.getProvider(this.settings.get_string('provider'));
    TIMER_MINUTES = this.settings.get_int('minutes');
    TIMER_HOURS = this.settings.get_int('hours');

    this._setTimer();
  },

  _setTimer: function () {
    if (this.timer) {
      GLib.Source.remove(this.timer);
    }

    if (TIMER_HOURS + TIMER_MINUTES > 0) {
      this.timer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT,
        TIMER_MINUTES * 60 + TIMER_HOURS * 3600,
        Lang.bind(this, function () {
          this.provider.next(this._setWallpaper);
          return true;
        }));
    } else {
      this.timer = null;
    }
  },

  _setWallpaper: function (path) {
    const background_setting = new Gio.Settings({ schema: "org.gnome.desktop.background" });

    if (background_setting.is_writable("picture-uri")) {
      if (background_setting.set_string("picture-uri", "file://" + path)) {
        Gio.Settings.sync();
      }
    }
  }
});

@@ -380,51 +380,132 @@
 s;%0A%0A
-le
+cons
 t TIMER
-_MINUTES = 0;%0Alet TIMER_HOURS = 0;
+ = %7B%0A  minutes: 0,%0A  hours: 0,%0A%0A  toSeconds: function () %7B%0A    return this.minutes * 60 + this.hours * 3600%0A  %7D%0A%7D
 %0A%0Ale
@@ -1691,24 +1691,50 @@
 Wallpaper);%0A
+      this._resetTimer();%0A
     %7D));%0A  %7D
@@ -1951,24 +1951,24 @@
    TIMER
-_MINUTES
+.minutes
  = this.
@@ -2005,22 +2005,22 @@
    TIMER
-_HOURS
+.hours
  = this.
@@ -2057,16 +2057,18 @@
   this._
+re
 setTimer
@@ -2080,16 +2080,18 @@
  %7D,%0A%0A  _
+re
 setTimer
@@ -2190,30 +2190,20 @@
 IMER
-_HOURS + TIMER_MINUTES
+.toSeconds()
  %3E 0
@@ -2290,42 +2290,20 @@
 IMER
-_MINUTES * 60 + TIMER_HOURS * 3600
+.toSeconds()
 ,%0A  

Reset timer on Next Wallpaper press, refactoring