# reactor.py 设计改进型 尚未完成功能重构
import particles as pt
import auxiliary as aux
import time
from typing import List

class BaseScheduler():
    "调度器接口"
    
    def revisor(self, electron: pt.Electron, quality: int):
        """由 quality 更新电子的记忆参数"""
        pass
    
    def get_atoms_for_review(self, electron_file: pt.ElectronUnion):
        """从电子文件中筛选出当前需要复习的所有原子"""
        pass
    
    def get_atoms_for_learning(self, nucleon_file: pt.NucleonUnion, limit: int):
        """从核子文件中获取待学习的新原子"""
        pass
    
class SM2Scheduler(BaseScheduler):
    "基于 SM-2 的调度器实现"
    def revisor(self, electron: pt.Electron, quality: int, is_new_activation):
        print(f"REVISOR: {quality}, {is_new_activation}")
        if quality == -1:
            return -1

        electron.metadata['efactor'] = electron.metadata['efactor'] + (
            0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02)
        )
        electron.metadata['efactor'] = max(1.3, electron.metadata['efactor'])

        if quality < 3:
            # 若保留率低于 3，重置重复次数
            electron.metadata['rept'] = 0
            electron.metadata['interval'] = 0  # 设为0，以便下面重新计算 I(1)
        else:
            electron.metadata['rept'] += 1

        electron.metadata['real_rept'] += 1

        if is_new_activation:  # 初次激活
            electron.metadata['rept'] = 0
            electron.metadata['efactor'] = 2.5

        if electron.metadata['rept'] == 0:  # 刚被重置或初次激活后复习
            electron.metadata['interval'] = 1  # I(1)
        elif electron.metadata['rept'] == 1:
            electron.metadata['interval'] = 6  # I(2) 经验公式
        else:
            electron.metadata['interval'] = round(
                electron.metadata['interval'] * electron.metadata['efactor']
            )

        electron.metadata['last_date'] = aux.get_daystamp()
        electron.metadata['next_date'] = aux.get_daystamp() + electron.metadata['interval']
        electron.metadata['last_modify'] = time.time()

    def get_atoms_for_learning(self, nucleon_file: pt.NucleonUnion, limit: int):
        def electron_dict_get_fallback(key) -> pt.Electron:
            value = self.electron_dict.get(key)
            # 如果值不存在，则设置默认值
            if value is None:
                value = pt.Electron(key, {})  # 获取默认值
                self.electron_dict[key] = value  # 将默认值存入字典
                electron_file.sync()
            return value  # 返回获取的值（可能是默认值）

        for i in nucleon_file.nucleons:
            if i.metadata

    def get_atoms_for_review(self, electron_file: pt.ElectronUnion):
        return super().get_atoms_for_review(electron_file)
    
class FSRSScheduler():
    "基于 FSRS 的调度器实现"