from odoo import api, fields, models, _
import urlparse

class EtourismDirectoryInterestPoint(models.Model):
    _name = 'etourism.directory.interest.point'
    _description = "Interest Point"
    _inherit = ['ir.needaction_mixin', 'mail.thread']

    name = fields.Char('Name',required=True)
    place_id = fields.Many2one('ecom.location', string='Place', required=True, domain=[('level', '=', 2)])
    image = fields.Binary("Image", attachment=True, required=True,
                          help="This field holds the main image for this interest point")
    images_id = fields.One2many('etourism.directory.interest.point.image', 'interest_point_id', string='Images',required=True)
    category_id = fields.Many2one("etourism.directory.category", string='Category', store=True, required=True)
    details = fields.Html('Details')
    details_en = fields.Html('Details EN')
    details_fr = fields.Html('Details FR')
    tag_ids = fields.Many2many('etourism.directory.tag', string='Tags')
    embed_map_src = fields.Text(string='Google Map Embed URL', store=True)


class EtourismDirectoryInterestPointsImage(models.Model):
    _name = 'etourism.directory.interest.point.image'
    _order = 'sequence, id DESC'

    name = fields.Char('Name')
    description = fields.Text('Description')
    sequence = fields.Integer('Sequence')
    image_alt = fields.Text('Image Label')
    image = fields.Binary('Image')
    image_small = fields.Binary('Small Image')
    interest_point_id = fields.Many2one('etourism.directory.interest.point', 'Interest Point',copy=False)
    from_main_image = fields.Boolean('From Main Image', default=False)
    url = fields.Text('Image Url')

    @api.multi
    def get_image_url(self, model, field, rec_id):
        url = ''
        if model and rec_id:
            base_url = self.env['ir.config_parameter'].get_param('web.base.url')
            url = "%s" % urlparse.urljoin(base_url, 'web/image/%s/%s/%s' % (model, rec_id, field))
        return url
