# -*- coding: utf-8 -*-
# © 2016 Elico Corp (www.elico-corp.com).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import api, models


class AnalyticAccount(models.Model):
    _inherit = 'account.analytic.account'

    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):

        if not args:
            args=[]
        if context is None:
            context={}
        account_ids = []
        if name:
            account_ids = self.search(cr, uid, [('code', '=', name)] + args, limit=limit, context=context)
            if not account_ids:
                dom = []
                if '/' in name:
                    for name2 in name.split('/'):
                        # intermediate search without limit and args - could be expensive for large tables if `name` is not selective
                        account_ids = self.search(cr, uid, dom + [('name', operator, name2.strip())], limit=None, context=context)
                        if not account_ids: break
                        dom = [('parent_id','in',account_ids)]
                    if account_ids and args:
                        # final filtering according to domain (args)4
                        account_ids = self.search(cr, uid, [('id', 'in', account_ids)] + args, limit=limit, context=context)
        if not account_ids:
            return super(account_analytic_account, self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)
        return self.name_get(cr, uid, account_ids, context=context)

    def _get_full_names(self, elmt, level):
        # a general tail recursion function
        def iter_parent_ids(elmt, level, full_names):

            if level <= 0:
                full_names.append('...')
                return (None, 0, full_names)
            elif elmt.parent_id and not elmt.type == 'template':
                full_names.append(elmt.name)
                return iter_parent_ids(elmt.parent_id, level-1, full_names)
            else:
                full_names.append(elmt.name)
                return (None, level-1, full_names)

        # call recursion function
        (_, _, full_names) = iter_parent_ids(elmt, level, [])

        return full_names

    def name_get(self, cr, uid, ids, context=None): 

        res = []
        if not ids:
            return res
        if isinstance(ids, (int, long)):
            ids = [ids]


        for id in ids:
            import pdb
            pdb.set_trace()

            elmt = self.browse(cr, uid, id, context=context)
            full_names = self._get_full_names(elmt, 6)

            if len(full_names)>0:
        
                project_ids = self.pool['project.project'].search(
                    cr, uid,
                    [('analytic_account_id', '=', id)],
                    context=context
                )


                if project_ids and (len(project_ids) > 0):
                    project_obj = self.pool['project.project'].browse(
                        cr, uid,
                        project_ids[0],
                        context=context
                    )

                    partner_ref = project_obj._get_partner_ref()[0]

                    full_names[0] = "%s - %s" % (partner_ref, full_names[0])

                # path order change to [from root to leaf]
                full_names.reverse()
                
            res.append((id, '/'.join(full_names)))

        return res


    '''    
    def _get_one_full_name(self, elmt, level=6):
        if level<=0:
            return '...'
        if elmt.parent_id and not elmt.type == 'template':
            parent_path = self._get_one_full_name(elmt.parent_id, level-1) + " / "
        else:
            parent_path = ''
        return parent_path + 'ccc' + elmt.name
    '''

@@ -255,1394 +255,70 @@
 def 
-name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):%0A%0A        if not args:%0A            args=%5B%5D%0A        if context is None:%0A            context=%7B%7D%0A        account_ids = %5B%5D%0A        if name:%0A            account_ids = self.search(cr, uid, %5B('code', '=', name)%5D + args, limit=limit, context=context)%0A            if not account_ids:%0A                dom = %5B%5D%0A                if '/' in name:%0A                    for name2 in name.split('/'):%0A                        # intermediate search without limit and args - could be expensive for large tables if %60name%60 is not selective%0A                        account_ids = self.search(cr, uid, dom + %5B('name', operator, name2.strip())%5D, limit=None, context=context)%0A                        if not account_ids: break%0A                        dom = %5B('parent_id','in',account_ids)%5D%0A                    if account_ids and args:%0A                        # final filtering according to domain (args)4%0A                        account_ids = self.search(cr, uid, %5B('id', 'in', account_ids)%5D + args, limit=limit, context=context)%0A        if not account_ids:%0A            return super(account_analytic_account, self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)%0A        return self.name_get(cr, uid, account_ids, context=context)%0A%0A    def _get_full_names(self, elmt, level):%0A        # a ge
+_get_full_names(self, elmt, level):%0A%0A        # This is a in
 ner
-al
  tai
@@ -1138,59 +1138,8 @@
 ids:
-%0A            import pdb%0A            pdb.set_trace()
 %0A%0A  
@@ -1938,49 +1938,38 @@
    #
- path
  order 
-change to %5Bfrom root to l
+is First-Root-Last-L
 eaf
-%5D
 %0A   
@@ -2048,17 +2048,19 @@
 d((id, '
-/
+ / 
 '.join(f
@@ -2096,353 +2096,4 @@
 res%0A
-%0A%0A    '''    %0A    def _get_one_full_name(self, elmt, level=6):%0A        if level%3C=0:%0A            return '...'%0A        if elmt.parent_id and not elmt.type == 'template':%0A            parent_path = self._get_one_full_name(elmt.parent_id, level-1) + %22 / %22%0A        else:%0A            parent_path = ''%0A        return parent_path + 'ccc' + elmt.name%0A    '''%0A

clear obsolete comment