package com.sysu.admin.api.crop;

import com.sysu.admin.controller.aland.StatItemData;
import junit.framework.TestCase;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.io.VelocityWriter;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.VelocimacroFactory;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class CropControllerTest {

    @Test
    public void runVelocityModel() throws IOException {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.setProperty(RuntimeConstants.INPUT_ENCODING, Charset.forName("GBK"));
        ve.init();
        Template t = ve.getTemplate("styleModel.vm");
        VelocityContext context = new VelocityContext();

        for(String key : StatItemData.typeMap.keySet()){
            String field =  StatItemData.typeMap.get(key)[1];
            String[] names = StatItemData.get(key,"Names");
            String[] colors = StatItemData.get(key,"Colors");
            Map<String, String>[] rules = new HashMap[names.length];
            for(int i=0;i<names.length;i++){
                Map<String, String> item = new HashMap<>();
                item.put("title",names[i]);
                item.put("field",field);
                item.put("value",i+"");
                item.put("color",colors[i]);
                rules[i] = item;
            }
            context.put("name","leizhou_land_"+key+"_type");
            context.put("rules",rules);
            File tempFile = new File("E:/FFout","leizhou_land_"+key+"_type");
            FileOutputStream fs=new FileOutputStream(tempFile);
            OutputStreamWriter fileWriter=new OutputStreamWriter(fs, Charset.forName("UTF-8"));
            t.merge(context, fileWriter);
            fileWriter.flush();
            fileWriter.close();
        }


    }

}
