# -*- 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

class EcomLocation(models.Model):

    @api.multi
    @api.depends('name', 'parent_id')
    def name_get(self):
        context = self._context or {}
        result = []
        if context and 'filter_only_name' in context:
            for location in self:
                name = location.name
                result.append((location.id, name))
        elif context and 'website_id' in context:
            for location in self:
                name = location.name
                result.append((location.id, name))
        else:
            for location in self:
                name = location.name
                if location.parent_id:
                    name = location.parent_id.name+' / '+name
                result.append((location.id, name))
        return result

    def _location_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
        res = self.name_get(cr, uid, ids, context=context)
        return dict(res)

    _name = 'ecom.location'
    _order="name asc"

    name = fields.Char(string='Name', required=True)
    complete_name = fields.Char(string='Name', store=True, compute='_location_name_get_fnc')
    parent_id = fields.Many2one('ecom.location', string='Belongs To', required=False)
    latitude = fields.Float(string='Latitude')
    longitude = fields.Float(string='Longitude')
    citizenship = fields.Char('Citizenship')
    level = fields.Selection([('1','Country'),
				('2','Island'),
				('3','County'),
				('4','Parish'),
				('5','Local'),
				('6','Neighborhood')
		], string='Location type')
    level_import = fields.Char('Level')
    ordering = fields.Integer('Order')
    code = fields.Char('Code')
    photo = fields.Binary('Photo')
    icolor = fields.Char('Color')
    #JCF 12-07-2018
    location_image_line = fields.One2many('ecom.location.image', 'location_id','Images')


class LocationImage(models.Model):
    _name = 'ecom.location.image'
    _description = 'Location Image'

    name = fields.Char(string='Name')
    description = fields.Text(string='Description')
    image_alt = fields.Text(string='Image Label')
    image = fields.Binary(string='Image')
    image_small = fields.Binary(string='Small Image')
    image_url = fields.Char(string='Image URL')
    directory_image = fields.Boolean(string='Publish in directory?', default=False)
    location_id = fields.Many2one('ecom.location', 'Location',copy=False)

    @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

    @api.model
    def create(self, vals):

        image = super(LocationImage, self).create(vals)
        image_url = self.get_image_url('ecom.location.image', 'image', image.id)
        image.write({'image_url': image_url})
        return image
		
    @api.multi
    def write(self, vals):
        """
        Overrides orm write method.
        @param self: The object pointer
        @param vals: dictionary of fields value.
        """
        vals['image_url'] = self.get_image_url('ecom.location.image', 'image', self.id)

        return super(LocationImage, self).write(vals)
