angular.module("c8y.parts.kpi").controller("kpiEditCtrl",["$scope","$routeParams","$location","kpiSvc","c8yTitle","gettext",function(a,e,n,i,t,l){var d,u=["target","min","max","yellowRangeMin","yellowRangeMax","redRangeMin","redRangeMax"],r={IS_REQUIRED:"required",SHOULD_CONTAIN_NUMBER:"number",SHOULD_BE_DEFINED:"should-be-defined",GREATER_THAN_SCALE_MAX:"greater-than-scale-max",LESS_THAN_SCALE_MIN:"less-than-scale-min",GREATER_THAN_RANGE_MAX:"greater-than-range-max",LESS_THAN_RANGE_MIN:"less-than-range-min"},m={};function o(n){return u.forEach(function(e){n[e]&&!_.isNumber(n[e])&&(n[e]=parseFloat(n[e]))}),n}function E(e){return _.isUndefined(e.enabled)&&(e.enabled=!0),e}function s(n){return _.isUndefined(n.unit)?i.getOneSeries(e.measurement,e.series).then(function(e){return n.unit=e.unit,n}):n}function A(e){d=e,a.kpi=d,t.changeTitle({subtitle:"".concat(d.fragment," / ").concat(d.series)})}function c(e,n){return!!(_.isUndefined(e)||!e&&0!==e||_.isUndefined(n)||!n&&0!==n)||e<n}function N(e,n){return!!(_.isUndefined(e)||!e&&0!==e||_.isUndefined(n)||!n&&0!==n)||e<=n}function R(e,n){e.$setValidity(r.SHOULD_BE_DEFINED,V(e.$modelValue,n.$modelValue)),n.$setValidity(r.SHOULD_BE_DEFINED,V(n.$modelValue,e.$modelValue)),e.$setValidity(r.GREATER_THAN_RANGE_MAX,c(e.$modelValue,n.$modelValue)),n.$setValidity(r.LESS_THAN_RANGE_MIN,c(e.$modelValue,n.$modelValue))}function V(e,n){return!(!_.isUndefined(n)&&(n||0===n))||!_.isUndefined(e)&&(e||0===e)}function $(e,n,i,a){n.$setValidity(r.LESS_THAN_SCALE_MIN,N(e.$modelValue,n.$modelValue)),i.$setValidity(r.GREATER_THAN_SCALE_MAX,N(i.$modelValue,a.$modelValue))}function S(){n.path("/device/".concat(e.deviceId,"/kpi"))}m[r.IS_REQUIRED]=l("This field is required."),m[r.SHOULD_CONTAIN_NUMBER]=l("This field must contain a number."),m[r.SHOULD_BE_DEFINED]=l("Value must be defined."),m[r.GREATER_THAN_SCALE_MAX]=l("Value must be less than scale maximum."),m[r.LESS_THAN_SCALE_MIN]=l("Value must be greater than scale minimum."),m[r.GREATER_THAN_RANGE_MAX]=l("Value must be less than range maximum."),m[r.LESS_THAN_RANGE_MIN]=l("Value must be greater than range minimum."),a.isFormValid=function(){var e,n=a.editKpi;return(e=n).min.$setValidity(r.GREATER_THAN_SCALE_MAX,c(e.min.$modelValue,e.max.$modelValue)),e.max.$setValidity(r.LESS_THAN_SCALE_MIN,c(e.min.$modelValue,e.max.$modelValue)),R(n.yellowRangeMin,n.yellowRangeMax),R(n.redRangeMin,n.redRangeMax),$(n.min,n.yellowRangeMin,n.yellowRangeMax,n.max),$(n.min,n.redRangeMin,n.redRangeMax,n.max),(e=n).target.$setValidity(r.LESS_THAN_SCALE_MIN,N(e.min.$modelValue,e.target.$modelValue)),e.target.$setValidity(r.GREATER_THAN_SCALE_MAX,N(e.target.$modelValue,e.max.$modelValue)),n.$valid},a.isFieldInvalid=function(e){return(e=a.editKpi[e])&&e.$invalid},a.getFieldValidationHints=function(e){var i=[];return a.editKpi[e]&&a.editKpi[e].$error&&_.forEach(a.editKpi[e].$error,function(e,n){e&&i.push(m[n])}),i.join(" ")},a.save=function(){i.updateKpi(d).then(S)},a.cancel=function(){S()},i.getKpi(e.measurement,e.series).then(o).then(E).then(s).then(A)}]);