/*
 NAME: 
 DESCRIPTION: 导入gerber资料
 PARAMETER:
 	[
	    {
			name : 'rename_layer_rule',
			title : '原始/正式层名规则',
			type : 'TextEdit',
			property:{tool_tip:"参数配置：{rule1:[{orig_name:'org',new_name:'new'},..],rule2:[]..}，注意引号要用单引号"},
			setter:function(obj,value,self){
				obj.setPlainText(value);
			},
			getter:function(obj,self){
				return obj.plainText
			}
		},
		{
			name : 'ori_step',
			type : 'LineEdit',
			title : '原稿step名称',
			pack : {row:0,column:1},
			property:{tool_tip:'若为空，则默认ori'},
		},
		{
			name : 'db',
			type : 'LineEdit',
			title : 'genesis db',
			pack : {row:0,column:1},
			property:{tool_tip:'genesis新料号存放db，未设定则默认为genesis'},
		},
	]
	
  
 HELP:
	<html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		  <p> 导入gerber资料以及合并、改名、排序 </p>
		  <br>
		<font size="3" color="#003DB2"><p>参数配置</p></font>
		<font color="#008000"><p> ● 无</p></font>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		  <p> ● 无 </p>
		  <br>
	</body></html>
*/


var $ = require('topcam.scriptfunc').argv();
var ikm = $.ikm;
var gen = $.gen;
var gui = $.gui;
var fs = require('fs');
var Job = 'demo1', step = 'orig';
try {
    
    var ReNameRule1 = [
    	{ orig_name : "pasttop.art", new_name : "past_top" },
    	{ orig_name : "pleg.art", new_name : "silk_top" },
    	{ orig_name : "soldtop.art", new_name : "sm_top" },
    	{ orig_name : "art(\\d+)\\.art", new_name : "layer_(\\d)" },
    	{ orig_name : "soldbotm.art", new_name : "sm_botm" },
    	{ orig_name : "sleg.art", new_name : "silk_bot" },
        { orig_name : "pastbotm.art", new_name : "past_botm" },
    	{ orig_name : "multipack.art", new_name : "array" },
    	{ orig_name : "outline.art", new_name : "outline" },
    	{ orig_name : "fab.art", new_name : "fab.art" },
    	{ orig_name : "top-enig", new_name : "top-enig" },
    	{ orig_name : "bot-enig", new_name : "bot-enig" },
    	{ orig_name : "fab_page2", new_name : "fab_page2" }
    ];

    var reNameRules = {
        ReNameRule1: ReNameRule1
    }

    var matrix = gen.getMatrix({job:Job})
    var matrixNames = Object.keys(matrix).map(function(key){
        return matrix[key]['name']
    })
    
    function getRenameCount(namelist, rule){  // 传入名称列表  和 命名 规则  得到符合规则的名称列表
        var res = [];
        var max = {index:-1,value:"0"};
        namelist.forEach(function(v){
            rule.forEach(function(rule){
                var evalReg = "/" + rule.orig_name + "/ig.test(v)";
                if(eval(evalReg)){
                    var new_name;
                    if(v === rule.orig_name){
                        new_name = rule.new_name;
                    } else {
                        max.index++;
                        var evaltmp = "/" + rule.orig_name + "/ig.exec(v)";
                        var tmp =  eval(evaltmp);
                        if(Number(tmp[1])>Number(max.value)){
                            max.value = tmp[1];
                        }
                        if(tmp[1] <= 1){
                            new_name = "top";
                        } else {
                            new_name = rule.new_name.replace("(\\d)",tmp[1]);
                        }
                    }
                    res.push({
                        orig_name:  v,
                        new_name: new_name
                    })
                }
            })
        })
        res[max.index]["new_name"] = "bottom";
        return res;
    }

    var reNameList = [];  // 记录匹配程度最高的改名结果
    Object.keys(reNameRules).forEach(function(key){
        var rule = reNameRules[key];
        var renames =  getRenameCount(matrixNames,rule);
        if(renames.length > reNameList.length){
            reNameList = renames;
        }
    })
    // 更改层名
    for(var i in reNameList){
        gen.renameLayer({job:Job,layer:reNameList[i].orig_name,new_name:reNameList[i].new_name})
    }
    gui.msg('end')
    return 'done';
}
catch (error) {
    gui.msg(error);
    return 'error';
}
