# -*- coding: utf-8 -*-
###############################################################################
#
#    Tech-Receptives Solutions Pvt. Ltd.
#    Copyright (C) 2009-TODAY Tech-Receptives(<http://www.techreceptives.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

from odoo.osv import expression
from odoo.tools.float_utils import float_round as round
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import UserError, ValidationError
from odoo import api, fields, models, _
import urlparse
import logging
_logger = logging.getLogger(__name__)

class EtourismBookingGroup(models.Model):
    _inherit = "etourism.booking.group"

    user_input_id = fields.Many2one('survey.user_input', 'Survey user input')
    survey_url = fields.Char(compute="_compute_survey_url", string="Survey URL", store=True)

    @api.one
    @api.depends('user_input_id')
    def _compute_survey_url(self):
        base_url = self.env['ir.config_parameter'].get_param('web.base.url')
        for booking in self:
            url = '%s' % urlparse.urljoin(base_url, 'survey/start/%s/%s'%(booking.user_input_id.survey_id.id, booking.user_input_id.token))
            booking.survey_url = url

    @api.multi
    def send_email_notification(self, email_template=False):
        print "SEND EMAIL NOTIFICATION - SATISFACTION:::::::::::::::::::::::::::::::::"
        if email_template:
            ir_model_data = self.env['ir.model.data']
            try:
                template_id = ir_model_data.get_object_reference('etourism_satisfaction', email_template)[1]
            except ValueError:
                template_id = False

            if template_id:
                mail_template = self.env['mail.template']
                template_ids = mail_template.browse(template_id)
                template_ids.with_context({'ltgt_replace': True, 'enterprise_name': self.enterprise_id.name}).send_mail(self.id, force_send=True) 
            else:
                _logger.error('Email template not found. Template: %s Booking Id: %s'%(email_template, self.id))
        return False

    @api.multi
    def action_checkout(self):
        vals = {}
        res = super(EtourismBookingGroup, self).action_checkout()
        survey = self.env['survey.survey'].search([('satisfaction_survey', '=', True)], limit=1)
        if survey:
            vals['survey_id'] = survey.id
            vals['type'] = 'link'
            vals['partner_id'] = self.guest_id.id
            vals['email'] = self.guest_id.email
            vals['booking_group_id'] = self.id

            survey_user_input = self.env['survey.user_input'].create(vals)
            if survey_user_input:
                booking_grp_rec = self.env['etourism.booking.group'].browse(self.id)
                booking_grp_rec.write({'user_input_id':survey_user_input.id})
                _logger.info('Sending Email notification on booking checkout. Booking Id: %s'%(self.id))
                self.send_email_notification('mail_template_etourism_booking_satisfaction')
        return res


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
