#!/usr/bin/env python3
#
# Copyright (C) 2016 James Murphy
# Licensed under the GPL version 2 only
#
# A battery indicator blocklet script for i3blocks

import re
from subprocess import check_output

def get_symbol(name, color='white'):
    SYMBOLS = {'LIGHTNING': 0xf0e7,
               'PLUG': 0xf1e6,
               'BATTERY': 0xf240,
               'BATTERY2': 0xf241,
               'BATTERY3': 0xf242,
               'BATTERY4': 0xf243,
               'BATTERY5': 0xf244,
               'QUESTION': 0xf128}

    code = SYMBOLS[name]
    return u"<span color='{}'><span font='FontAwesome'>{}</span></span>".format(color, chr(code))


def battery_colors(percent):
    if percent < 10:
        # exit code 33 will turn background red
        return "#FFFFFF"
    if percent < 20:
        return "#FF3300"
    if percent < 30:
        return "#FF6600"
    if percent < 40:
        return "#FF9900"
    if percent < 50:
        return "#FFCC00"
    if percent < 60:
        return "#FFFF00"
    if percent < 70:
        return "#FFFF33"
    if percent < 80:
        return "#FFFF66"
    return "#FFFFFF"


def get_status():
    status = check_output(['acpi'], universal_newlines=True)

    if not status:
        # stands for no battery found
        return "Unknown", None, 100

        #fulltext = "<span color='red'><span font='FontAwesome'>\uf00d \uf240</span></span>"
        #percentleft = 100
    else:
        # if there is more than one battery in one laptop, the percentage left is 
        # available for each battery separately, although state and remaining 
        # time for overall block is shown in the status of the first battery 
        try:
            time_left = re.findall(r"(\d+:\d+)", status)[0]
        except IndexError:
            time_left = ""

        state = re.findall(r": (.*), .+%", status)
        state = [stat for stat in state if not stat == 'Unknown']
        state = 'Unknown' if len(state) == 0 else state[0]

        percent = [int(perc) for perc in re.findall(r"(\d+)%", status)]
        percent = int(float(sum(percent)) / len(percent))

        return state, time_left, percent


state, timeleft, percentleft = get_status()

if state == "Full" or (state == "Unknown" and percentleft > 90):
    fulltext = get_symbol('PLUG') + " "
    timeleft = ""
elif state == "Charging":
    fulltext = get_symbol('LIGHTNING', 'yellow') + " "
elif state == "Discharging":
    fulltext = ""
else:
    fulltext = get_symbol('QUESTION', 'yellow') + " "
    timeleft = ""

if percentleft >= 80:
    fulltext += get_symbol('BATTERY')
if percentleft < 80:
    fulltext += get_symbol('BATTERY2')
elif percentleft < 60:
    fulltext += get_symbol('BATTERY3')
elif percentleft < 30:
    fulltext += get_symbol('BATTERY4')
elif percentleft < 10:
    fulltext += get_symbol('BATTERY5')

fulltext += " "

if percentleft < 99:
    fulltext += '<span color="{}">{}% {}</span>'.format(battery_colors(percentleft), percentleft, timeleft)

print(fulltext)
if percentleft < 10:
    exit(33)

