# -*- coding: utf-8 -*-
# Part of BiztechCS. See LICENSE file for full copyright and licensing details.

from openerp import api, fields, models


class product_images(models.Model):
    _name = 'product.images'
    _description = "Add Multiple Image in Product"

    name = fields.Char(string='Label')
    image = fields.Binary(string='Image')
    sequence = fields.Integer(string='Sort Order')
    product_tmpl_id = fields.Many2one('product.template', string='Product')
    more_view_exclude = fields.Boolean(string="More View Exclude")



class product_template(models.Model):
    _inherit = 'product.template'
    _description = "Multiple Images"

    images = fields.One2many('product.images', 'product_tmpl_id',
                             string='Images')
    multi_image = fields.Boolean("Add Multiple Images?")


class website(models.Model):
    _inherit = 'website'

    def get_multiple_images(self, product_id=None):
        product_img_data = False
        if product_id:
            self._cr.execute(
                'select id from product_images where product_tmpl_id=%s and more_view_exclude IS NOT TRUE order  by sequence', ([product_id]))
            product_ids = map(lambda x: x[0], self._cr.fetchall())
            if product_ids:
                product_img_data = self.env[
                    'product.images'].browse(product_ids)
        return product_img_data

    def get_zoom_feature(self):
        zoom = self.env['product.multiple.image.config'].search(
            [('is_zoom_feature', '=', True)])
        if zoom:
            return True
        else:
            return False

    def get_image_url(self, record, field, size=None):
        """Returns a local url that points to the image field of a given browse record."""
        model = record._name
        sudo_record = record.sudo()
        id = '%s_%s' % (record.id, hashlib.sha1(
            sudo_record.write_date or sudo_record.create_date or '').hexdigest()[0:7])
        size = '' if size is None else '/%s' % size
        return '/website/image/%s/%s/%s%s' % (model, id, field, size)
