import configparser

# Erstelle eine Parser-Klasse für INI-Dateien
class ParameterParser:
    def __init__(self, config_file='config.ini'):
        self.config_file = config_file
        self.config = configparser.ConfigParser()
        self.config.read(self.config_file)

        # Ensure 'Parameters' and 'Video' sections exist
        if 'Parameters' not in self.config:
            self.config['Parameters'] = {}
        if 'Video' not in self.config:
            self.config['Video'] = {}

    def get_parameters(self):
        # Lese die Parameter aus der Konfigurationsdatei
        try:
            alpha = self.config.getfloat('Parameters', 'alpha')
            cutoff = self.config.getfloat('Parameters', 'cutoff')
            low = self.config.getfloat('Parameters', 'low')
            high = self.config.getfloat('Parameters', 'high')
            chromAttenuation = self.config.getfloat('Parameters', 'chromAttenuation')
            mode = self.config.getint('Parameters', 'mode')
            
            # Read video parameters
            width = self.config.getint('Video', 'width', fallback=1280)
            height = self.config.getint('Video', 'height', fallback=720)
            fps = self.config.getint('Video', 'fps', fallback=30)

            return {
                "alpha": alpha,
                "cutoff": cutoff,
                "low": low,
                "high": high,
                "chromAttenuation": chromAttenuation,
                "mode": mode,
                "width": width,
                "height": height,
                "fps": fps
            }
        except Exception as e:
            print(f"Error reading config: {e}")
            return None

    def set_parameters(self, alpha, cutoff, low, high, chromAttenuation, mode, width, height, fps):
        # Schreibe die Parameter in die Konfigurationsdatei
        self.config['Parameters'] = {
            'alpha': str(alpha),
            'cutoff': str(cutoff),
            'low': str(low),
            'high': str(high),
            'chromAttenuation': str(chromAttenuation),
            'mode': str(mode)
        }

        # Save video parameters
        self.config['Video'] = {
            'width': str(width),
            'height': str(height),
            'fps': str(fps)
        }

        with open(self.config_file, 'w') as configfile:
            self.config.write(configfile)

# Beispiel: Erstellen und Speichern von Parametern
if __name__ == "__main__":
    parser = ParameterParser()
    
    # Beispielwerte speichern
    parser.set_parameters(0.5, 1.0, 0.2, 1.5, 0.8, 1, 1280, 720, 30)

    # Parameter auslesen
    parameters = parser.get_parameters()
    print(parameters)
