# _*_ encoding：utf-8 _*_
import pdfplumber
from openpyxl import load_workbook
from os import scandir, remove
from os.path import exists
from sys import argv


def traversal_files(path, w2t):
    # 功能：以列表的形式分别返回指定路径下的文件和文件夹，不包含子目录
    # 参数：路径
    # 返回值：路径下的文件夹列表 路径下的文件列表
    if not exists(path):
        msg = f'数据文件夹{path}不存在，请确认后重试......'
        w2t(msg, 0, 1, 'red')
    else:
        dirs = files = []
        for item in scandir(path):
            if item.is_dir():
                dirs.append(item.path)
            elif item.is_file():
                files.append(item.path)

        return dirs, files


def p_iso(file, p_files, ws, tmpfile):
    p_files.append(file)

    pdf = pdfplumber.open(file)
    with open(tmpfile, mode='w', encoding='utf-8') as fb:
        for page in pdf.pages:
            fb.write(page.extract_text())

    with open(tmpfile, mode='r', encoding='utf-8') as fb:
        lines = fb.readlines()
        lines = [line for line in lines if not line.startswith('Page ')]
        for line in lines:
            if line.strip() == "Pose Accuracy and Repeatability":
                index = lines.index(line)
                ws.cell(row=3, column=7).value = float(lines[index+4].split()[1])
                ws.cell(row=4, column=7).value = float(lines[index+5].split()[1])
                ws.cell(row=5, column=7).value = float(lines[index+6].split()[1])
                ws.cell(row=6, column=7).value = float(lines[index+7].split()[1])
                ws.cell(row=7, column=7).value = float(lines[index+8].split()[1])

                ws.cell(row=8, column=7).value = float(lines[index+4].split()[2])
                ws.cell(row=9, column=7).value = float(lines[index+5].split()[2])
                ws.cell(row=10, column=7).value = float(lines[index+6].split()[2])
                ws.cell(row=11, column=7).value = float(lines[index+7].split()[2])
                ws.cell(row=12, column=7).value = float(lines[index+8].split()[2])
            elif line.strip() == 'Pose Accuracy Variation':
                index = lines.index(line)
                ws.cell(row=13, column=7).value = float(lines[index+4].split()[1])
                ws.cell(row=14, column=7).value = float(lines[index+5].split()[1])
                ws.cell(row=15, column=7).value = float(lines[index+6].split()[1])
            elif line.strip() == 'Distance Accuracy':
                index = lines.index(line)
                ws.cell(row=16, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=17, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Stabilisation Time and Overshoot':
                index = lines.index(line)
                ws.cell(row=18, column=7).value = float(lines[index + 7].split()[3])
                ws.cell(row=19, column=7).value = float(lines[index + 7].split()[2])
            elif line.strip() == 'Velocity Accuracy and Repeatability':
                index = lines.index(line)
                ws.cell(row=20, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=21, column=7).value = float(lines[index + 4].split()[2])
                ws.cell(row=22, column=7).value = float(lines[index + 4].split()[3])
            elif line.strip()[:31] == 'Path Accuracy and Repeatability':
                index = lines.index(line)
                ws.cell(row=29, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=30, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Corner Overshoot and Roundoff':
                index = lines.index(line)
                ws.cell(row=35, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=36, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Robot Weaving':
                index = lines.index(line)
                ws.cell(row=41, column=7).value = float(lines[index + 4].split()[2])
                ws.cell(row=42, column=7).value = float(lines[index + 4].split()[3])
                ws.cell(row=43, column=7).value = float(lines[index + 4].split()[4])
            else:
                pass


def p_iso_100(file, p_files, ws, tmpfile):
    p_files.append(file)

    pdf = pdfplumber.open(file)
    with open(tmpfile, mode='w', encoding='utf-8') as fb:
        for page in pdf.pages:
            fb.write(page.extract_text())

    with open(tmpfile, mode='r', encoding='utf-8') as fb:
        lines = fb.readlines()
        lines = [line for line in lines if not line.startswith('Page ')]
        for line in lines:
            if line.strip() == 'Velocity Accuracy and Repeatability':
                index = lines.index(line)
                ws.cell(row=26, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=27, column=7).value = float(lines[index + 4].split()[2])
                ws.cell(row=28, column=7).value = float(lines[index + 4].split()[3])
            elif line.strip()[:31] == 'Path Accuracy and Repeatability':
                index = lines.index(line)
                ws.cell(row=33, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=34, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Corner Overshoot and Roundoff':
                index = lines.index(line)
                ws.cell(row=39, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=40, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Robot Weaving':
                index = lines.index(line)
                ws.cell(row=47, column=7).value = float(lines[index + 4].split()[2])
                ws.cell(row=48, column=7).value = float(lines[index + 4].split()[3])
                ws.cell(row=49, column=7).value = float(lines[index + 4].split()[4])
            else:
                pass


def p_iso_1000(file, p_files, ws, tmpfile):
    p_files.append(file)

    pdf = pdfplumber.open(file)
    with open(tmpfile, mode='w', encoding='utf-8') as fb:
        for page in pdf.pages:
            fb.write(page.extract_text())

    with open(tmpfile, mode='r', encoding='utf-8') as fb:
        lines = fb.readlines()
        lines = [line for line in lines if not line.startswith('Page ')]
        for line in lines:
            if line.strip() == 'Velocity Accuracy and Repeatability':
                index = lines.index(line)
                ws.cell(row=23, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=24, column=7).value = float(lines[index + 4].split()[2])
                ws.cell(row=25, column=7).value = float(lines[index + 4].split()[3])
            elif line.strip()[:31] == 'Path Accuracy and Repeatability':
                index = lines.index(line)
                ws.cell(row=31, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=32, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Corner Overshoot and Roundoff':
                index = lines.index(line)
                ws.cell(row=37, column=7).value = float(lines[index + 4].split()[1])
                ws.cell(row=38, column=7).value = float(lines[index + 4].split()[2])
            elif line.strip() == 'Robot Weaving':
                index = lines.index(line)
                ws.cell(row=44, column=7).value = float(lines[index + 4].split()[2])
                ws.cell(row=45, column=7).value = float(lines[index + 4].split()[3])
                ws.cell(row=46, column=7).value = float(lines[index + 4].split()[4])
            else:
                pass


def main(path, w2t):
    dirs, files = traversal_files(path, 1)

    try:
        wb = load_workbook(path + "/iso-results.xlsx")
        ws = wb.active
        for i in range(3, 50):
            ws.cell(row=i, column=7).value = None
        tmpfile = f"{path}\\data.txt"
    except Exception as Err:
        w2t(f"发生错误：{Err}", 0, 2, 'red')

    p_files = []
    for file in files:
        if file.endswith('.pdf') and file.split('\\')[-1] == 'ISO.pdf':
            w2t(f"正在处理{file}......")
            p_iso(file, p_files, ws, tmpfile)
            w2t(f"文件{file}已处理完毕。\n")

        elif file.endswith('.pdf') and file.split('\\')[-1] == 'ISO-V100.pdf':
            w2t(f"正在处理{file}......")
            p_iso_100(file, p_files, ws, tmpfile)
            w2t(f"文件{file}已处理完毕。\n")

        elif file.endswith('.pdf') and file.split('\\')[-1] == 'ISO-V1000.pdf':
            w2t(f"正在处理{file}......")
            p_iso_1000(file, p_files, ws, tmpfile)
            w2t(f"文件{file}已处理完毕。\n")

        else:
            pass
    wb.save(path + '/iso-results.xlsx')
    wb.close()

    if len(p_files) == 0:
        w2t(f"目录 {path} 下没有需要处理的文件，需要确认......", 0, 3, 'red')
    else:
        remove(tmpfile)
        w2t("------------------------------------------")
        w2t("所有文件均已处理完毕！")


if __name__ == '__main__':
    main(path=argv[1], w2t=argv[2])
