angular.module('app').controller('ColumnSettingsCtrl', function ($scope, $rootScope, $timeout, SQConstants, UniqueIdGenerator, AppService, ColumnSettingsService, sqPlugin, L) {
    console.log("ColumnSettingsCtrl initialized");

    $scope.getResultTypeLabel = function () {
        var resultType = getItem($scope.resultTypesList, "value", $scope.config.resultType);
        return resultType ? L.tsq(resultType.name) : '';
    }

    $scope.getMarketLabel = function () {
        var market = getItem($scope.markets, "value", $scope.config.market);
        return market ? market.name : '';
    }

    $scope.onSampleTypeSelect = function (sampleType) {
        $scope.config.sampleType = sampleType;

        changed();
    }

    $scope.onDirectionSelect = function (direction) {
        $scope.config.direction = direction;

        changed();
    }

    $scope.onPlTypeSelect = function (plType) {
        $scope.config.plType = plType;

        changed();
    }

    $scope.onWFResultTypeSelect = function (type) {
        $scope.config.subresult = type;

        changed();
    }

    $scope.onSettingsChange = function () {
        $scope.extSampleType = $scope.config.resultType=='main' || $scope.config.resultType=='portfolio' || $scope.config.resultType=='RetestOnAdditionalMarkets' || $scope.config.resultType=='RetestWithHigherPrecision' || $scope.config.resultType=='WhatIf';

        changed();
    }

    $scope.showCCMainResultOption = function() {
        var resultType = getItem($scope.resultTypesList, "value", $scope.config.resultType);
        return resultType && resultType.crosscheck;
    }

    $scope.isCrossCheck = function() {
        if($scope.config.resultType=='RetestOnAdditionalMarkets' || $scope.config.resultType=='RetestWithHigherPrecision' || $scope.config.resultType=='WhatIf' || $scope.config.resultType=='SequentialOptimization') return false;

        var resultType = getItem($scope.resultTypesList, "value", $scope.config.resultType);
        return resultType && resultType.crosscheck;
    }

    $scope.displayWFValueOptions = function() {
        if($scope.config.resultType!='WalkForwardMatrix' && $scope.config.resultType!='WalkForwardOptimization') {
            return false;
        }

        return  $scope.config.subresult == $scope.columnTypes.wf.value;        
    }

    $scope.isFitness = function() {
        return $scope.config.class == 'Fitness';        
    }
    
    $scope.isMiniEquityChart = function(){
        return $scope.config.class == 'MiniEquityChart'; 
    }

    $scope.hideForWF = function() {
        //osetrene v jave DatabankTableColumnEntry.tryToReturnWFTextValue()
        return $scope.config.class == 'Symbol' || $scope.config.class == 'TimeFrame' || $scope.config.class == 'MiniEquityChart' || $scope.config.class == 'Fitness';
    }

    function changed() {
        if(loading) {
            return;
        }

        if($scope.onChange) {
            $scope.onChange();
        }
    }

    function init() {
        $scope.config = {
            sampleType: $scope.sampleTypes.full,
            direction: $scope.directions.both,
            plType: $scope.plTypes.money,
            resultType: $scope.resultTypes.main,
            confidenceLevel: 50,
            market: 1,
            subresult: $scope.columnTypes.wf.value,
            showMainResult: false
        }
        
        $scope.conditionsEvery = $scope.type=='conditions';

        for (var i = 0; i <= 50; i++) {
            if($scope.type=='manage-views' && i==0) {
                continue;
            }

            $scope.markets.push({'value': i, 'name': i==0 ? 'Every' : i});
        }

        loading = false;
    }

    $scope.instance.setSettings = function(settings) {
        loading = true;

        $scope.config.class = settings.class;
        $scope.config.sampleType = settings.sampleType ? settings.sampleType : $scope.sampleTypes.full;
        $scope.config.direction = settings.direction ? settings.direction : $scope.directions.both;
        $scope.config.plType = settings.plType ? settings.plType : $scope.plTypes.money;
        $scope.config.resultType = settings.resultType ? settings.resultType : $scope.resultTypes.main;
        $scope.config.confidenceLevel = settings.confidenceLevel ? settings.confidenceLevel : 50;
        $scope.config.market = settings.market;
        $scope.config.subresult = settings.subresult ? settings.subresult : $scope.columnTypes.wf.value;
        $scope.config.showMainResult = settings.showMainResult!==undefined ? settings.showMainResult : true;
        loadResultTypes($scope.resultTypesList, ColumnSettingsService.resultTypesList);

        $scope.instance.columnChanged(settings);

        $timeout(function(settings) {
            loading = false;
        }, 0, false);
    }

    $scope.instance.columnChanged = function(settings) {
        if(onlyMainData) {
            $scope.resultTypesList.length = 0;
            $scope.resultTypesList.push(ColumnSettingsService.resultTypesList[0]);

            return;
        }

        if(isSpecialWFColumn(settings)) { //special WF columns
            //result type
            $scope.resultTypesList.length = 0;
            for (var i = 0; i < ColumnSettingsService.resultTypesList.length; i++) {
                if(ColumnSettingsService.resultTypesList[i].walkforward) {
                    $scope.resultTypesList.push(ColumnSettingsService.resultTypesList[i]);
                }
            }
            
            $scope.config.resultType = $scope.resultTypesList[0].value;
            $scope.config.subresult = $scope.columnTypes.wfSpecial.value;
        } else {
            if(onlyMainData) {
                $scope.resultTypesList.length = 0;
                $scope.resultTypesList.push(ColumnSettingsService.resultTypesList[0]);
            
            } else if(settings.class && settings.class=='MiniEquityChart') {
                $scope.resultTypesList.length = 0;
                $scope.resultTypesList.push(ColumnSettingsService.resultTypesList[0]);
                $scope.resultTypesList.push(ColumnSettingsService.resultTypesList[1]);
                
                for (var i = 0; i < ColumnSettingsService.resultTypesList.length; i++) {
                if(ColumnSettingsService.resultTypesList[i].walkforward) {
                    $scope.resultTypesList.push(ColumnSettingsService.resultTypesList[i]);
                }
            }

            } else {
                loadResultTypes($scope.resultTypesList, ColumnSettingsService.resultTypesList);
            }
            
            if(!settings.subresult) {
                $scope.config.subresult = $scope.columnTypes.wf.value;
            }

            if($scope.type == 'wf-chart') {
                 $scope.config.resultType = 'WalkForwardOptimization';
            }
        }

        $scope.onSettingsChange();

        try { $scope.$digest(); } catch (err) {}
    }

    function loadResultTypes(array, data, addOnly) {
        if (!addOnly) array.length = 0;
    
        if (!data) {
            return;
        }
    
        for (var i = 0; i < data.length; i++) {
            array.push(data[i]);
        }
    }

    function isSpecialWFColumn(column) {
        if(!column) {
            return false;
        }

        var item = getItem(columns, 'class', column);
        if (item) {
            return item.columnType==$scope.columnTypes.wfSpecial.value;
        }
        
        var item = getItem(columns, 'class', column.class);
        if (item) {
            return item.columnType==$scope.columnTypes.wfSpecial.value;
        }

        item = getItem(columns, 'class', column.column);
        if (item) {
            return item.columnType==$scope.columnTypes.wfSpecial.value;
        }

        return false;
    }

    $scope.instance.getSettings = function() {  
        return $scope.config;
    }

    $scope.instance.onlyMainData = function() {
        onlyMainData = true;
    }

    $scope.instance.onlyISOOS = function(restricted) {
        $scope.onlyISOOS = restricted;
    }
    
    var onlyMainData = false;
    $scope.onlyISOOS = false;

    $scope.resultSettings = sqPlugin.getPlugins("ResultSettings");
    $scope.resultTypes = ColumnSettingsService.resultTypes;

    $scope.resultTypesList = [];
    loadResultTypes($scope.resultTypesList, ColumnSettingsService.resultTypesList);

    $scope.sampleTypes = ColumnSettingsService.sampleTypes;
    $scope.directions = ColumnSettingsService.directions;
    $scope.plTypes = ColumnSettingsService.plTypes;
    $scope.columnTypes = ColumnSettingsService.columnTypes;

    $scope.config = {}
    $scope.markets = [];

    var columnTypes = SQConstants.getConstants().databankColumnTypes; 
    var columns = SQConstants.getColumnsByType([columnTypes.general, columnTypes.wfSpecial]);

    var loading = true;

    init();
});