import json
import os
from pathlib import Path


class ParamsManager:
    
    def __init__(self, params_dir='params'):
        self.params_dir = params_dir
        self._ensure_dir_exists()
    
    def _ensure_dir_exists(self):
        Path(self.params_dir).mkdir(parents=True, exist_ok=True)
    
    def save(self, params, filename):
        if not filename.endswith('.json'):
            filename = filename + '.json'
        
        filepath = os.path.join(self.params_dir, filename)
        
        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(params, f, indent=2, ensure_ascii=False)
    
    def load(self, filename):
        if not filename.endswith('.json'):
            filename = filename + '.json'
        
        filepath = os.path.join(self.params_dir, filename)
        
        with open(filepath, 'r', encoding='utf-8') as f:
            params = json.load(f)
        return params
    
    def save_multiple(self, params_dict):
        filepaths = []
        for filename, params in params_dict.items():
            filepath = self.save(params, filename)
            filepaths.append(filepath)
        return filepaths
    
    def load_multiple(self, filenames):
        result = {}
        for filename in filenames:
            result[filename] = self.load(filename)
        return result
    
# from ParamsManager import ParamsManager

# # 创建参数管理器实例
# pm = ParamsManager('params')  # 参数保存在 params 文件夹中

# # 保存单个参数
# pm.save(params, 'AWG_NCO')  # 自动添加 .json 后缀

# # 加载单个参数
# params = pm.load('AWG_NCO')
