# Copyright © 2024 Garazd Creation (https://garazd.biz)
# @author: Yurii Razumovskyi (support@garazd.biz)
# @author: Iryna Razumovska (support@garazd.biz)
# License OPL-1 (https://www.odoo.com/documentation/16.0/legal/licenses.html).

from odoo import _, api, Command, fields, models
from odoo.exceptions import UserError


class ProductLabelLayout(models.TransientModel):
    _inherit = 'product.label.layout'

    @api.model
    def default_get(self, fields_list):
        # flake8: noqa: E501
        default_vals = super(ProductLabelLayout, self).default_get(fields_list)
        if 'garazd_label_template_id' not in default_vals:
            default_vals['garazd_label_template_id'] = self.env.user.print_label_template_id.id
        default_vals['garazd_allowed_template_ids'] = [Command.set(self.env['print.product.label.template']._get_user_allowed_templates().ids)]
        return default_vals

    use_alternative_template = fields.Boolean(string='Custom Templates')
    garazd_label_template_id = fields.Many2one(
        comodel_name='print.product.label.template',
        string='Label Template',
    )
    garazd_allowed_template_ids = fields.Many2many(
        comodel_name='print.product.label.template',
        compute='_compute_garazd_allowed_template_ids',
        string='Allowed Templates',
        help='Technical field to restrict allowed for the current user templates.',
    )
    garazd_allowed_template_count = fields.Integer(compute='_compute_garazd_allowed_template_ids')
    garazd_show_template_limit = fields.Integer(compute='_compute_garazd_allowed_template_ids')

    @api.depends_context('uid')
    @api.depends('use_alternative_template')
    def _compute_garazd_allowed_template_ids(self):
        allowed_templates = self.env['print.product.label.template']._get_user_allowed_templates()
        self.garazd_allowed_template_ids = [Command.set(allowed_templates.ids)]
        self.garazd_allowed_template_count = len(allowed_templates)
        # flake8: noqa: E501
        self.garazd_show_template_limit = self.env['ir.config_parameter'].sudo().get_param('garazd_product_label.show_label_template_limit', 7)

    def process(self):
        self.ensure_one()
        if not self.use_alternative_template:
            return super(ProductLabelLayout, self).process()

        if not self.garazd_label_template_id:
            raise UserError(_("Please specify a custom label template."))
        if not (self.product_tmpl_ids or self.product_ids):
            # flake8: noqa: E501
            raise UserError(_("No product to print, if the product is archived please unarchive it before printing its label."))

        return self.env['print.product.label'].get_quick_report_action(
            model_name='product.template' if self.product_tmpl_ids else 'product.product',
            ids=self.product_tmpl_ids.ids if self.product_tmpl_ids else self.product_ids.ids,
            qty=self.custom_quantity,
            template=self.garazd_label_template_id,
            close_window=True,
        )
