#!/usr/bin/env python

#imports
import i3ipc,re,subprocess,time
# <Settings> 
I3_CONNECTION            = i3ipc.Connection()
PRIMARY_SOUND_DEV        = "alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo"
SECONDARY_SOUND_DEV      = "alsa_output.pci-0000_00_1b.0.analog-stereo"
# Colors in hex
WS_COLOR_UNDERLINE       = "#62AFEE"
WS_COLOR_NORMAL          = "#dddddd"
WS_COLOR_HIGHLIGHT       = "#ffffff"
WS_COLOR_ICON            = "#62AFEE"
COLOR_UNDERLINE          = "%{F#62AFEE}"
COLOR_ICON               = "%{F#62AFEE}"
COLOR_TEXT               = "%{F#dddddd}"
COLOR_RED                = "%{F#FF0000}"
END_COLOR                = "%{F-}"
# Align bar objects
LEFT                     = "%{l}"
CENTER                   = "%{c}"
RIGHT                    = "%{r}"
GAP                      = "%{O10}"
SEP                      = " "
UNDERLINE_INIT           = "%{+u}%{U#62AFEE}"
UNDERLINE_END            = "%{U-}%{-u}"
OVERLINE_INIT            = "%{+o}%{O#62AFEE}"
OVERLINE_END             = "%{O-}%{-o}"
#icons - AwesomeFont
WORKSPACES               = ""
TIME                     = ""
DATE                     = ""
VOLUME                   = ""
HEADPHONES               = ""
PLAY                     = ""
PAUSE                    = ""
NEXT                     = ""
BACK                     = ""
STOP                     = ""
WALLPAPER                = ""
BATTERY_CRITICAL         = ""   #   5%
BATTERY_LOW              = ""   #  25%
BATTERY_HALF             = ""   #  50%
BATTERY_ALMOST           = ""   #  75%
BATTERY_FULL             = ""   # 100%
WIRED                    = ""
#</Settings>


def get_volume(pdev,sdev):
  raw = run("pactl list sinks"); icon = ""; action = "" ; nn = 0
  if pdev in raw:
    dev = pdev; icon = HEADPHONES; nn = 150
    actions = "%{A1:pavucontrol:}%{A4:amixer -q -D sysdefault sset Headphone 10%+ unmute:}%{A5:amixer -q -D sysdefault sset Headphone 10%- unmute:}"
  elif sdev in raw:
    dev = sdev; icon = VOLUME; nn = 100
    actions = "%{A1:pavucontrol:}%{A4:amixer -q sset Master 10%+ unmute:}%{A5:amixer -q sset Master 10%- unmute:}"
  else:
    return COLOR_ICON + VOLUME + COLOR_TEXT + "nosnd"

  find = raw.splitlines()[8]
  sraw = raw.splitlines()
  y = 0; x = 0; n = 0
  for x in range (0,nn):
    if dev in sraw[x]:
      y = x
      for y in range (x,nn):
        if find in sraw[y]:
          n = y
          break
      break
  num = n+1
  s = sraw[num:num+1][0]
  m = sraw[num-1:num][0]

  value = s[s.find("/ ")+2:s.find("%")]
  if m[m.find("Звук выключен: ")+15:]=="yes":
    value = "muted"
    return COLOR_ICON + actions + icon + "%{A}%{A}%{A}" + COLOR_TEXT + value
  elif m[m.find("Звук выключен: ")+15:]=="no":
    return COLOR_ICON + actions + icon + "%{A}%{A}%{A}" + COLOR_TEXT + value +"%"
