'use strict';

var utils = require('./utils/utils');
var snakeCase = require('snake-case');

//------------------------------------------------------------------------------
// Routing - Snake case in dynamic segments of routes
//------------------------------------------------------------------------------

module.exports = function(context) {

  var message = 'Use snake case in dynamic segments of routes';

  var report = function(node) {
    context.report(node, message);
  };

  var isRoute = function(node) {
    return utils.isMemberExpression(node.callee) &&
      utils.isThisExpression(node.callee.object) &&
      utils.isIdentifier(node.callee.property) &&
      node.callee.property.name === 'route';
  };

  var isSegment = function(property) {
    return property.key.name === 'path' && property.value.value.indexOf(':') > -1;
  };

  var getSegmentName = function(property) {
    if (!isSegment(property)) return;
    return property.value.value.split(':')[1];
  };

  var isSnakeCase = function(name) {
    return snakeCase(name) === name;
  };

  return {
    CallExpression: function(node) {
      if (!isRoute(node)) return;

      var routeOptions = utils.isObjectExpression(node.arguments[1]) ? node.arguments[1] : false;

      if (routeOptions) {
        routeOptions.properties.forEach(function(property) {
          var segmentName = getSegmentName(property);
          if (segmentName && !isSnakeCase(segmentName)) {
            report(property.value);
          }
        });
      }
    }
  };

};

@@ -865,16 +865,17 @@
 mentName
+s
  = funct
@@ -928,26 +928,31 @@
 return;%0A
+%0A
     
-return
+var path =
  propert
@@ -968,22 +968,214 @@
 alue
-.split(':')%5B1%5D
+;%0A    var pattern = /:(%5Ba-zA-Z0-9-_%5D+)/g;%0A    var segmentNames = %5B%5D;%0A    var execResult;%0A%0A    while (execResult = pattern.exec(path)) %7B%0A      segmentNames.push(execResult%5B1%5D);%0A    %7D%0A%0A    return segmentNames
 ;%0A  
@@ -1186,16 +1186,19 @@
   var is
+Not
 SnakeCas
@@ -1245,17 +1245,17 @@
 e(name) 
-=
+!
 == name;
@@ -1555,16 +1555,17 @@
 mentName
+s
  = getSe
@@ -1573,16 +1573,17 @@
 mentName
+s
 (propert
@@ -1586,16 +1586,17 @@
 perty);%0A
+%0A
         
@@ -1616,37 +1616,55 @@
 Name
+s
  && 
-!isSnakeCase(segmentName)
+segmentNames.filter(isNotSnakeCase).length
 ) %7B%0A

add multiple segments support to the routes-segments-snake-case rule