/**********************************************************************
 * Copyright (c) 2014 HubSpot Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **********************************************************************/
package com.hubspot.jinjava.lib.tag;

import org.apache.commons.lang3.StringUtils;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.TagNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * {% set primary_line_height = primary_font_size_num*1.5 %}
 *
 * {% set lw_font_size = "font-size: " ~ lw_font_size_base ~ "px; " %} {% set lw_secondary_font_size = "font-size: " ~ (lw_font_size_base - 2) ~ "px; " %} {% set lw_line_height = "line-height: " ~ lw_font_size_base*1.5 ~ "px; " %}
 *
 * @author anysome
 *
 */
@JinjavaDoc(
    value = "Assigns the value or result of a statement to a variable",
    params = {
        @JinjavaParam(value = "var", type = "variable identifier", desc = "The name of the variable"),
        @JinjavaParam(value = "expr", type = "expression", desc = "The value stored in the variable (string, number, boolean, or sequence")
    },
    snippets = {
        @JinjavaSnippet(
            desc = "Set a variable in with a set statement and print the variable in a expression",
            code = "{% set primaryColor = \"#F7761F\" %}\n" +
                "{{ primaryColor }}\n"),
        @JinjavaSnippet(
            desc = "You can combine multiple values or variables into a sequence variable",
            code = "{% set var_one = \"String 1\" %}\n" +
                "{% set var_two = \"String 2\" %}\n" +
                "{% set sequence = [var_one,  var_two] %}"),
    })
public class SetTag implements Tag {

  private static final long serialVersionUID = -8558479410226781539L;
  private static final String TAGNAME = "set";

  @Override
  public String getName() {
    return TAGNAME;
  }

  @Override
  public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
    if (!tagNode.getHelpers().contains("=")) {
      throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'set' expects an assignment expression with '=', but was: " + tagNode.getHelpers(), tagNode.getLineNumber());
    }

    int eqPos = tagNode.getHelpers().indexOf('=');
    String var = tagNode.getHelpers().substring(0, eqPos).trim();
    String expr = tagNode.getHelpers().substring(eqPos + 1, tagNode.getHelpers().length());

    if (var.length() == 0) {
      throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'set' requires a var name to assign to", tagNode.getLineNumber());
    }
    if (StringUtils.isBlank(expr)) {
      throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'set' requires an expression to assign to a var", tagNode.getLineNumber());
    }

    String[] varTokens = var.split(",");

    if(varTokens.length > 1) {

      //handle multi-variable assignment

      List<String> exprTokens = Pattern.compile("(,)(?=(?:[^']|'[^']*')*$)").splitAsStream(expr).collect(Collectors.toList());

      if (varTokens.length != exprTokens.size()) {
        throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'set' declares an uneven number of variables and assigned values", tagNode.getLineNumber());
      }

      for (int i = 0; i < varTokens.length; i++) {
        String varItem = varTokens[i].trim();
        String exprItem = exprTokens.get(i).trim();
        Object val = interpreter.resolveELExpression(exprItem, tagNode.getLineNumber());
        interpreter.getContext().put(varItem, val);
      }

    } else {

      //handle single variable assignment

      Object val = interpreter.resolveELExpression(expr, tagNode.getLineNumber());
      interpreter.getContext().put(var, val);

    }

    return "";
  }

  @Override
  public String getEndTagName() {
    return null;
  }

}

@@ -1164,120 +1164,12 @@
 til.
-ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.regex.Matcher
+List
 ;%0Aim

remove unused imports