#!/usr/bin/env python
# ==============================================================
#
#  Abs:   Script for compiling pdf reports from magnet auto-checkout data
#         based on https://web.archive.org/web/20121024021221/http://e6h.de/post/11/
#         and https://tug.org/tug2019/slides/slides-ziegenhagen-python.pdf
#
#  Name:  mgnt_generate_checkout_report
#
#  Usage: mgnt_generate_checkout_report [-h] [-f FACILITY] [-e ELEMENT]
#
#  Facility: SLAC Controls (Development)
#
#  Auth:  08-Nov-2021, A. Le    (anle)
# --------------------------------------------------------------
#  Mods: (Latest to oldest)
#     11-Aug-2022, A. Le        (anle)
#         Add option to specify magnets by IOC
#     02-Jun-2022, A. Le        (anle)
#         Add verbose option
#     27-May-2022, A. Le        (anle)
#         Add options to specify magnets by area or device name
#     26-May-2022, A. Le        (anle)
#         Add verbose option
#     31-Mar-2022, A. Le        (anle)
#         Use dirname to get parent directory in local mode
#         Rename reports directory to doc
#         Rename out directory to log
#     22-Mar-2022, A. Le        (anle)
#         Add option to read JSON from local directory
#
# ==============================================================
#
from __future__ import print_function

import argparse
import io
import os
import shutil
import subprocess
import sys
import tempfile

import jinja2
from dateutil import parser

import helpers.mgnt_utilities as utilities
from helpers.mgnt_facility_agent import FacilityAgent
from mgnt_auto_checkout_data import AutoCheckoutData
from mgnt_auto_checkout_structure import AutoCheckoutStructure

template_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
latex_jinja_env = jinja2.Environment(
    block_start_string='\BLOCK{',
    block_end_string='}',
    variable_start_string='\VAR{',
    variable_end_string='}',
    comment_start_string='\#{',
    comment_end_string='}',
    line_statement_prefix='%-',
    line_comment_prefix='%#',
    trim_blocks=True,
    autoescape=False,
    loader=jinja2.FileSystemLoader(template_dir)
)

latex_escapes = {
    '\n': '\\newline ',
    'red<<': '\\textcolor{Red}{',
    '>>': '}',
    '$': '\\$',
    '_': '\\_',
    '%': '\\%',
    '#': '\\#',
    '{': '\\{',
    '}': '\\}'
}


def escape_latex(data):
    escaped_data = data
    for char, escaped_char in latex_escapes.items():
        escaped_data = escaped_data.replace(char, escaped_char)

    return escaped_data


latex_jinja_env.filters['escape_latex'] = escape_latex

# Parse arguments
arg_parser = argparse.ArgumentParser(description='Generate pdf report of magnet autocheckout')
group = arg_parser.add_mutually_exclusive_group(required=True)
group.add_argument('-f', '--facility', help='Specify facility on which to read autocheckout JSON files')
group.add_argument('-j', '--json-dir',
                   help='Read JSON from directory (default: $MGNT_REPORT/data in specified facility)')
arg_parser.add_argument('-c', '--csv', dest='csv_file', help='Get magnets from flat file instead of Oracle')
magnet_group = arg_parser.add_mutually_exclusive_group(required=True)
magnet_group.add_argument('-e', '--element', help='Generate report for specified element (ALL for all magnets)')
magnet_group.add_argument('-a', '--area', help='Generate reports for magnets in specified area')
magnet_group.add_argument('-d', '--device', help='Generate reports for magnets by device name (* and ? allowed)')
magnet_group.add_argument('-i', '--ioc', help='Check out magnets by IOC (PV or node name format OK)')
arg_parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose logging')
if len(sys.argv) == 1:
    arg_parser.print_help()
    arg_parser.exit()

args = arg_parser.parse_args()

# Get matching magnets
utilities.init(args.facility, args.json_dir, args.csv_file, args.verbose)
if args.area:
    matching_magnets = utilities.get_magnets_by('area', args.area)
