const UPower = imports.ui.status.power.UPower
const Main = imports.ui.main

let watching

function bind () {
  getBattery(proxy => {
    watching = proxy.connect('g-properties-changed', update)
  })
}

function unbind () {
  getBattery(proxy => {
    proxy.disconnect(watching)
  })
}

function show () {
  getBattery((proxy, icon) => {
    icon.show()
  })
}

function hide () {
  getBattery((proxy, icon) => {
    icon.hide()
  })
}

function update () {
  getBattery(proxy => {
    let isBattery = proxy.Type === UPower.DeviceKind.BATTERY
    let notDischarging = proxy.State !== UPower.DeviceState.DISCHARGING
    if (isBattery && notDischarging && proxy.Percentage === 100) {
      hide()
    } else {
      show()
    }
  })
}

function getBattery (callback) {
  let menu = Main.panel.statusArea.aggregateMenu
  if (menu && menu._power) {
    callback(menu._power._proxy, menu._power.indicators)
  }
}

/* exported init, enable, disable */

function init () { }

function enable () {
  bind()
  update()
}

function disable () {
  unbind()
  show()
}

@@ -484,23 +484,27 @@
   let is
-Battery
+Discharging
  = proxy
@@ -508,11 +508,12 @@
 oxy.
-Typ
+Stat
 e ==
@@ -531,20 +531,25 @@
 vice
-Kind.BATTERY
+State.DISCHARGING
 %0A   
@@ -553,30 +553,30 @@
     let 
-notDisc
+isFullyC
 harg
-ing
+ed
  = proxy
@@ -586,11 +586,11 @@
 ate 
-!
 ==
+=
  UPo
@@ -601,35 +601,37 @@
 DeviceState.
-DIS
+FULLY_
 CHARG
-ING
+ED
 %0A    if (isB
@@ -631,39 +631,126 @@
 if (
-isBattery && notDischarging && 
+proxy.Type !== UPower.DeviceKind.BATTERY) %7B%0A      show()%0A    %7D else if (isFullyCharged) %7B%0A      hide()%0A    %7D else if (
 prox
@@ -769,16 +769,34 @@
  === 100
+ && !isDischarging
 ) %7B%0A    

Improve logic for more edge cases