#!/usr/bin/env python3
# 模拟规划完成所有记忆单元集文件的最小时间
import os
import particles as pt
import datetime
import pathlib
import math
import time

MINIMAL_REPEATATION = 2
FILTER = "ALL"
WORST_EF = 5
PAYLOAD = 16

print("SM-2 任务预规划实用程序")
print(f"运行时刻: {datetime.datetime.now()}")
print(f" > 筛选器模式: {FILTER}")
print(f" > 最小重复次数设置: {MINIMAL_REPEATATION}")
print(f" > 最坏难度系数: {WORST_EF}")
print(f" > 单日单元负荷: {PAYLOAD}")

print("--------")

filelist = []

if FILTER == "ALL":
    for i in os.listdir('./nucleon'):
        if "toml" in i:
            print("扫描到记忆单元集: " + i)
            filelist.append(i)
    print(f"共需记忆 {len(filelist)} 个记忆单元集")
else:
    filelist = [FILTER]

print("--------")
time_counter = 0
text_counter = 0
content_counter = 0
for i in filelist:
    print(f"处理: {i}")
    nucu = pt.NucleonUnion(pathlib.Path("./nucleon/" + i))
    print(f"记忆单元数: {len(nucu.nucleons)}")
    content_cnt = 0
    metadata_trsl_cnt = 0
    metadata_note_cnt = 0
    metadata_kwrd_cnt = 0
    for i in nucu.nucleons:
        if i.content[-1] != "/":
            print('检测到不规范字符串')
            print(i.content)
        content_cnt += len(i.content)
        metadata_trsl_cnt += len(str(i.metadata["translation"]))
        metadata_kwrd_cnt += len(str(i.metadata["keyword_note"]))
        metadata_note_cnt += len(str(i.metadata["note"]))
    print(" - 原文文字数: " + str(content_cnt))
    metadata_cnt = metadata_kwrd_cnt + metadata_note_cnt + metadata_trsl_cnt
    print(" - 元数据字数: " + str(metadata_cnt) + f"\n = {metadata_trsl_cnt}[翻译] + {metadata_kwrd_cnt}[关键词] + {metadata_note_cnt}[笔记]")
    print(f" - 总文字数: {content_cnt + metadata_cnt}")
    print(f"独占记忆时间: {len(nucu.nucleons) / PAYLOAD} -> {math.ceil(len(nucu.nucleons) / PAYLOAD)}")
    #time.sleep(0.1)
    text_counter += (content_cnt + metadata_cnt)
    content_counter += content_cnt
    time_counter += math.ceil(len(nucu.nucleons) / PAYLOAD)
    print("--------")
for i in range(MINIMAL_REPEATATION):
    print(f"若按计划进行, 最长需要 {time_counter + (i + 1) * 6} 天完成全部内容的第 {i + 1} 次记忆")
print(f"规划包含 {content_counter} 字, 附加了 {text_counter - content_counter} 字的元数据内容, 共计 {text_counter} 字")
