var _ = require('underscore');
var $ = require('jquery');
var cdb = require('cartodb.js');
var randomQuote = require('../../view_helpers/random_quote.js');
var ErrorDetailsView = require('../../views/error_details_view');

/**
 * View for the last step of all merge kinds, creates the actual merged table.
 * TODO: taken from old code, cdb.admin.MergeTableDialog.merge, could this be done in a better way?
 */
module.exports = cdb.core.View.extend({

  initialize: function() {
    _.bindAll(this, '_onMergeSuccess', '_onMergeError');
    this.elder('initialize');
    this._startMerge();
  },

  render: function() {
    this.$el.html(
      this.getTemplate('common/templates/loading')({
        title: 'Merging datasets and generating the new one…',
        quote: randomQuote()
      })
    );
    return this;
  },

  _startMerge: function() {
    // TODO: taken from old code, cdb.admin.MergeTableDialog.merge
    //   could this be done in a better way?
    $.ajax({
      type: 'POST',
      url: cdb.config.prefixUrl() + '/api/v1/imports',
      data: {
        table_name: this.model.get('tableName') + '_merge',
        sql: this.model.get('sql')
      },
      success: this._onMergeSuccess,
      error: this._onMergeError
    });
  },

  _onMergeSuccess: function(r) {
    var imp = this.importation = new cdb.admin.Import({
      item_queue_id: r.item_queue_id
    });
    this.add_related_model(imp);

    // Bind complete event
    imp.bind('importComplete', function() {
      imp.unbind();
      window.location.href = cdb.config.prefixUrl() + "/tables/" + (imp.get("table_name") || imp.get("table_id")) + "/";
    }, this);

    var self = this;
    imp.bind('importError', function(e) {
      self._showError(e.attributes.error_code, e.attributes.get_error_text.title, e.attributes.get_error_text.what_about);
    }, this);
    imp.pollCheck();
  },

  _onMergeError: function(e) {
    try {
      this._showError(
        e.attributes.error_code,
        e.attributes.get_error_text.title,
        e.attributes.get_error_text.what_about
      );
    } catch(err) {
      this._showError('99999', 'Unknown', '');
    }
  },

  //Show the error when duplication fails
  _showError: function(number, description, wadus) {
    var view = new ErrorDetailsView({
      err: {
        error_code: number,
        title: description,
        what_about: wadus
      },
      user: this.model.get('user')
    });
    this.addView(view);
    this.$el.html(view.render().el);
  }

});

@@ -1723,16 +1723,25 @@
 owError(
+%0A        
 e.attrib
@@ -1756,16 +1756,24 @@
 or_code,
+%0A       
  e.attri
@@ -1799,16 +1799,24 @@
 t.title,
+%0A       
  e.attri
@@ -1846,16 +1846,59 @@
 at_about
+,%0A        e.attributes.item_queue_id%0A      
 );%0A    %7D
@@ -2119,16 +2119,52 @@
 at_about
+,%0A        e.attributes.item_queue_id
 %0A      )
@@ -2320,26 +2320,40 @@
 er, 
-description, wadus
+title, what_about, item_queue_id
 ) %7B%0A
@@ -2450,19 +2450,13 @@
 le: 
-description
+title
 ,%0A  
@@ -2478,12 +2478,55 @@
 t: w
-adus
+hat_about,%0A        item_queue_id: item_queue_id
 %0A   

Include detailed info in error dialog