/**
 * Created by jhwang on 14-6-23.
 */
/**
 * id generator  创建不重复的各类id
 * TODO: need impl
 */
define(function (require, exports) {
    var Hashtable=require('./hashtable').Hashtable;

//    var numReg=  /\b([A-Za-z]+?)(\d+?)/i;
    var numReg=  /\d+/g;
    var slideReg=/^(\d+?)\.sco$/i;

    if (typeof Array.prototype['max'] == 'undefined') {
        Array.prototype.max = function(){
            return Math.max.apply({},this);
        }
        Array.prototype.min = function(){
            return Math.min.apply({},this);
        }
    }

    function Widget() {
        /**
         * 创建一个不重复的 id
         */
        this.typeNames=new Hashtable();

        this.newId = function (wtype) {
            if(!this.typeNames.contains(wtype)){
                this.typeNames.add(wtype,new Hashtable());
            }
            var arr=this.typeNames.get(wtype).getValues();
            var maxNum=arr.length==0?0:arr.max();
            var result=wtype+(maxNum+1);
            this.loadId(wtype,result);
            return result;
        }
        ;

        /**
         * 占用一个widget id
         * @param wtype
         * @param id
         */
        this.loadId = function (wtype, id) {
            if(wtype === 'path-group') return;
             if(!this.typeNames.contains(wtype)){
                 this.typeNames.add(wtype,new Hashtable());
             }
            var match = id.match(numReg);
            if(match!=null){
                var num= match.max();
                this.typeNames.get(wtype).add(id,parseInt(num));
            };
        };
    }


    function SlideGen() {
        /**
         * 创建一个不重复的 id
         */

        this.Names=new Hashtable();

        this.newId=function () {

            var arr=this.Names.getValues();
            var maxNum=arr.length==0?0:arr.max();
            var result=(maxNum+1)+'.sco';
            this.loadId(result);
            return result;
        }
        ;

        /**
         * 占用一个widget id
         * @param wtype
         * @param id
         */
        this.loadId=function (id) {
            if(this.Names.contains(id)){
                return;
            }
            var match = id.match(slideReg);
            if(match!=null){
                var num= match[1];
                this.Names.add(id,parseInt(num));
            };
        };
    }

    function Relation() {
        /**
         * 创建一个不重复的 id
         */

        this.Names=new Hashtable();

        this.newId=function () {

            var arr=this.Names.getValues();
            var maxNum=arr.length==0?0:arr.max();
            var result='r'+(maxNum+1);
            this.loadId(result);
            return result;
        }
        ;

        /**
         * 占用一个widget id
         * @param wtype
         * @param id
         */
        this.loadId=function (id) {
            if(this.Names.contains(id)){
                return;
            }
            var match = id.match(numReg);
            if(match!=null){
                var num= match.max();
                this.Names.add(id,parseInt(num));
            };
        };
    }

    function TimeNode() {
        /**
         * 创建一个不重复的 id
         */

        this.Names=new Hashtable();

        this.newId=function () {

            var arr=this.Names.getValues();
            var maxNum=arr.length==0?0:arr.max();
            var result='t'+(maxNum+1);
            this.loadId(result);
            return result;
        }
        ;

        /**
         * 占用一个widget id
         * @param wtype
         * @param id
         */
        this.loadId=function (id) {
            if(this.Names.contains(id)){
                return;
            }
            var match = id.match(numReg);
            if(match!=null){
                var num= match.max();
                this.Names.add(id,parseInt(num));
            };
        };
    }

    exports.Widget = Widget;
    exports.TimeNode = TimeNode;
    exports.Relation = Relation;

    // 这是个单实例
    exports.SlideIdGen = new SlideGen();


});