import yaml
import os

class Config:
    def __init__(self):
        self.config = {}
        self.load_config()

    def load_config(self):
        """Load configuration from YAML files"""
        config_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'config')
        
        # Load hardware configuration
        hardware_config_path = os.path.join(config_dir, 'hardware_config.yaml')
        if os.path.exists(hardware_config_path):
            with open(hardware_config_path, 'r') as f:
                self.config['hardware'] = yaml.safe_load(f)

        # Load mining configuration
        mining_config_path = os.path.join(config_dir, 'mining_config.yaml')
        if os.path.exists(mining_config_path):
            with open(mining_config_path, 'r') as f:
                self.config['mining'] = yaml.safe_load(f)

    def get(self, section, key, default=None):
        """Get configuration value"""
        return self.config.get(section, {}).get(key, default)

    def set(self, section, key, value):
        """Set configuration value"""
        if section not in self.config:
            self.config[section] = {}
        self.config[section][key] = value

    def save(self):
        """Save configuration to files"""
        config_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'config')
        
        # Save hardware configuration
        hardware_config_path = os.path.join(config_dir, 'hardware_config.yaml')
        with open(hardware_config_path, 'w') as f:
            yaml.dump(self.config.get('hardware', {}), f)

        # Save mining configuration
        mining_config_path = os.path.join(config_dir, 'mining_config.yaml')
        with open(mining_config_path, 'w') as f:
            yaml.dump(self.config.get('mining', {}), f)
