/*globals hqDefine, ko, $ */
hqDefine('commtrack/js/sms.js', function () {
    'use strict';
    function CommtrackSettingsViewModel(other_sms_codes) {
        this.keyword = ko.observable();
        this.actions = ko.observableArray();

        this.json_payload = ko.observable();

        this.keyword_error = ko.observable();

        // TODO: sort out possibly removing this redundant declaration in js
        this.action_types = [
            {label: 'Stock on hand', value: 'stockonhand'},
            {label: 'Receipts', value: 'receipts'},
            {label: 'Consumption', value: 'consumption'},
            {label: 'Stock out', value: 'stockout'},
        ];

        this.load = function (data) {
            this.keyword(data.keyword);
            this.actions($.map(data.actions, function (e) {
                return new ActionModel(e);
            }));
        };

        var settings = this;

        this.remove_action = function (action) {
            settings.actions.remove(action);
        };

        this.new_action = function () {
            settings.actions.push(new ActionModel({}));
        };

        this.validate = function () {
            this.keyword_error(null);

            var that = this;
            var valid = true;

            if (!this.keyword()) {
                this.keyword_error('required');
                valid = false;
            }
            if (!this.validate_sms(this, 'keyword', 'command', 'stock_report')) {
                valid = false;
            }

            $.each(this.actions(), function (i, e) {
                if (!e.validate(that)) {
                    valid = false;
                }
            });

            return valid;
        };

        this.presubmit = function () {
            if (!this.validate()) {
                return false;
            }

            var payload = this.to_json();
            this.json_payload(JSON.stringify(payload));
        };

        this.all_sms_codes = function () {
            var keywords = [];

            $.each(other_sms_codes, function (k, v) {
                keywords.push({keyword: k, type: v[0], name: 'product "' + v[1] + '"', id: null});
            });

            keywords.push({keyword: this.keyword(), type: 'command', name: 'stock report', id: 'stock_report'});

            $.each(this.actions(), function (i, e) {
                keywords.push({keyword: e.keyword(), type: 'action', name: e.caption(), id: i});
            });

            return keywords;
        };

        this.sms_code_uniqueness = function (keyword, type, id) {
            var conflict = null;
            $.each(this.all_sms_codes(), function (i, e) {
                if (keyword === e.keyword && !(type === e.type && id === e.id)) {
                    conflict = e;
                    return false;
                }
            });
            return conflict;
        };

        this.validate_sms = function (model, attr, type, id) {
            var conflict = this.sms_code_uniqueness(model[attr](), type, id);
            if (conflict) {
                model[attr + '_error']('conficts with ' + conflict.name);
                return false;
            }
            return true;
        };

        this.to_json = function () {
            return {
                keyword: this.keyword(),
                actions: $.map(this.actions(), function (e) { return e.to_json(); }),
            };
        };
    }

    function ActionModel(data) {
        this.keyword = ko.observable(data.keyword);
        this.caption = ko.observable(data.caption);
        this.type = ko.observable(data.type);
        this.name = data.name;

        this.keyword_error = ko.observable();
        this.caption_error = ko.observable();

        this.validate = function (root) {
            this.keyword_error(null);
            this.caption_error(null);

            var valid = true;

            if (!this.keyword()) {
                this.keyword_error('required');
                valid = false;
            }
            if (!this.caption()) {
                this.caption_error('required');
                valid = false;
            }

            if (!root.validate_sms(this, 'keyword', 'action', root.actions().indexOf(this))) {
                valid = false;
            }

            return valid;
        };

        this.to_json = function () {
            return {
                keyword: this.keyword(),
                caption: this.caption(),
                type: this.type(),
                name: this.name
            };
        };
    }

    function initCommtrackSettingsView($element, settings, other_sms_codes) {
        var model = new CommtrackSettingsViewModel(other_sms_codes);
        $element.submit(function () {
            return model.presubmit();
        });

        model.load(settings);
        $element.koApplyBindings(model);
    }

    $(function () {
        var initial_page_data = hqImport('hqwebapp/js/initial_page_data.js').get;
        var settings = initial_page_data('settings');
        var other_sms_codes = initial_page_data('other_sms_codes');
        initCommtrackSettingsView($('#settings'), settings, other_sms_codes);
    });
});

@@ -149,48 +149,8 @@
 ) %7B%0A
-        this.keyword = ko.observable();%0A
     
@@ -186,24 +186,24 @@
 bleArray();%0A
+
 %0A        thi
@@ -668,48 +668,8 @@
 ) %7B%0A
-            this.keyword(data.keyword);%0A
     
@@ -1181,264 +1181,8 @@
 e;%0A%0A
-            if (!this.keyword()) %7B%0A                this.keyword_error('required');%0A                valid = false;%0A            %7D%0A            if (!this.validate_sms(this, 'keyword', 'command', 'stock_report')) %7B%0A                valid = false;%0A            %7D%0A%0A
     
@@ -1858,122 +1858,8 @@
 );%0A%0A
-            keywords.push(%7Bkeyword: this.keyword(), type: 'command', name: 'stock report', id: 'stock_report'%7D);%0A%0A
     
@@ -2798,32 +2798,32 @@
 = function () %7B%0A
+
             retu
@@ -2831,49 +2831,8 @@
 n %7B%0A
-                keyword: this.keyword(),%0A
     

remove multiaction keyword–related js