openerp.custom_project = function(instance) {
  var QWeb = instance.web.qweb;

  instance.web_calendar.CalendarView = instance.web_calendar.CalendarView.extend({
    remove_event: function(id) {
      var self = this;

      function do_it() {
        return $.when(self.dataset.unlink([Number(id)])).then(function() {
          self.$calendar.fullCalendar('removeEvents', id);
        });
      }

      if (this.options.confirm_on_delete) {
        if (confirm(_t("Está seguro que desea eliminar esta actividad?"))) {
          return do_it();
        }
      } else
        return do_it();
    },
    event_data_transform: function(event) {
      var res = this._super.apply(this, arguments);
      //This would go off when there is no color set for hex_value. You could control this too.
      if (res && res.hasOwnProperty('className')) {
        //If you would uncomment the next line it would use the default colour that is set for the user. (default behaviour from Odoo calendars)
        // res.className = res.className.substring(0, res.className.indexOf(' calendar_color_'));
        // res.backgroundColor = '#DBDBDB';
        if (res.title.indexOf('false') > -1) {
          res.title = res.title.substring(0, res.title.indexOf(','));
        }
      }
      if (event.hex_value && res.title) {
        res.backgroundColor = event.hex_value;
        res.title = res.title.substring(0, res.title.indexOf(', ' + event.hex_value));
      }
      return res;
    }
  });

  instance.web_kanban.KanbanRecord = instance.web_kanban.KanbanRecord.extend({
    on_card_clicked: function() {
      if (this.view.dataset.model === 'project.task') {
        var self = this;
        var id = self.values.id.value;

        this.do_action({
          name: 'Tareas',
          type: 'ir.actions.act_window',
          res_model: 'project.task',
          view_mode: 'form',
          view_type: 'form',
          target: 'new',
          views: [
            [1576, 'form']
          ],
          res_id: id,
          flags: {
            'form': {
              'action_buttons': 'True'
            }
          }
        });
      } else {
        this._super.apply(this, arguments);
      }
    },
  });
  instance.web_kanban.KanbanGroup.include({
    hide_menu: function() {
      this.$el.find(".oe_dropdown_toggle ul.oe_dropdown_menu li").find('a:contains("Editar")').parent().hide();
      this.$el.find(".oe_dropdown_toggle ul.oe_dropdown_menu li").find('a:contains("Suprimir")').parent().hide();
    },

    start: function() {
      var self = this;

      self._super.apply(self, arguments);
      if (self.dataset.model == 'project.task') {
        this.hide_menu();
      }
      if( self.dataset.model == 'project.project'){
        self.$el.find('.oe_kanban_column_cards').css('margin-left', '6% !important');
      }
    }
  });
  instance.web_kanban.KanbanView.include({

    // set_background: function() {
    //   this.$el.find('.oe_kanban_groups_records').css('background', 'url("/custom_project/static/src/image/fondo7.jpeg")');
    //   this.$el.find('.oe_kanban_groups_records').css('background-repeat', 'round');
    // },
    //
    // start: function() {
    //   var self = this;
    //
    //   self._super.apply(self, arguments);
    //   if (self.dataset.model == 'project.project') {
    //
    //     this.set_background();
    //   }
    // },

    postprocess_m2m_tags: function() {
      var self = this;
      if (!this.many2manys.length) {
        return;
      }
      var relations = {};
      this.groups.forEach(function(group) {
        group.records.forEach(function(record) {
          self.many2manys.forEach(function(name) {
            var field = record.record[name];
            var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
            if (!relations[field.relation]) {
              relations[field.relation] = {
                ids: [],
                elements: {},
                context: self.m2m_context[name]
              };
            }
            var rel = relations[field.relation];
            field.raw_value.forEach(function(id) {
              rel.ids.push(id);
              if (!rel.elements[id]) {
                rel.elements[id] = [];
              }
              rel.elements[id].push($el[0]);
            });
          });
        });
      });
      _.each(relations, function(rel, rel_name) {
        var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context(rel.context));
        var defer = $.Deferred();
        var f = new openerp.Model('project.category');
        var fields = ['id', 'name', 'hex_value'];
        f.query(fields).filter().all().then(function(results) {
          _.each(results, function(item) {
            $(rel.elements[item.id]).append('<span class="oe_tag" style="background:' + item.hex_value + '">' + _.str.escapeHTML(item.name) + '</span>');
          });
        });
      });
    }
  });
}
