# -*- coding: utf-8 -*-

import traceback

from odoo import fields, models, tools
from odoo.exceptions import ValidationError, UserError


class IrActionsReport(models.Model):
    _inherit = "ir.actions.report"

    fusion_preview_mode = fields.Selection([
        ('default', 'Use User Preference'),
        ('preview', 'Always Preview'),
        ('download', 'Always Download'),
        ('auto_print', 'Always Auto-Print'),
    ], string='PDF Preview Mode', default='default',
       help='Override user-level PDF preview settings for this specific report.')

    def _render_qweb_pdf(self, report_ref, res_ids=None, data=None):
        if tools.config['test_enable'] or tools.config['test_file']:
            return super()._render_qweb_pdf(report_ref, res_ids=res_ids, data=data)

        result = False, False
        message_error = False
        try:
            result = super()._render_qweb_pdf(report_ref, res_ids=res_ids, data=data)
        except (UserError, ValidationError) as e:
            message_error = str(e)
        except Exception:
            message_error = traceback.format_exc()

        if message_error:
            report_ref = "fusion_pdf_preview.report_error_catcher"
            data = {'error': message_error}
            result = super()._render_qweb_pdf(report_ref, res_ids=[], data=data)
        return result
