import logging
import subprocess
import time
from multiprocessing import Process

from PyQt5.QtCore import QThread


class MusicPlayer(QThread):

    def __init__(self, parent=None):
        QThread.__init__(self, parent)
        self.songs_list = []

    def run(self):
        try:
            player = Process(target=self.play_list, args=())
            player.start()
            n = 0
            while True:
                time.sleep(0.1)
                n += 1
                print(n)
                if n == 50:
                    # print(1, self.get_next_start_time())
                    subprocess.run('pkill play', shell=True)
                    player.terminate()

                    break
        except Exception as e:
            logging.info(f'При попытке воспроизведения музыки возникла ошибка {e}')

    def play_song(self):
        subprocess.run('su - user -c "play /home/user/Загрузки/Ruki_Vverh!_Bez_lyubvi.mp3"',
                       shell=True)

    def play_list(self):
        logging.info(self.songs_list)
        print(self.songs_list)
        # command = f'play {" ".join(self.songs_list)}'
        # run_command(command)
