from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QPushButton
from job_thread import JobThread

class JobItem(QWidget):
    def __init__(self, name, folder1, folder2, interval):
        super().__init__()
        self.name = name
        self.folder1 = folder1
        self.folder2 = folder2
        self.interval = interval
        self.setup_thread()
        self.setup_ui()

    def setup_ui(self):
        layout = QHBoxLayout()
        self.setLayout(layout)

        self.name_label = QLabel(self.name)
        self.name_label.setObjectName('jobitem')
        self.play_button = QPushButton("Play")
        self.sync_button = QPushButton("Sync")
        self.delete_button = QPushButton("Delete")

        layout.addWidget(self.name_label)
        layout.addWidget(self.play_button)
        layout.addWidget(self.sync_button)
        layout.addWidget(self.delete_button)

        self.setup_connections()

    def setup_connections(self):
        self.play_button.clicked.connect(self.toggle_play)
        self.sync_button.clicked.connect(self.run_sync)
        self.thread.current_log.connect(self.update_log)

    def setup_thread(self):
        self.thread = JobThread(self)

    def toggle_play(self):
        if self.thread.is_running:
            self.thread.stop()
            self.play_button.setText("Play")
            self.play_button.setStyleSheet('''
            QPushButton {
                background-color: #558DED;
                color: white;
                border: none;
                padding: 5px 10px;
                border-radius: 5px;
                font-size: 14px;
            }
            QPushButton:hover {
                background-color: #55ED86;
            }  
                                           ''')
        else:
            self.thread.start()
            self.play_button.setText("Pause")
            self.play_button.setStyleSheet('''
            QPushButton {
                background-color: #55ED86;
                color: white;
                border: none;
                padding: 5px 10px;
                border-radius: 5px;
                font-size: 14px;
            }
            QPushButton:hover {
                background-color: #558DED;
            }  
                                           ''')

    def run_sync(self):
        if not self.thread.is_running:
            self.thread.sync()

    def update_log(self, msg, fg, bg):
        self.window().add_log(msg, fg, bg)
