from infi.systray import SysTrayIcon
import sys
import time
import os
import requests
#import pkg_resources
#import pkg_resources.py2_warn
#from playsound import playsound
from pydub import AudioSegment
from pydub.playback import play
from configparser import ConfigParser

curDir = os.path.dirname(os.path.realpath(__file__))

alert_lobby = AudioSegment.from_mp3(curDir + "\\alert_lobby.wav")
alert_start = AudioSegment.from_mp3(curDir + "\\alert_start.wav")

#test
#play(alert)

host_online = [0,0]
host_status = [0,0]
host_players = ""
host_online_old = [-1,-1]
host_active = 0
host_active_old = 0
antirepeater = 1

#config part
config = ConfigParser()
config.read(curDir + "\\dots_tray.ini")
lobby_people_alert_count = config.getint('settings', 'lobby_people_alert_count')
volume = config.getint('settings', 'volume')
b_play_alert_lobby = config.getboolean('settings', 'play_alert_lobby')
b_play_alert_start = config.getboolean('settings', 'play_alert_start')

alert_lobby = alert_lobby + volume #reduce or increase volume
alert_start = alert_start + volume #reduce or increase volume

def on_quit_callback(systray):
    print("exiting...")
    os._exit(0)

systray = SysTrayIcon(curDir + "\\ico\\dots3.ico", "", "", on_quit=on_quit_callback)
systray.start()
time.sleep(3)

while 1:
    
    try:
        response = requests.get("http://thdots.ru/old/online1.php")
        host_online[0] = response.text.split()[0]
        host_status[0] = response.text.split()[2]
        response = requests.get("http://thdots.ru/old/online2.php")
        host_online[1] = response.text.split()[0]
        host_status[1] = response.text.split()[2]
    except:
        systray.update(icon=curDir + "\\ico\\dots3.ico")
        systray.update(hover_text="Ошибка соединения. Повторная попытка через 15 секунд")
        time.sleep(15)
        continue
    
    if host_online[0] >= host_online[1]:
        host_active = 0
    else:
        host_active = 1
          
    if host_online[host_active] != host_online_old[host_active]  or ( host_active != host_active_old  ):
        response = requests.get("http://thdots.ru/old/players" + str(host_active + 1) + ".php")
        host_players = response.text.splitlines()
        host_players = list(filter(("").__ne__, host_players))
        i = 0
        while i < len(host_players):
            host_players[i] = host_players[i][6:]
            if len(host_players[i]) > 8:
                host_players[i] = host_players[i][:6] + "..."
            i += 1
        host_players = "\n".join(host_players)
        systray.update(hover_text=host_players)
        
        systray.update(icon=curDir + "\\ico\\" + str(host_active + 1) + "_" + str(host_online[host_active]) + ".ico")
        
    print("host_active: " + str(host_active) + " host_online_old: " + str(host_online_old[host_active]) + " host_online: " + str(host_online[host_active]))
    
    if b_play_alert_lobby is True:    
        if int(host_online_old[host_active]) < lobby_people_alert_count and int(host_online[host_active]) >= lobby_people_alert_count:
            print("alert_lobby")
            try:
                play(alert_lobby)
            except:
                pass
            
    if b_play_alert_start is True:    
        if int(host_status[host_active]) == 1 :
            if antirepeater == 1 :
                print("alert_start")
                try:
                    play(alert_start)
                except:
                    pass
                antirepeater = 0    
        else:
            antirepeater = 1 #лобби - сбрасываем
          
    
    host_online_old[host_active] = host_online[host_active]
    host_active_old = host_active
    time.sleep(5)


