'use strict';

var LSetup = require('../config/leaflet_setup');
var clearControl = require('../controls/clear');
var dataHandler = require('./data_handler');
var geocoder = require('./geocoder');
var address = require('./address');
var solveControl = require('../controls/solve');
var panelControl = require('../controls/panel');
var fitControl = require('../controls/fit');

var checkSolveControl = function(){
  if(!LSetup.map.solveControl){
    if((dataHandler.getStart() || dataHandler.getEnd())
       && (dataHandler.getJobsSize() > 0)){
      solveControl.addTo(LSetup.map);
    }
  }
  else{
    if(dataHandler.getJobsSize() === 0){
      LSetup.map.removeControl(solveControl);
    }
  }
}

// Add locations.
var addPlace = function(latlng, name){
  panelControl.hideInitDiv();

  if(!LSetup.map.fitControl){
    LSetup.map.addControl(fitControl);
  }
  if(!LSetup.map.clearControl){
    LSetup.map.addControl(clearControl);
  }
  if(dataHandler.isFirstPlace()){
    // Add vehicle start/end.
    dataHandler.firstPlaceSet();

    if(name){
      // Add start with provided name.
      dataHandler.addStart(latlng, name, checkSolveControl);
      dataHandler.addEnd(latlng, name, checkSolveControl);
    }
    else{
      geocoder.defaultGeocoder.reverse(latlng, LSetup.map.options.crs.scale(19), function(results){
        var r = results[0];
        if(r){
          name = address.display(r);
          // Add start based on geocoding result.
          dataHandler.addStart(latlng, name, checkSolveControl);
          dataHandler.addEnd(latlng, name, checkSolveControl);
        }
      });
    }
  }
  else{
    // Add regular job.
    if(name){
      // Add job with provided name.
      dataHandler.addJob(latlng, name, checkSolveControl);
      checkSolveControl();
    }
    else{
      geocoder.defaultGeocoder.reverse(latlng, LSetup.map.options.crs.scale(19), function(results){
        var r = results[0];
        if(r){
          name = address.display(r);
          // Add job based on geocoding result.
          dataHandler.addJob(latlng, name, checkSolveControl);
          checkSolveControl();
        }
      });
    }
  }
}

module.exports = {
  addPlace: addPlace
};

@@ -378,28 +378,24 @@
 ar check
-Solve
 Control
+s
  = funct
@@ -407,46 +407,72 @@
 %7B%0A  
-if(!LSetup.map.solveControl)%7B%0A    if((
+var hasJobs = (dataHandler.getJobsSize() %3E 0);%0A  var hasStart = 
 data
@@ -493,11 +493,24 @@
 rt()
- %7C%7C
+;%0A  var hasEnd =
  dat
@@ -530,51 +530,436 @@
 nd()
-)%0A       && (dataHandler.getJobsSize() %3E 0)
+;%0A  if(hasJobs %7C%7C hasStart %7C%7C hasEnd)%7B%0A    // Fit and clear controls as soon as we have a location.%0A    if(!LSetup.map.fitControl)%7B%0A      LSetup.map.addControl(fitControl);%0A    %7D%0A    if(!LSetup.map.clearControl)%7B%0A      LSetup.map.addControl(clearControl);%0A    %7D%0A  %7D%0A  if(!LSetup.map.solveControl)%7B%0A    // Solve control appears only when there's enough input to fire a%0A    // solving query.%0A    if((hasStart %7C%7C hasEnd) && hasJobs
 )%7B%0A 
@@ -1205,158 +1205,8 @@
 );%0A%0A
-  if(!LSetup.map.fitControl)%7B%0A    LSetup.map.addControl(fitControl);%0A  %7D%0A  if(!LSetup.map.clearControl)%7B%0A    LSetup.map.addControl(clearControl);%0A  %7D%0A
   if
@@ -1394,36 +1394,32 @@
  name, check
-Solve
 Control
+s
 );%0A      dat
@@ -1449,36 +1449,55 @@
  name, check
-Solve
+Controls);%0A      check
 Control
+s(
 );%0A    %7D%0A   
@@ -1779,36 +1779,32 @@
  name, check
-Solve
 Control
+s
 );%0A         
@@ -1838,36 +1838,59 @@
  name, check
-Solve
+Controls);%0A          check
 Control
+s(
 );%0A        %7D
@@ -2033,36 +2033,32 @@
  name, check
-Solve
 Control
+s
 );%0A      che
@@ -2055,36 +2055,32 @@
 %0A      check
-Solve
 Control
+s
 ();%0A    %7D%0A  
@@ -2363,28 +2363,24 @@
 e, check
-Solve
 Control
+s
 );%0A     
@@ -2393,20 +2393,16 @@
 heck
-Solve
 Control
+s
 ();%0A

Improve control hide/display. #16