angular.module('app').service('ProjectConditionsService', function ($q, $timeout, AppService, L, SQConstants) {

    this.loadConditions = function(parentObj){
        var conditionsObj = getChildElement(parentObj, "Conditions");
        var conditionObjs = getChildElements(conditionsObj, "Condition");
        var conditions = [];

        for (var i = 0; i < conditionObjs.length; i++) {
            var conditionObj = conditionObjs[i];
            var type = getAttrStringValue(conditionObj, "type", null);
            
            var conditionType = getItem(instance.conditionTypes, 'type', type);
            if(!conditionType) continue;

            var condition = angular.copy(conditionType);

            var fieldObjs = getChildElements(conditionObj, 'Field');
            for(var a=0; a<fieldObjs.length; a++){
                var fieldObj = fieldObjs[a];
                var field = getItem(condition.fields, 'type', getAttrStringValue(fieldObj, "type", null));
                if(!field) continue;

                field.value = fieldObj.childNodes[0] ? fieldObj.childNodes[0].nodeValue : field.defaultValue;

                if(field.type == instance.projectConditionFields.duration){
                    instance.setDurationInDHM(field);
                }
            }

            conditions.push(condition);
        }

        return conditions;
    }

    this.saveConditions = function(parentObj, conditions, xmlDoc){
        var conditionsObj = createChild(parentObj, "Conditions", xmlDoc, true);

        removeAllChildren(conditionsObj);

        for (var i = 0; i < conditions.length; i++) {
            var condition = conditions[i];
            var conditionObj = createChild(conditionsObj, "Condition", xmlDoc, true);

            conditionObj.setAttribute('type', condition.type);

            for(var a=0; a<condition.fields.length; a++){
                var field = condition.fields[a];
                var fieldObj = createChild(conditionObj, "Field", xmlDoc, true);
                fieldObj.setAttribute("type", field.type);
                
                if(field.type == instance.projectConditionFields.duration){
                    setDurationInMinutes(field);
                }

                setNodeValue(fieldObj, field.value, xmlDoc);
            }
        }
    }

    function setDurationInMinutes(field){
        field.value = parseInt(field.days) * 24 * 60;
        field.value += parseInt(field.hours) * 60;
        field.value += parseInt(field.minutes);
    }

    this.setDurationInDHM = function(field){
        var minutes = parseInt(field.value);
        field.days = parseInt(minutes / 24 / 60);
        minutes -= field.days * 24 * 60;
        field.hours = parseInt(minutes / 60);
        minutes -= field.hours * 60;
        field.minutes = minutes;
    }

    var instance = this;

    this.projectConditionFields = SQConstants.getConstants().projectConditionFields;
    this.conditionTypes = SQConstants.getConstants().projectConditionTypes;

});