angular.module('app').service('ColumnSettingsService', function(SQConstants, L) {
 
    this.printColumnTitle = function(valueObj) {
        var columns = SQConstants.getColumnsByType([instance.columnTypes.general, instance.columnTypes.wfSpecial]);

        var columnClass = correctAttrValue(valueObj.attributes["class"]);
        var column = getItem(columns, "class", columnClass);
        var columnName = column ? L.tsq(column.name) : columnClass;

        var direction = correctAttrValue(valueObj.attributes["direction"]); 
        var sampleType = correctAttrValue(valueObj.attributes["sampleType"]); 
        var plType = correctAttrValue(valueObj.attributes["plType"]); 
        var resultType = correctAttrValue(valueObj.attributes["resultType"]); 

        var confidenceLevel = getAttrIntValue(valueObj, "confidenceLevel", 50);
        var market = getAttrIntValue(valueObj, "market", 1);
        var subresultType = getAttrStringValue(valueObj, "subresult", instance.wfSubresultsList[0].value);
        
        var subresultObj = getItem(instance.wfSubresultsList, "value", subresultType);
        if(subresultObj) {
            subresult = subresultObj.name;
        }

        var resultTypeObject = getItem(instance.resultTypesList, "value", resultType);
        var isCrossCheck = resultTypeObject && resultTypeObject.crosscheck;
        if(isCrossCheck) {
            var title = resultTypeObject.title;
            title = title.replace("#columnName#", columnName);
            title = title.replace("#confidenceLevel#", confidenceLevel);
            title = title.replace("#market#", market==0 ? L.tsq("Every market") : L.tsq("Market %s", [market]));
            title = title.replace("#subresult#", subresult);

            if(resultType=='RetestOnAdditionalMarkets' || resultType=='RetestWithHigherPrecision' || resultType=='WhatIf' || resultType=='SequentialOptimization') {
                var sampleTypeCC = instance.getSampleType(sampleType);
                var directionCC = instance.getDirection(direction);
                var plTypeCC = instance.getPlType(plType);
                
                title = title.replace(", #sampleType#", sampleTypeCC ? ", "+sampleTypeCC : '');
                title = title.replace(", #direction#", directionCC ? ", "+directionCC : '');
                title = title.replace(", #plType#", plTypeCC ? ", "+plTypeCC : '');

                return title;
            }

            if(resultType!='WalkForwardMatrix' && resultType!='WalkForwardOptimization') {
                return title;
            }

            if(subresultType != instance.columnTypes.wf.value) {
                return title;
            }

            columnName = title;
        }

        var text = columnName;

        resultType = isCrossCheck ? null : instance.getResultType(resultType);
        direction = instance.getDirection(direction);
        sampleType = instance.getSampleType(sampleType);
        plType = instance.getPlType(plType);
        
        if(!resultType && !direction && !sampleType && !plType) return text;

        text += ' (';
        if(resultType) text += resultType + ', ';
        if(sampleType) text += sampleType + ', ';
        if(direction) text += direction + ', ';
        if(plType) text += plType + ', ';
        
        if(text.charAt(text.length - 2) == ','){
            text = text.substr(0, text.length - 2);
        }

        text += ')';

        return text;
    }

    this.getDirection = function(direction){
        switch(parseInt(direction)){
            case instance.directions.both:
                return null;
            case instance.directions.long:
                return L.tsq("Long");
            case instance.directions.short:
                return L.tsq("Short");
            default:
                return "?";
        }
    }

    this.getSampleType = function(sampleType){
        if(parseInt(sampleType) == instance.sampleTypes.full) return null;

        var sampleType = getItem(instance.sampleTypeList, "value", sampleType);
        return sampleType ? sampleType.name : '? ('+sampleType+')';
    }

    this.getPlType = function(plType){
        switch(parseInt(plType)){
            case instance.plTypes.money:
                return null;
            case instance.plTypes.percent:
                return "%";
            case instance.plTypes.pips:
                return L.tsq("Pips");
            case instance.plTypes.ticks:
                return L.tsq("Ticks");
            default:
                return "?";
        }
    }

    this.getResultType = function(resultType){
        var resultType = getItem(instance.resultTypesList, "value", resultType);
        if(resultType) {
            return resultType.value == instance.resultTypes.main ? null : L.tsq(resultType.name);
        }

        return '?';
    }

    this.resultTypes = SQConstants.getConstants().resultTypes;
    this.resultTypesList = SQConstants.getConstants().resultTypesList;
    this.sampleTypes = SQConstants.getConstants().sampleTypes;
    this.sampleTypeList = SQConstants.getConstants().sampleTypeList;
    this.directions = SQConstants.getConstants().directions;
    this.plTypes = SQConstants.getConstants().plTypes;
    this.wfSubresultsList = SQConstants.getConstants().wfSubresultsList;
    this.columnTypes = SQConstants.getConstants().databankColumnTypes;

    var instance = this;
});