# -*- coding: utf-8 -*-
# Copyright 2016 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from lxml import etree

from odoo import api, fields, models, tools, SUPERUSER_ID, _
from odoo.exceptions import UserError, ValidationError
from odoo.tools.safe_eval import safe_eval
from odoo.tools.translate import _


WARNING_TYPES = [('warning','Warning'),('info','Information'),('error','Error')]

class warning(models.TransientModel):
    _name = 'warning'
    _description = 'warning'
    _rec_name = 'title'

    type = fields.Selection(WARNING_TYPES, string='Type', readonly=True)
    title = fields.Char(string="Title", size=100, readonly=True)
    note = fields.Text(string='Message', readonly=True)


    @api.multi
    def _get_view_id(self):
        """Get the view id
        @return: view id, or False if no view found
        """
        res = self.env['ir.model.data'].get_object_reference('prime_warning', 'warning_form')
        return res and res[1] or False

    @api.multi
    def message(self,id):
        war = self.browse([id])
        res = {
            'name': '%s: %s' % (_(war.id.type), _(war.id.title)),
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': self._get_view_id(),
            'res_model': 'warning',
            'domain': [],
            'context': {},
            'type': 'ir.actions.act_window',
            'target': 'new',
            'res_id': war.id.id
        }
        return res

    @api.multi
    def warning(self, title, message):
        id = self.env['warning'].create({'title': title, 'note': message, 'type': 'warning'})
        res = self.message(id)
        return res

    @api.multi
    def info(self, title, message):
        id = self.env['warning'].create({'title': title, 'note': message, 'type': 'info'})
        res = self.message(id)
        return res

    @api.multi
    def error(self, title, message):
        id = self.env['warning'].create({'title': title, 'note': message, 'type': 'error'})
        res = self.message(id)
        return res