from odoo import models, fields, api, _, SUPERUSER_ID
import random
from datetime import datetime, timedelta
from odoo.tools import email_re, email_escape_char, email_split
from odoo.exceptions import UserError,ValidationError
from odoo.tools import email_re
import uuid
import pytz
import logging
_logger = logging.getLogger(__name__)


class HelpdeskTicket(models.Model):
    _inherit = 'helpdesk.ticket'

    maintenance_type_id = fields.Many2one('maintenance.type',string="Tipo mantenimiento")
    date_fail = fields.Date('Fecha de falla')
    severity_type = fields.Selection([
        ('1','Leve'),
        ('2','Medio'),
        ('3','Grave'),
    ],string='Tipo de gravedad')
    fail_type = fields.Selection([
        ('fail','Falla'),('warranty','Garantía')
    ],string="Tipo de falla")
    is_maintenance = fields.Boolean('Es mantenimiento',compute='_verify_ticket_type')

    def _compute_access_url(self):
        super(HelpdeskTicket, self)._compute_access_url()
        for ticket in self:
            if ticket.team_id.id == self.env.ref('helpdesk_maintenance.maintenance').id:
                ticket.access_url = '/my/manteniminetos/%s' % (ticket.id)

    @api.depends('team_id')     
    def _verify_ticket_type(self):
        for ticket in self:
            if ticket.team_id.id == self.env.ref('helpdesk_maintenance.maintenance').id:
                ticket.is_maintenance = True
            else:
                ticket.is_maintenance = False

    def _get_color(self):
        super(HelpdeskTicket, self)._get_color()

        in_progress_stage = self.env.ref('sh_all_in_one_helpdesk.in_progress_stage').id
        new_stage = self.env.ref('sh_all_in_one_helpdesk.new_stage').id
       

        simple_time = float(self.env['ir.config_parameter'].sudo().get_param('helpdesk_maintenance.time_simple_alert_maintenance'))
        medium_time = float(self.env['ir.config_parameter'].sudo().get_param('helpdesk_maintenance.time_medium_alert_maintenance'))
        extreme_time = float(self.env['ir.config_parameter'].sudo().get_param('helpdesk_maintenance.time_extreme_alert_maintenance'))
        for rec in self:
            if rec.is_maintenance:
                if fields.Datetime.now() >= rec.create_date+timedelta(hours=simple_time) and fields.Datetime.now() <= rec.create_date+timedelta(hours=medium_time) and rec.stage_id.id in [new_stage]:
                    rec.color = self.env['ir.config_parameter'].sudo().get_param('helpdesk_maintenance.color_simple_alert_maintenance')
                if fields.Datetime.now() >= rec.create_date+timedelta(hours=medium_time) and fields.Datetime.now() <= rec.create_date+timedelta(hours=extreme_time) and rec.stage_id.id in [new_stage,in_progress_stage]:
                    rec.color = self.env['ir.config_parameter'].sudo().get_param('helpdesk_maintenance.color_medium_alert_maintenance')
                if fields.Datetime.now() >= rec.create_date+timedelta(hours=extreme_time) and rec.stage_id.id in [new_stage,in_progress_stage]:
                    rec.color = self.env['ir.config_parameter'].sudo().get_param('helpdesk_maintenance.color_extreme_alert_maintenance')