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

""" Coworker module """

from odoo import models, fields, api, _
from odoo.exceptions import ValidationError

class Coworker(models.Model):
    """ Coworker model """
    _name = 'coworking.coworker'
    _description = 'Coworker model definition'
    _order = 'id desc'
    contact_date = fields.Date(default=fields.Date.context_today)

    full_name = fields.Char(compute='_compute_full_name', store=True, index=True)
    firstname = fields.Char('First name', required=True)

    coworker_type = fields.Selection([('staffer', 'Staffer'),
                                      ('worker', 'Worker'), ('member', 'Member'),
                                      ('volunteer', 'Volunteer'),
                                      ('visitor', 'Visitor')])
    company_name = fields.Char('Company')
    job = fields.Char()

#Adress fields
    street = fields.Char()
    contact_zip = fields.Char()
    city = fields.Char()
    full_contact_adress = fields.Char(compute='_compute_full_contact_adress')

    phone_number = fields.Char()
    gsm = fields.Char('GSM')
    email = fields.Char()
    url = fields.Char('URL')
    note = fields.Text()

#Images fields
    contact_image = fields.Binary()

    is_done = fields.Boolean('Done?')
    is_active = fields.Boolean('Active?', default=True)

    @api.depends('name', 'firstname')
    def _compute_full_name(self):
        """Concaténation du nom et du prénom"""
        for coworker in self:
            coworker.full_name = u'{} {}'.format(coworker.name, coworker.firstname)

    @api.constrains('contact_date')
    def _check_contact_date(self):
        """Test si la modification de la date n'est pas superieur à la date du jour"""
        if self.contact_date > fields.Date.context_today(self):
            raise ValidationError(_('Date most be equal of inferior to to day'))

    @api.constrains('company_name', 'job')
    def _check_company_name(self):
        for coworker in self:
            """Contrainte python sur company_name et job,
            si il y a une raison sociale le champ job est à remplir
            """
            if coworker.company_name and not coworker.job:
                raise ValidationError(_('You must enter job and compagny both'))

    @api.depends('street', 'contact_zip', 'city')
    def _compute_full_contact_adress(self):
        """Concatènation de l'adresse si les chanps street, contact-zip et city sont renseignés"""
        for coworker in self:
            coworker.full_contact_adress = u'{} {} {}'.format \
            (coworker.street or u'', coworker.contact_zip or u'', coworker.city or u'').strip()