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

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

class project_scrum_email(models.TransientModel):
    _name = 'project.scrum.email'

    @api.model
    def default_get(self, fields):
        """
        This function gets default values
        @param self: The object pointer
        @param cr: the current row, from the database cursor,
        @param uid: the current user’s ID for security checks,
        @param fields: List of fields for default value
        @param context: A standard dictionary for contextual values

        @return : default values of fields.
        """
        context = self._context
        if context is None:
            context = {}
        meeting_pool = self.env['project.scrum.meeting']
        record_ids = context and context.get('active_ids', []) or []
        res = super(project_scrum_email, self).with_context(context).default_get(fields)
        for meeting in meeting_pool.with_context(context).browse(record_ids):
            sprint = meeting.sprint_id
            if 'scrum_master_email' in fields:
                res.update({'scrum_master_email': sprint.scrum_master_id and sprint.scrum_master_id.user_email or False})
            if 'product_owner_email' in fields:
                res.update({'product_owner_email': sprint.product_owner_id and sprint.product_owner_id.user_email or False})
            if 'subject' in fields:
                subject = _("Scrum Meeting : %s") %(meeting.date)
                res.update({'subject': subject})
            if 'message' in fields:
                message = _("Hello  , \nI am sending you Scrum Meeting : %s for the Sprint  '%s' of Project '%s' ") %(meeting.date, sprint.name, sprint.project_id)

                #message = _("Hello  , \nI am sending you Scrum Meeting")
                res.update({'message': message})
        return res

    scrum_master_email = fields.Char('Scrum Master Email', size=64, help="Email Id of Scrum Master")
    product_owner_email = fields.Char('Product Owner Email', size=64, help="Email Id of Product Owner")
    subject = fields.Char('Subject', size=64)
    message = fields.Text('Message')

    @api.multi
    def button_send_scrum_email(self):
        context = self._context
        uid = self._uid
        ids = [self.id]
        if context is None:
            context = {}

        active_id = context.get('active_id', False)
        scrum_meeting_pool = self.env['project.scrum.meeting']
        user_pool = self.env['res.users']
        meeting = scrum_meeting_pool.browse(active_id)

        #wizard data
        data_id = ids and ids[0] or False
        if not data_id or not active_id:
            return False
        data = self.browse(data_id)

        email_from = tools.config.get('email_from', False)
        user = user_pool.browse(uid)
        #BUG in v7 address_id does not exit, today its alias_id but we don't find address in the db
        user_email = email_from or user.alias_id.email
        body = "%s\n" %(data.message)
        body += "\n%s\n" %_('Tasks since yesterday')
        body += "_______________________\n"
        body += "\n%s\n" %(meeting.question_yesterday or _('None'))
        body += "\n%s\n" %_("Task for Today")
        body += "_______________________ \n"
        body += "\n%s\n" %(meeting.question_today or _('None'))
        body += "\n%s\n" % _('Blocking points encountered:') 
        body += "_______________________ \n"
        body += "\n%s\n" %(meeting.question_blocks or _('None'))
        body += "\n%s\n%s" %(_('Thank you,'), user.name)
        if user.signature:
            body += "\n%s" %(user.signature)
        if data.scrum_master_email == data.product_owner_email:
            data.product_owner_email = False
        if data.scrum_master_email:
            tools.email_send(user_email, [data.scrum_master_email], data.subject, body, reply_to=user_email)
        if data.product_owner_email:
            tools.email_send(user_email, [data.product_owner_email], data.subject, body, reply_to=user_email)
        return {'type': 'ir.actions.act_window_close'}

