var Backbone = require('backbone');
var CoreView = require('backbone/core-view');
var Infobox = require('../../../../components/infobox/infobox-factory');
var InfoboxModel = require('../../../../components/infobox/infobox-model');
var InfoboxCollection = require('../../../../components/infobox/infobox-collection');
var PanelWithOptionsView = require('../../../../components/view-options/panel-with-options-view');
var panelTemplate = require('./panel-with-options.tpl');
var LayerContentAnalysesView = require('./layer-content-analyses-view');
var OnboardingLauncher = require('../../../../components/onboardings/generic/generic-onboarding-launcher');
var OnboardingView = require('../../../../components/onboardings/layers/analysis-onboarding/analysis-onboarding-view');
var layerOnboardingKey = require('../../../../components/onboardings/layers/layer-onboarding-key');
var checkAndBuildOpts = require('../../../../helpers/required-opts');
var fetchAllQueryObjectsIfNecessary = require('../../../../helpers/fetch-all-query-objects');
var AnalysesService = require('./analyses-service');

var REQUIRED_OPTS = [
  'userActions',
  'analysisFormsCollection',
  'layerDefinitionModel',
  'analysisDefinitionNodesCollection',
  'configModel',
  'userModel',
  'editorModel',
  'stackLayoutModel',
  'onboardings',
  'onboardingNotification'
];

module.exports = CoreView.extend({

  initialize: function (opts) {
    checkAndBuildOpts(opts, REQUIRED_OPTS, this);

    this._selectedNodeId = opts.selectedNodeId;
    this._infoboxState = this._infoboxState.bind(this);

    this._infoboxModel = new InfoboxModel({
      state: ''
    });

    this._overlayModel = new Backbone.Model({
      visible: this._isLayerHidden()
    });

    this._onboardingLauncher = new OnboardingLauncher({
      view: OnboardingView,
      onboardingNotification: this._onboardingNotification,
      notificationKey: layerOnboardingKey,
      onboardings: this._onboardings
    }, {
      editorModel: this._editorModel,
      selector: 'LayerOnboarding'
    });

    this._getQueryAndCheckState();

    this._initBinds();
  },

  render: function () {
    this._launchOnboarding();
    this.clearSubViews();
    this.$el.empty();

    var self = this;
    var infoboxSstates = [
      {
        state: 'georeference',
        createContentView: function () {
          var name = this._layerDefinitionModel ? this._layerDefinitionModel.getTableName() : '';
          var body = [
            _t('editor.tab-pane.layers.georeference.body', { name: name }),
            _t('editor.layers.georeference.manually-add')
          ].join(' ');

          return Infobox.createWithAction({
            type: 'alert',
            title: _t('editor.tab-pane.layers.georeference.title'),
            body: body,
            mainAction: {
              label: _t('editor.layers.georeference.georeference-button')
            }
          });
        },
        mainAction: self._onGeoreferenceClicked.bind(self)
      }, {
        state: 'layer-hidden',
        createContentView: function () {
          return Infobox.createWithAction({
            type: 'alert',
            title: _t('editor.messages.layer-hidden.title'),
            body: _t('editor.messages.layer-hidden.body'),
            mainAction: {
              label: _t('editor.messages.layer-hidden.show')
            }
          });
        },
        mainAction: self._showHiddenLayer.bind(self)
      }
    ];

    var infoboxCollection = new InfoboxCollection(infoboxSstates);

    var panelWithOptionsView = new PanelWithOptionsView({
      template: panelTemplate,
      className: 'Editor-content',
      editorModel: self._editorModel,
      infoboxModel: self._infoboxModel,
      infoboxCollection: infoboxCollection,
      createContentView: function () {
        return new LayerContentAnalysesView({
          className: 'Editor-content',
          userActions: self._userActions,
          analysisDefinitionNodesCollection: self._analysisDefinitionNodesCollection,
          userModel: self._userModel,
          analysisFormsCollection: self._analysisFormsCollection,
          configModel: self._configModel,
          layerDefinitionModel: self._layerDefinitionModel,
          stackLayoutModel: self._stackLayoutModel,
          selectedNodeId: self._selectedNodeId,
          overlayModel: self._overlayModel
        });
      }
    });

    this.$el.append(panelWithOptionsView.render().el);
    this.addView(panelWithOptionsView);
    return this;
  },

  _initBinds: function () {
    this.listenTo(this._layerDefinitionModel, 'change:visible', this._infoboxState);
    // Only when there is no nodes we should check if node can be georeferenced
    this.listenTo(this._analysisFormsCollection, 'reset remove', this._getQueryAndCheckState);
  },

  _getQueryAndCheckState: function () {
    if (!this._analysisFormsCollection.isEmpty()) {
      this._infoboxState();
      return;
    }

    var analysisDefinitionNodeModel = this._layerDefinitionModel.getAnalysisDefinitionNodeModel();

    fetchAllQueryObjectsIfNecessary(
      {
        queryRowsCollection: analysisDefinitionNodeModel.queryRowsCollection,
        queryGeometryModel: analysisDefinitionNodeModel.queryGeometryModel,
        querySchemaModel: analysisDefinitionNodeModel.querySchemaModel
      },
      this._infoboxState
    );
  },

  _launchOnboarding: function () {
    if (this._onboardingNotification.getKey(layerOnboardingKey)) {
      return;
    }

    if (this._analysisFormsCollection.isEmpty()) {
      this._onboardingLauncher.launch();
    }
  },

  _infoboxState: function () {
    if (this._isLayerHidden()) {
      this._infoboxModel.set({ state: 'layer-hidden' });
      this._overlayModel.set({ visible: true });
    } else if (this._layerDefinitionModel.canBeGeoreferenced() && this._analysisFormsCollection.isEmpty()) {
      this._infoboxModel.set({ state: 'georeference' });
      this._overlayModel.set({ visible: false });
    } else {
      this._infoboxModel.set({ state: '' });
      this._overlayModel.set({ visible: false });
    }
  },

  _showHiddenLayer: function () {
    this._layerDefinitionModel.toggleVisible();
    this._userActions.saveLayer(this._layerDefinitionModel);

    this._infoboxState();
  },

  _isLayerHidden: function () {
    return this._layerDefinitionModel.get('visible') === false;
  },

  _onGeoreferenceClicked: function () {
    AnalysesService.addGeoreferenceAnalysis();
  }
});

@@ -2264,731 +2264,8 @@
   %7B%0A
-        state: 'georeference',%0A        createContentView: function () %7B%0A          var name = this._layerDefinitionModel ? this._layerDefinitionModel.getTableName() : '';%0A          var body = %5B%0A            _t('editor.tab-pane.layers.georeference.body', %7B name: name %7D),%0A            _t('editor.layers.georeference.manually-add')%0A          %5D.join(' ');%0A%0A          return Infobox.createWithAction(%7B%0A            type: 'alert',%0A            title: _t('editor.tab-pane.layers.georeference.title'),%0A            body: body,%0A            mainAction: %7B%0A              label: _t('editor.layers.georeference.georeference-button')%0A            %7D%0A          %7D);%0A        %7D,%0A        mainAction: self._onGeoreferenceClicked.bind(self)%0A      %7D, %7B%0A
     
@@ -5033,224 +5033,8 @@
 %7D);%0A
-    %7D else if (this._layerDefinitionModel.canBeGeoreferenced() && this._analysisFormsCollection.isEmpty()) %7B%0A      this._infoboxModel.set(%7B state: 'georeference' %7D);%0A      this._overlayModel.set(%7B visible: false %7D);%0A
     

Remove georeference state from infobox