"use strict";

var _ = require("underscore");
var NodeView = require("../node").View;
var $$ = require("substance-application").$$;
var TextView = require("../text/text_view");
var License = require("../license").Model;


// Substance.Contributor.View
// ==========================================================================

var PublicationInfoView = function(node) {
  NodeView.call(this, node);

  this.$el.attr({id: node.id});
  this.$el.addClass("content-node publication-info");

  this.$el.on('change', "#license", _.bind(this.updateLicense, this));
  this.$el.on('change', "#published_on", _.bind(this.updatePublicationDate, this));
};

PublicationInfoView.Prototype = function() {

  this.updatePublicationDate = function(e) {
    var text = this.$('#published_on');
    console.log('Published_on', text);
  };

  this.updateLicense = function(e) {
    var license = this.$('#license').val();
    this.node.setLicense(license);
  };

  // Render it
  // --------
  //

  this.render = function() {
    NodeView.prototype.render.call(this);

    // Resource Body
    // -------
    //
    // Wraps all the contents of the resource card

    var body = $$('.resource-body', {contenteditable: false});

    var formattedDates = {
      "created_at": new Date(this.node.document.created_at).toUTCString(),
      "updated_at": new Date(this.node.document.updated_at).toUTCString(),
      "published_on": new Date(this.node.document.published_on).toDateString()
    };

    var createdAt = $$('.created-at', {
      children: [
        $$('.label', {text: "Created at"}),
        $$('.pub-date', {text: formattedDates["created_at"]})
      ]
    });

    body.appendChild(createdAt);

    var createdAt = $$('.updated-at', {
      children: [
        $$('.label', {text: "Modified at"}),
        $$('.pub-date', {text: formattedDates["updated_at"]})
      ]
    });

    body.appendChild(createdAt);

    var pubDate = $$('.publication-date', {
      children: [
        $$('.label', {text: "Publication Date"}),
        $$('.pub-date', {text: formattedDates["published_on"]})
        // $$('input#published_on', {value: "2014-02-04"})
      ]
    });

    body.appendChild(pubDate);

    var license = $$('.license', {
      children: [
        $$('.label', {text: "Choose License"}),
        $$('select#license', {
          children: _.map(License.available_licenses, function(l) {
            var attrs = {value: l.code, text: l.name};
            if (this.node.getLicense() === l.code) attrs["selected"] = "selected";
            return $$('option', attrs);
          }, this)
        })
      ]
    });

    body.appendChild(license);

    this.content.appendChild(body);

    return this;
  };

  this.dispose = function() {
    NodeView.prototype.dispose.call(this);
  };

};

PublicationInfoView.Prototype.prototype = NodeView.prototype;
PublicationInfoView.prototype = new PublicationInfoView.Prototype();

module.exports = PublicationInfoView;

@@ -1288,35 +1288,32 @@
 t.created_at).to
-UTC
 String(),%0A      
@@ -1372,11 +1372,8 @@
 ).to
-UTC
 Stri
@@ -1576,32 +1576,34 @@
        $$('.
-pub-date
+created-at
 ', %7Btext: fo
@@ -1682,31 +1682,110 @@
 edAt);%0A%0A    
-var cre
+this.updatedAtEl = $$('.updated-at', %7Btext: formattedDates%5B%22updated_at%22%5D%7D)%0A    var upd
 atedAt = $$(
@@ -1876,61 +1876,24 @@
     
-$$('.pub-date', %7Btext: formattedDates%5B%22updated_at%22%5D%7D)
+this.updatedAtEl
 %0A   
@@ -1919,35 +1919,35 @@
 ody.appendChild(
-cre
+upd
 atedAt);%0A%0A    va
@@ -2120,67 +2120,8 @@
 %5D%7D)%0A
-        // $$('input#published_on', %7Bvalue: %222014-02-04%22%7D)%0A
     
@@ -2617,25 +2617,24 @@
 d(license);%0A
-%0A
     this.con
@@ -2657,17 +2657,16 @@
 (body);%0A
-%0A
     retu
@@ -2673,16 +2673,624 @@
 rn this;
+%0A  %7D;%0A%0A%0A  this.updateModificationDate = function() %7B%0A    var dat = new Date(this.node.document.updated_at).toString();%0A    this.updatedAtEl.innerHTML = dat;%0A  %7D;%0A%0A  this.onGraphUpdate = function(op) %7B%0A    // Call super handler and return if that has processed the operation already%0A    if (NodeView.prototype.onGraphUpdate.call(this, op)) %7B%0A      return true;%0A    %7D%0A%0A    this.render();%0A    // When published date has changed, rerender%0A    if (_.isEqual(op.path, %5B%22document%22,%22published_on%22%5D)) %7B%0A      this.render();%0A      return true;%0A    %7D else %7B%0A      this.updateModificationDate();%0A      return true;%0A    %7D
 %0A  %7D;%0A%0A 

Deal with incremental updates of dates.