"""
Usage:
python scripts/annotate.py /home/iriley/Documents/pdf/scanned/10.pdf 5 --type table --show
python scripts/annotate.py /home/iriley/Documents/pdf/scanned/10.pdf 5 --type redaction --show
python scripts/annotate.py /home/iriley/Documents/pdf/scanned/10.pdf 5 --type layout --show
python scripts/annotate.py /home/iriley/Documents/pdf/scanned/10.pdf 5 --type figure --show
"""

import argparse

from cv_analysis.utils.display import show_image
from cv_analysis.utils.draw import draw_contours, draw_rectangles
from cv_analysis.utils.open_pdf import open_pdf
from cv_analysis.utils.visual_logging import vizlogger


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("pdf_path")
    parser.add_argument("--page_index", type=int, default=0)
    parser.add_argument("--type", choices=["table", "redaction", "layout", "figure"], default="table")
    parser.add_argument("--show", action="store_true", default=False)
    args = parser.parse_args()
    return args


def annotate_page(page_image, analysis_function, drawing_function, name="tmp.png", show=True):
    result = analysis_function(page_image)
    page_image = drawing_function(page_image, result)
    vizlogger.debug(page_image, name)
    show_image(page_image)


if __name__ == "__main__":
    args = parse_args()
    page = open_pdf(args.pdf_path, first_page=args.page_index, last_page=args.page_index)[0]
    name = f"{args.type}_final_result.png"
    draw = draw_rectangles
    if args.type == "table":
        from cv_analysis.table_parsing import parse_tables as analyze
    elif args.type == "redaction":
        from cv_analysis.redaction_detection import find_redactions as analyze

        draw = draw_contours
    elif args.type == "layout":
        from cv_analysis.layout_parsing import parse_layout as analyze
    elif args.type == "figure":
        from cv_analysis.figure_detection.figure_detection import detect_figures
        analyze = detect_figures
    annotate_page(page, analyze, draw, name=name, show=args.show)
