# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################

from odoo import models, fields, api
from odoo.tools.translate import _

class AccountPayment(models.Model):
    _inherit = "account.payment"

    @api.multi
    def action_send(self):
        """ Open a window to compose an email, with the edi payment template
            message loaded by default
        """
        self.ensure_one()
        template = self.env.ref('account_payment_send_email.email_template_edi_payment', False)
        compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)
        ctx = dict(
            default_model='account.payment',
            default_res_id=self.id,
            default_use_template=bool(template),
            default_template_id=template and template.id or False,
            default_composition_mode='comment',
            default_reply_to=self.company_id.email,
        )
        return {
            'name': _('Compose Email'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'mail.compose.message',
            'views': [(compose_form.id, 'form')],
            'view_id': compose_form.id,
            'target': 'new',
            'context': ctx,
        }

class MailMessage(models.Model):
    _inherit = "mail.message"

    @api.model
    def create(self, vals):
        context = self.env.context or dict()
        if 'default_reply_to' in context and ('reply_to' not in vals or not vals['reply_to']):
            vals['reply_to'] = context['default_reply_to']
        return super(MailMessage, self).create(vals)
