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

from odoo import models, fields, api

class calendar_event(models.Model):
    _inherit = 'calendar.event'

    create_task = fields.Boolean('Create Task')
    task_ids = fields.One2many('project.task','meeting_id','Tasks')
    
    @api.multi
    def write(self, vals):
        for event in self:
            if vals.get('create_task'):
                #carregar todos os participantes
                #verificar quais tem user criado 
                #para cada user criar uma tarefa
                participante = self._uid
                tasks = self.env['project.task'].search([('meeting_id','=',event.id)])
                lst_task_ids = []
               
                for tsk in tasks:
                    lst_task_ids.append(tsk.user_id.id)
                if participante not in lst_task_ids:
                    task = {
                            'name':vals.get('name') or event.name,
                            'date_deadline':vals.get('stop', False) or event.stop,
                            'planned_hours':vals.get('duration') or event.duration,
                            'meeting_id':event.id,
                            'description':vals.get('description') or event.description
                            }
                    self.env['project.task'].create(task)
               	
                if vals.get('partner_ids'): 
                    task = {}
               	    partners = self.env['res.partner'].browse(vals.get('partner_ids')[0][2])
               	    for partner in partners:
                        if partner.user_ids:
                            if partner.user_ids[0].id <> uid:
                                if partner.user_ids[0].id not in lst_task_ids:
                                    participante = partner.user_ids[0].id
                                    task = {
                                            'name':vals.get('name') or event.name,
                                            'date_deadline':vals.get('stop', False) or event.stop,
                                            'planned_hours':vals.get('duration') or event.duration,
                                            'meeting_id':event.id,
                                            'description':vals.get('description') or event.description,
                                            'user_id':participante
                                            }
                                    task_id = self.env['project.task'].create(task)	
              				
    	return super(calendar_event, self).write(vals)
    	
    @api.model
    def create(self, vals):
        context = self._context or {}
            
        meeting_id = super(calendar_event, self).create(vals)
        if vals.get('create_task'):
            #carregar todos os participantes
            #verificar quais tem user criado 
            #para cada user criar uma tarefa
            participante = self._uid
            task = {
                    'name':vals.get('name'),
                    'date_deadline':vals.get('stop'),
                    'planned_hours':vals.get('duration'),
                    'meeting_id':meeting_id.id,
                    'description':vals.get('description')
                    }
            self.env['project.task'].create(task)
            if vals.get('partner_ids'): 
                task = {}
                partners = self.env['res.partner'].browse(vals.get('partner_ids')[0][2])
                for partner in partners: 
                    if partner.user_ids:
                        if partner.user_ids[0].id <> self._uid:
                            participante = partner.user_ids[0].id  
                            task = {
                                'name':vals.get('name'),
                                'date_deadline':vals.get('stop'),
                                'planned_hours':vals.get('duration'),
                                'user_id':participante,
                                'meeting_id':meeting_id.id,
                                'description':vals.get('description')
                                }
                            task_id = self.env['project.task'].create(task)	
        return meeting_id


class task(models.Model):
    _inherit = 'project.task'
    
    meeting_id = fields.Many2one('calendar.event','Meeting')
    
