angular.module('app').controller('SampleTypeCtrl', function ($scope, $rootScope, $timeout, SQConstants, L) {

    $scope.sampleTypes = SQConstants.getConstants().sampleTypes;
    var sampleTypeListAll = SQConstants.getConstants().sampleTypeList;

    $scope.sampleTypeListIn = angular.copy(sampleTypeListAll).filter(function (item) {
        if (!$scope.every && item.value == $scope.sampleTypes.isvEvery) {
            return false;
        }

        return item.name.startsWith("IS");
    });

    $scope.sampleTypeListOut = angular.copy(sampleTypeListAll).filter(function (item) {
        if (!$scope.every && item.value == $scope.sampleTypes.oosEvery) {
            return false;
        }

        return item.name.startsWith("OOS");
    });

    if($scope.instance) {
        $scope.instance.setResultTypes = function (resultTypes) {
            $scope.displaySimpleButtons = resultTypes && ([10,11,20,21].toString() == resultTypes.toString() || [10,11,20].toString() == resultTypes.toString() || [10,20].toString() == resultTypes.toString());

            $scope.sampleTypeListIn.length = 0;
            for (var i = 0; i < sampleTypeListAll.length; i++) {
                var item = sampleTypeListAll[i];

                if (!$scope.every && item.value == $scope.sampleTypes.isvEvery) {
                    continue;
                }

                if(resultTypes && !resultTypes.includes(item.value)) {
                    continue;
                }

                if(!item.name.startsWith("IS")) {
                    continue;
                }

                $scope.sampleTypeListIn.push(item);
            }

            $scope.sampleTypeListOut.length = 0;
            for (var i = 0; i < sampleTypeListAll.length; i++) {
                var item = sampleTypeListAll[i];

                if (!$scope.every && item.value == $scope.sampleTypes.oosEvery) {
                    continue;
                }

                if(resultTypes && !resultTypes.includes(item.value)) {
                    continue;
                }

                if(!item.name.startsWith("OOS")) {
                    continue;
                }

                $scope.sampleTypeListOut.push(item);
            }
        }
    }

    function getTypeOfValue(value) {
        var selectedKey = "full";
        for (itemKey in $scope.sampleTypes) {
            if ($scope.sampleTypes[itemKey] == value) {
                selectedKey = itemKey;
                break;
            }
        }

        if (selectedKey.indexOf("is") >= 0 || selectedKey.indexOf("in") >= 0) {
            return "in";
        }
        if (selectedKey.indexOf("oos") >= 0 || selectedKey.indexOf("out") >= 0) {
            return "out";
        }
        return "full";
    }

    $scope.selected = $scope.sampleTypes.full;
    $scope.inPreselected = $scope.sampleTypes.in;
    $scope.outPreselected = $scope.sampleTypes.out;

    $scope.selectedType = "full";

    $scope.onSampleSelect = function (sampleType) {
        $scope.selected = sampleType;
        $scope.onChange(sampleType);
        $scope.selectedType = "full";
    }

    $scope.onSampleSelectIn = function (sampleType) {
        $scope.selected = sampleType;
        $scope.inPreselected = sampleType;
        $scope.onChange(sampleType);
        $scope.selectedType = "in";
    }

    $scope.onSampleSelectOut = function (sampleType) {
        $scope.selected = sampleType;
        $scope.outPreselected = sampleType;
        $scope.onChange(sampleType);
        $scope.selectedType = "out";
    }

    $scope.getSampleTypeLabel = function (item) {
        var sampleType = getItem(sampleTypeListAll, "value", item);
        return sampleType ? L.tsq(sampleType.name) : $scope.selected;
    }

    $scope.closeDropdown = function ($event) {
        $($event.target).closest(".dropdown, .btn-group").removeClass("open");
    }

    $scope.$on("$destroy", function () {
        destroyModelWatch();
    });

    var destroyModelWatch = $scope.$watch("selected", function (newVal) {
        $scope.selectedType = getTypeOfValue(newVal);
        $scope.inPreselected = $scope.sampleTypes.in;
        $scope.outPreselected = $scope.sampleTypes.out;
        if ($scope.selectedType == "in") {
            $scope.inPreselected = newVal;
        }
        if ($scope.selectedType == "out") {
            $scope.outPreselected = newVal;
        }
    });

});