"""
Handles bulk processing operations for multiple files
"""

import os
from pathlib import Path
from typing import List, Tuple


class BulkProcessor:
    def __init__(self, parent):
        self.parent = parent
        self.bulk_files: List[Tuple[str, str]] = []
        self.bulk_index = 0

    def run_bulk_command(self, base_command: List[str]):
        """Run chdman command on multiple files in a directory"""
        directory = self.parent.param_widgets.get("input", QLineEdit()).text()
        if not directory or not os.path.isdir(directory):
            return False

        extensions = {
            "createraw": [".img", ".iso"],
            "createhd": [".img", ".iso"],
            "createcd": [".cue", ".iso"],
            "createdvd": [".iso"],
            "createld": [".cue", ".iso"],
            "extractraw": [".chd"],
            "extracthd": [".chd"],
            "extractcd": [".chd"],
            "extractdvd": [".chd"],
            "extractld": [".chd"],
            "verify": [".chd"],
            "info": [".chd"],
            "dumpmeta": [".chd"],
            "copy": [".chd"],
            "addmeta": [".chd"],
            "delmeta": [".chd"],
        }

        command_name = self.parent.command_combo.currentText()
        supported_exts = extensions.get(command_name, [])
        files = [
            f
            for f in os.listdir(directory)
            if os.path.splitext(f)[1].lower() in supported_exts
        ]

        if not files:
            return False

        self.parent.progress_bar.setMaximum(len(files))
        self.parent.progress_bar.setValue(0)
        self.parent.progress_bar.setVisible(True)

        self.bulk_files = [(os.path.join(directory, f), command_name) for f in files]
        self.bulk_index = 0
        self.run_next_bulk_command(base_command)
        return True

    def run_next_bulk_command(self, base_command: List[str]):
        """Run the next command in the bulk queue"""
        if self.bulk_index >= len(self.bulk_files):
            self.parent.progress_bar.setVisible(False)
            self.parent.output_text.append("\nBulk processing completed.")
            return

        file_path, command_name = self.bulk_files[self.bulk_index]
        command = base_command.copy()
        command.append("--input")
        command.append(file_path)

        if command_name in [
            "createraw",
            "createhd",
            "createcd",
            "createdvd",
            "createld",
            "extractraw",
            "extracthd",
            "extractcd",
            "extractdvd",
            "extractld",
            "dumpmeta",
            "copy",
        ]:
            output_ext = self.parent.get_output_extension(command_name)
            output_path = os.path.splitext(file_path)[0] + output_ext
            command.append("--output")
            command.append(output_path)

        self.parent.output_text.append(f"\nProcessing: {' '.join(command)}")
        self.parent.process.start(command[0], command[1:])
