_snippet.define(('userSnippetsList'),function(require, _) {
    
var USER_CONFIG_PATH = dw.getUserConfigurationPath();
var CONFIG_PATH = dw.getConfigurationPath();
var userSnippetsPath= USER_CONFIG_PATH+"Snippets/dwSnippets.json";
    
var file = require('file');
var readSnippets = function(absPath){
    var tempContent=null;
    if(file.fileExist(absPath)){
        file.read(absPath, function(err, content) {
            if (err) { 
                return;
            } 
            tempContent=JSON.parse(String(content));
        });
        
    }
    return tempContent;
};
							
var userDefinedSnippetsList=readSnippets(userSnippetsPath);
function isAllowedChar(ch) {
    var charCode = ch.charCodeAt(0);
    var notAllowedChars = "\t<>@!#$%^&*()+[]{}?:;|'\"\\,./~`-= ";
    return (notAllowedChars.indexOf(ch) == -1);
}
function getTriggerKey(str) {
    var curOffset = str.length;
    var startIndex = -1;   
    while (true) {
        curOffset--;
        if (curOffset < 0) {
            // moved to the beginning of the line
            startIndex = 0;
            break;
        }
        var ch = str.charAt(curOffset);
       
        if (!isAllowedChar(ch)) {
            // found stop symbol           
            startIndex = curOffset + 1;
            break;
        }
        
    }
    return str.substring(startIndex);

}
    return {
        reloadSnippetsList:function()
        {
            userDefinedSnippetsList=readSnippets(userSnippetsPath);            
        },
        getSnippetId: function(newTriggerKey)
        {
            if(userDefinedSnippetsList !=null && newTriggerKey in userDefinedSnippetsList.snippets){
                return userDefinedSnippetsList.snippets[newTriggerKey];
            }
            return false;                   
                
        },
        addSnippet: function(newTriggerKey,snippetID)
        {   
            if(userDefinedSnippetsList!= null)
            {
                if(newTriggerKey in userDefinedSnippetsList.snippets)
                {                    		
                    return false;
                }
                userDefinedSnippetsList.snippets[newTriggerKey]=snippetID;
               	
                file.save(userSnippetsPath,JSON.stringify(userDefinedSnippetsList));
            }
            else
            {
                var item={snippets:{}};
                item.snippets[newTriggerKey] = snippetID;
                file.save(userSnippetsPath,JSON.stringify(item));
                userDefinedSnippetsList=readSnippets(userSnippetsPath);
            }
            return true;
        },
        removeSnippet:function(oldTriggerKey)
        {
            if(userDefinedSnippetsList!= null && oldTriggerKey in userDefinedSnippetsList.snippets)
            {
                delete userDefinedSnippetsList.snippets[oldTriggerKey];
                file.save(userSnippetsPath,JSON.stringify(userDefinedSnippetsList));                
            }
            return true;
        },
        replaceSnippet:function(oldTriggerKey,newTriggerKey,snippetID)
        {
            if(userDefinedSnippetsList == null){
                return false;
            }
            if(userDefinedSnippetsList != null && newTriggerKey in userDefinedSnippetsList.snippets){
               
                if(oldTriggerKey == newTriggerKey && userDefinedSnippetsList.snippets[oldTriggerKey]==snippetID)
                    return false;
                else if(oldTriggerKey != newTriggerKey)
                    return false;
                
            }
                
           
            delete userDefinedSnippetsList.snippets[oldTriggerKey];
            userDefinedSnippetsList.snippets[newTriggerKey]=snippetID;		
            file.save(userSnippetsPath,JSON.stringify(userDefinedSnippetsList));
            return true;
        },
        insertSnippet: function(editor)
        {		
            var caretPos = editor.getSelectionRange().end;
            var range = require('range').create(editor.getSelectionRange());			
			var content = String(editor.getContent());			
            var curLine = editor.getCurrentLineRange();
            
			if(range.length())
                return false;
			var triggerkey= getTriggerKey(content.substring(curLine.start, range.start));
            
            
           	if (!_.isEmpty(triggerkey)) {
                if (!_.isEmpty(content)) {
                    if(userDefinedSnippetsList != null && triggerkey in userDefinedSnippetsList.snippets)
                    {
                        var value=String(userDefinedSnippetsList.snippets[triggerkey]);	
                        editor.createSelection( caretPos - triggerkey.length, caretPos);
                        dw.snippetPalette.insertSnippet(userDefinedSnippetsList.snippets[triggerkey]);	
                       
                        return true;
                    }
                
                }
            }
		
            return false;
        }      
	
    };
});

