/**
 * Knockout view models to represent current state of map page.
 * Copyright (c) 2014 University of Oxford
 */
'use strict';

ko.bindingHandlers.counter = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        var counter = $(element).FlipClock(ko.unwrap(valueAccessor()), {
            clockFace: "Counter"
        });
        ko.utils.domData.set(element, "counter", counter);
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        var counter = ko.utils.domData.get(element, "counter");
        counter.setValue(ko.unwrap(valueAccessor()));
    }
};

var DataValidationViewModels = (function() {
    var LayerSelectorViewModel = function () {
        this.validationTypes = ko.observableArray(["disease occurrences", "disease extent"]);
        this.selectedType = ko.observable();
        this.diseaseInterests = ko.observableArray(diseaseInterests);
        this.selectedDisease = ko.observable();
        this.selectedDisease.subscribe(function () {
            DataValidationViewModels.selectedPointViewModel.clearSelectedPoint();
            LeafletMap.switchDiseaseLayer(this.selectedDisease().id);
            DataValidationViewModels.counterViewModel.reviewCount(this.selectedDisease().reviewCount);
        }, this);
    };

    var SelectedPointViewModel = function () {
        this.selectedPoint = ko.observable(null);
        this.hasSelectedPoint = ko.computed(function () {
            return this.selectedPoint() !== null;
        }, this);
        this.clearSelectedPoint = function () {
            this.selectedPoint(null);
        };
        this.translationUrl = ko.computed(function () {
            if (this.hasSelectedPoint()) {
                var googleTranslateUrl = "http://translate.google.com/"
                var feedLanguage = this.selectedPoint().properties.alert.feedLanguage;
                var encodedSummary = encodeURIComponent(this.selectedPoint().properties.alert.summary);
                if (feedLanguage != null) {
                    return googleTranslateUrl + "?langpair=" + feedLanguage + "%7Cen&text=" + encodedSummary;
                } else {
                    return googleTranslateUrl + "#auto/en/" + encodedSummary;
                }
            }
        }, this);
        this.submitReview = function(review) {
            return function () {
                var diseaseId = DataValidationViewModels.layerSelectorViewModel.selectedDisease().id;
                var feature = DataValidationViewModels.selectedPointViewModel.selectedPoint();
                var occurrenceId = feature.id;
                var url = baseUrl + "datavalidation/diseases/" + diseaseId + "/occurrences/" + occurrenceId + "/validate";
                $.post(url, { review: review })
                    .done(function(data, status, xhr) {
                        // Status 2xx
                        // Remove the point from the map and side panel, display a success alert, increment the counter
                        DataValidationViewModels.selectedPointViewModel.clearSelectedPoint();
                        $("#submitReviewSuccess").fadeIn();
                        LeafletMap.removeReviewedPoint(feature.id);
                        DataValidationViewModels.counterViewModel.incrementCount();
                    })
                    .fail(function (xhr) {
                        alert("Something went wrong. Please try again. " + xhr.responseText);
                    });
            };
        };
    };

    var CounterViewModel = function() {
        this.reviewCount = ko.observable();
        this.incrementCount = function () {
            this.reviewCount(this.reviewCount() + 1);
        };
    }

    var layerSelectorViewModel = new LayerSelectorViewModel();
    var selectedPointViewModel = new SelectedPointViewModel();
    var counterViewModel = new CounterViewModel();

    $(document).ready(function () {
        ko.applyBindings(layerSelectorViewModel, $("#layerSelector")[0]);
        ko.applyBindings(selectedPointViewModel, $("#datapointInfo")[0]);
        ko.applyBindings(counterViewModel, $("#counterDiv")[0]);
    });

    return {
        layerSelectorViewModel: layerSelectorViewModel,
        selectedPointViewModel: selectedPointViewModel,
        counterViewModel: counterViewModel
    }
}());





@@ -2066,32 +2066,33 @@
          return 
+(
 googleTranslateU
@@ -2148,32 +2148,51 @@
 + encodedSummary
+).substring(0,2048)
 ;%0A              
@@ -2229,16 +2229,17 @@
  return 
+(
 googleTr
@@ -2279,16 +2279,35 @@
 dSummary
+).substring(0,2048)
 ;%0A      

Truncate URL string to max allowable length, to prevent 414 error from Google Translate, for now.