import Log from 'debug';
import bus from 'bus';
import render from '../render/render';
import View from '../view/view';
import ForumRow from './forum-row-view';
import template from './template.jade';
import newForumButton from './new-forum-button.jade';
import forumStore from '../forum-store/forum-store';

const log = new Log('democracyos:settings-forum:view');

export default class ForumsView extends View {

  /**
   * Creates a profile edit view
   */

  constructor () {
    super(template);
    this.forumsList = this.find('.forums');
    this.load();
  }

  load () {
    forumStore.findUserForum()
      .then(forum => this.add(forum))
      .catch(err => {
        if (404 === err.status) {
          this.empty();
        } else {
          log('Found Error %s', err);
        }
      })
    ;
  }

  add (forum) {
    var row = new ForumRow(forum);
    this.forumsList.append(row.el);
    bus.on(`forum-store:destroy:${forum.name}`, row.bound('remove'));
  }

  empty () {
    this.forumsList.empty();
    this.forumsList.append(render(newForumButton));
  }
}

@@ -902,16 +902,18 @@
   bus.on
+ce
 (%60forum-
@@ -946,27 +946,95 @@
 %7D%60, 
-row.bound('remove')
+() =%3E %7B%0A      row.remove();%0A      if ('' == this.forumsList.html()) this.empty();%0A    %7D
 );%0A 

add create button when all forums where deleted Closes #1089