# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016 Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################

from odoo import fields, models, api

class sandbox_transfer_to_backlog(models.TransientModel):
    _name = 'project.scrum.sandbox.to.backlog'
    _description = 'Transfer user story from sandbox to Product Backlogs'

    role_id = fields.Many2one('project.scrum.role', "As")
    name = fields.Char('I want', size=128)
    for_then = fields.Char('For', size=128)
    project_id = fields.Many2one('project.project', "Project", domain=[('is_scrum', '=', True)])
    developer_id = fields.Many2one('res.users', 'Developer')
    release_id = fields.Many2one('project.scrum.release', "Release")
    
    @api.multi
    def do_transfer(self):
        uid = self._uid
        ids = [self.id]
        context = self._context
        if not context:
            context = {}
        
        sandbox_spg = self.env['project.scrum.sandbox']
        backlog_spg = self.env['project.scrum.product.backlog']
        sandboxes = sandbox_spg.browse(context['active_ids'])
        data = self.read(ids, [])[0]
        print "data = ", data
        for sandbox in sandboxes:
            backlog_id = backlog_spg.create({
                'role_id': sandbox.role_id.id,
                'name': sandbox.name,
                'for_then': sandbox.for_then,
                'project_id': sandbox.project_id.id,
                'release_id':data.get('release_id') and data['release_id'][0] or False, 
            })
            if backlog_id:
                sandbox_spg.unlink(sandbox.id)
        #mod_obj = self.pool.get('ir.model.data')
        #task = self.pool.get('project.task')
        #backlog_id = self.pool.get('project.scrum.product.backlog')
        #document_pool = self.pool.get('ir.attachment')
        #ids_task = []
        #
        #data = self.read(cr, uid, ids, [], context=context)[0]
        #backlogs = backlog_id.browse(cr, uid, context['active_ids'], context=context)
        #result = mod_obj._get_id(cr, uid, 'project', 'view_task_search_form')
        #id = mod_obj.read(cr, uid, result, ['res_id'])
        #
        #for backlog in backlogs:
        #    task_id = task.create(cr, uid, {
        #        'product_backlog_id': backlog.id,
        #        'name': backlog.name,
        #        'description': backlog.description,
        #        'project_id': backlog.project_id.id,
        #        'user_id': data.get('user_id') and data['user_id'][0] or False,
        #        'planned_hours': backlog.expected_hours,
        #        'remaining_hours':backlog.expected_hours,
        #        'sequence':backlog.sequence,
        #    })
        #    document_ids = document_pool.search(cr, uid, [('res_id', '=', backlog.id), ('res_model', '=', backlog_id._name)])
        #    for document_id in document_ids:
        #        document_pool.copy(cr, uid, document_id, default={'res_id':task_id, 'res_model':task._name})
        #    ids_task.append(task_id)
        #return {
        #    'domain': "[('product_backlog_id','in',["+','.join(map(str, context['active_ids']))+"])]",
        #    'name': 'Tasks',
        #    'res_id': ids_task,
        #    'view_type': 'form',
        #    'view_mode': 'tree,form',
        #    'res_model': 'project.task',
        #    'view_id': False,
        #    'type': 'ir.actions.act_window',
        #    'search_view_id': id['res_id'],
        #}
        print "test method do_transfer"
        return True

