package com.lifeonwalden.codeGenerator.javaClass.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.lifeonwalden.codeGenerator.ConstBasedGenerator;
import com.lifeonwalden.codeGenerator.bean.EnumConst;
import com.lifeonwalden.codeGenerator.bean.ValueEnum;
import com.lifeonwalden.codeGenerator.bean.config.Config;
import com.lifeonwalden.codeGenerator.util.OutputUtilities;

public class JsEnumGeneratorImpl implements ConstBasedGenerator {

    @Override
    public String generate(List<EnumConst> enumConstList, Config config) {
        StringBuilder sb = new StringBuilder();

        OutputUtilities.newLine(sb
                        .append("if (typeof Object.assign != 'function') {  Object.assign = function(target) {    'use strict';    if (target == null) {      throw new TypeError('Cannot convert undefined or null to object');    }    target = Object(target);    for (var index = 1; index < arguments.length; index++) {      var source = arguments[index];      if (source != null) {        for (var key in source) {          if (Object.prototype.hasOwnProperty.call(source, key)) {            target[key] = source[key];          }        }      }    }    return target;  };}"));
        OutputUtilities.newLine(sb);

        OutputUtilities.newLine(sb.append("function Enum(){this._index = []; this._values=[]}"));
        OutputUtilities.newLine(sb
                        .append("Enum.prototype.values=function(){if(0==this._values.length){for (var i in this){var tmp = this[i]; if((typeof tmp == 'object') && tmp.hasOwnProperty('value') && tmp.hasOwnProperty('desc')){this._values.push(tmp);this._index[tmp.value]=tmp;}}} return this._values;};"));
        OutputUtilities.newLine(sb.append("Enum.prototype.valueOf=function(val){if(0==this._index.length) this.values();return this._index[val];};"));
        OutputUtilities.newLine(sb);

        for (EnumConst enumConst : enumConstList) {
            if (null != enumConst.getNote() && enumConst.getNote().length() > 0) {
                OutputUtilities.newLine(sb.append("// ").append(enumConst.getNote()));
            }

            sb.append("var ").append(enumConst.getName()).append(" = Object.assign(new Enum(), {");
            for (ValueEnum value : enumConst.getOptions()) {
                sb.append(value.getName()).append(" : { name : '").append(value.getName()).append("', value :").append(value.getValue())
                                .append(", alias :'").append(value.getAlias()).append("',desc:'").append(value.getDesc()).append("'},");
            }
            sb.deleteCharAt(sb.length() - 1).append("});");
            OutputUtilities.newLine(sb);
        }

        try {
            File outputFile =
                            new File(new File(config.getOutputLocation()).getPath() + File.separator + config.getConstInfo().getFolderName() + "\\"
                                            + config.getConstInfo().getPackageName() + ".js");
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), config.getEncoding()));
            bw.write(sb.toString());
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

@@ -3020,15 +3020,8 @@
 me()
- + %22%5C%5C%22
 %0D%0A  
@@ -3054,23 +3054,32 @@
         
-       
++ File.separator
  + confi

change \ to File.separator