elif args.element:
    if args.element == 'ALL':
        matching_magnets = utilities.get_all_magnets()
    else:
        matching_magnets = utilities.get_magnets_by('element', args.element)

elif args.device:
    matching_magnets = utilities.get_magnets_by('device', args.device, wildcard=True)
elif args.ioc:
    ioc_name = args.ioc.upper().replace('-', ':')
    matching_magnets = utilities.get_magnets_by('ioc', ioc_name)

else:
    matching_magnets = utilities.get_all_magnets()

if not matching_magnets:
    raise ValueError('No matching magnets found')

agent = FacilityAgent(args.facility)
if args.json_dir:
    json_dir = args.json_dir
else:
    json_dir = '$MGNT_REPORT/data'

data_file_list = ['{}.json'.format(magnet['element']) for magnet in matching_magnets]
if not data_file_list:
    print('Error: no magnets matching "{}"'.format(args.element))
    sys.exit(1)

# Get user information
user_name = agent.get_full_name()

# Load template from file
template = latex_jinja_env.get_template('mgnt_report_template.tex')

# Get existing data files
json_glob = os.path.join(json_dir, '*.json')
existing_json = agent.ls_basenames(json_glob)

for json_file in data_file_list:
    # Check if data file exists
    if json_file not in existing_json:
        json_path = os.path.join(json_dir, json_file)
        print('Warning: {} not found'.format(json_path))
        continue

    # Generate dictionary for report
    checkout_structure = AutoCheckoutStructure()
    checkout_data = AutoCheckoutData(args.facility, args.json_dir)
    checkout_data.load_json(json_file)
    report_dict = checkout_structure.generate_report_dict(checkout_data)

    # Process json data
    for section in report_dict['sections']:
        for subsection in section['subsections']:
            for test in subsection['tests']:
                if test['status'] is None:
                    test['status'] = 'N/A'
                elif test['status']:
                    test['status'] = '\\textcolor{Green}{PASS}'
                else:
                    test['status'] = '\\textcolor{Red}{FAIL}'

                test_datetime = parser.parse(test['datetime'])
                test['date'] = test_datetime.strftime('%m/%d/%y')

    # Combine template and variables
    document = template.render(element=report_dict['element'], device=report_dict['device'], name=user_name,
                               sections=report_dict['sections'])

    # Create temporary directory
    temp_dir = tempfile.mkdtemp()

    # Write document to latex
    latex_file = '{}.tex'.format(report_dict['element'])
    latex_dir = os.path.join(temp_dir, 'latex')
    latex_path = os.path.join(latex_dir, latex_file)
    os.mkdir(latex_dir)
    with io.open(latex_path, mode='w', encoding='utf-8') as f:
        f.write(document)

    # Create pdf from latex
    #log_dir = os.path.join(temp_dir, 'log')
    #os.mkdir(log_dir)
    #pdflatex_args = ['pdflatex', '--output-directory=' + log_dir, latex_path]
    #if args.verbose:
    #    subprocess.call(pdflatex_args)
    #else:
    #    with open(os.devnull, 'w') as devnull:
    #        subprocess.call(pdflatex_args, stdout=devnull)

    ## Move pdf to doc directory
    #pdf_file = '{}.pdf'.format(report_dict['element'])
    #pdf_path = os.path.join(log_dir, pdf_file)
    #doc_dir = os.path.join(temp_dir, 'doc')
    #dest_pdf_path = os.path.join(doc_dir, pdf_file)
    #os.mkdir(doc_dir)
    #shutil.move(pdf_path, dest_pdf_path)

    # Move doc, latex, and log to remote server
    if args.facility:
        parent_dir = os.path.dirname(json_dir)
    else:
        parent_dir = os.path.abspath(os.path.join(json_dir, os.pardir))

    agent.rsync_to_remote(temp_dir + '/', parent_dir + '/')

    # Delete temporary directory
    shutil.rmtree(temp_dir)
