var items = {
  myProj: Items.get('f20c5503-e3e3-4aa7-b001-8be9ac92d5ad'), // проект “Разговор с Диспетчером”
  my: Items.get('f4c9d254-cf5e-406a-b3fc-eb71fb227446'), // доска “Мои обращения”
  myNew: Items.get('8b0d87e7-02f4-4984-ae2a-af9689a0cd6b'), // колонка “Новые”
  myInProgress: Items.get('d7fd0393-f43b-4bc3-9a90-9b2fa57445d1'), // колонка “В работе”
  myClosed: Items.get('b80cb7d1-082f-4660-802d-a547c84f770b'), // колонка “Закрытые”,

  deskProj: Items.get('020ac305-6291-49b0-a163-ed4707c1f56f'), // проект “HelpDesk KKM”
  desk: Items.get('0b695634-de7e-4ef5-9b7c-99b47425bd40'), // доска “Обращения”,
  deskNew: Items.get('09783a57-6837-467f-ab92-d75e5460704a'), // колонка “Новые”,
  deskClosed: Items.get('d7780e4d-6a78-467f-8152-592cae509a55') // колонка “Закрытые”
};

function isAdmin() {
  return Current.user.isAdmin;
}

function inside(item, container) {
  if (!item || item.type === 'Company' || !container) {
    return false;
  }
  return item.id === container.id || inside(item.up(), container);
}

Items.allowRename = function(obj) {
  return !inside(obj, items.my) && !inside(obj, items.deskProj);
};

Items.allowDelete = function(obj) {
  return !inside(obj, items.my) && !inside(obj, items.deskProj);
};

Items.allowDrag = function(obj) {
  return (
    !inside(obj, items.my) &&
    (!inside(obj, items.desk) ||
      (Current.board && Current.board.id === items.desk.id))
  );
};

Items.onBeforeAdd = function(location, name) {
  if (location.id === items.myNew.id) {
    var task = items.deskNew.add(name);
    task.setData({reportedById: Current.user.id});
    Chat.addUser(task, Current.user);
    setTimeout(function() {
      items.myNew.triggerUpdate();
    }, 500);
    return false;
  }
};

Items.allowAdd = function(obj) {
  return (
    (!inside(obj, items.myProj) &&
      (!inside(obj, items.desk) ||
        obj.type !== 'Column' ||
        obj.id === items.deskNew.id)) ||
    obj.id === items.myNew.id
  );
};

Items.onList = function(obj, list) {
  if (obj.type === 'Column' && obj.up().id === items.my.id) {
    var res = [];
    if (obj.id === items.myNew.id) {
      res = items.deskNew.list();
    } else if (obj.id === items.myClosed.id) {
      res = items.deskClosed.list();
    } else if (obj.id === items.myInProgress.id) {
      var cols = items.desk.list().filter(function(c) {
        return c.id !== items.deskNew.id && c.id !== items.deskClosed.id;
      });
      var tasks = cols.map(function(c) {
        return c.list();
      });
      res = [].concat.apply([], tasks);
    }
    return res.filter(function(task) {
      var data = task.getData();
      return data.reportedById === Current.user.id;
    });
  }
  return list;
};

Items.onUpdate = function(item) {
  if (Current.board && Current.board.id === items.my.id) {
    var deskColIds = items.desk.list().map(function(c) {
      return c.id;
    });

    if (deskColIds.indexOf(item.id) > -1) {
      items.my.triggerUpdate();
      items.my.list().forEach(function(group) {
        group.triggerUpdate();
      });
    }
  }
};

function draw() {
  if (!Current.board) {
    return;
  }
  UI.components.stickerPanel.clear();
  if (Current.board.id === items.my.id) {
    UI.components.stickerPanel.style = {display: 'none'};
  } else {
    UI.components.stickerPanel.style = {display: 'block'};
  }
}

draw();
Current.onBoardChange = draw;
UI.components.stickerPanel.style = {display: 'none'};
