/* global d3 */
var url = hqImport('hqwebapp/js/initial_page_data').reverse;

function AWCDailyStatusController($scope, $routeParams, $location, $filter, icdsCasReachService, locationsService,
    dateHelperService, navigationService, userLocationId, storageService, haveAccessToAllLocations,
    baseControllersService, isAlertActive, isMobile) {
    baseControllersService.BaseController.call(this, $scope, $routeParams, $location, locationsService,
        dateHelperService, navigationService, userLocationId, storageService, haveAccessToAllLocations,
        false, isMobile);
    var vm = this;
    vm.isAlertActive = isAlertActive;
    vm.sectionSlug = 'icds_cas_reach';
    vm.usePercentage = false;
    vm.forceYAxisFromZero = true;
    vm.serviceDataFunction = icdsCasReachService.getAwcDailyStatusData;

    vm.label = "AWC Daily Status";
    vm.steps = {
        'map': {route: '/icds_cas_reach/awc_daily_status/map', label: 'Map View'},
        'chart': {route: '/icds_cas_reach/awc_daily_status/chart', label: 'Chart View'},
    };
    vm.data = {
        legendTitle: 'Percentage AWCs',
    };
    vm.filters = ['month', 'age', 'gender'];
    vm.rightLegend = {
        info: 'Of the total number of AWCs, the percentage of AWCs that were open yesterday.',
    };

    vm.templatePopup = function(loc, row) {
        var total = row ? $filter('indiaNumbers')(row.all) : 'N/A';
        var inDay = row ? $filter('indiaNumbers')(row.in_day) : 'N/A';
        var percent = row ? d3.format('.2%')(row.in_day / (row.all || 1)) : 'N/A';
        return vm.createTemplatePopup(
            loc.properties.name,
            [{
                indicator_name: 'Total number of AWCs that were open yesterday: ',
                indicator_value: inDay,
            },
            {
                indicator_name: 'Total number of AWCs that have been launched: ',
                indicator_value: total,
            },
            {
                indicator_name: '% of AWCs open yesterday: ',
                indicator_value: percent,
            }]
        );
    };

    vm.init();

    var options = {
        'xAxisTickFormat': '%m/%d/%y',
        'yAxisTickFormat': ",",
        'captionContent': ' Of the total number of AWCs, the percentage of AWCs that were open yesterday.',
    };
    vm.chartOptions = vm.getChartOptions(options);
    vm.chartOptions.chart.color = d3.scale.category10().range();
    vm.chartOptions.chart.xAxis.rotateLabels = -45;
    vm.chartOptions.chart.callback = function(chart) {
        var tooltip = chart.interactiveLayer.tooltip;
        tooltip.contentGenerator(function (d) {
            var findValue = function (values, date) {
                var day = _.find(values, function(num) { return num.x === date; });
                return day.y;
            };
            var total = findValue(vm.chartData[0].values, d.value);
            var value = findValue(vm.chartData[1].values, d.value);
            return vm.tooltipContent(d3.time.format('%m/%d/%y')(new Date(d.value)), value, total);
        });
        return chart;
    };

    vm.tooltipContent = function(monthName, value, total) {
        return "<div>Total number of AWCs that were open on <strong>" + monthName + "</strong>: <strong>" + $filter('indiaNumbers')(value) + "</strong></div>"
        + "<div>Total number of AWCs that have been launched: <strong>" + $filter('indiaNumbers')(total) + "</strong></div>"
        + "<div>% of AWCs open on <strong>" + monthName + "</strong>: <strong>" + d3.format('.2%')(value / (total || 1)) + "</strong></div>";
    };
}

AWCDailyStatusController.$inject = [
    '$scope', '$routeParams', '$location', '$filter',
    'icdsCasReachService', 'locationsService', 'dateHelperService', 'navigationService', 'userLocationId',
    'storageService', 'haveAccessToAllLocations', 'baseControllersService', 'isAlertActive', 'isMobile',
];

window.angular.module('icdsApp').directive('awcDailyStatus', ['templateProviderService', function (templateProviderService) {
    return {
        restrict: 'E',
        templateUrl: templateProviderService.getMapChartTemplate,
        bindToController: true,
        scope: {
            data: '=',
        },
        controller: AWCDailyStatusController,
        controllerAs: '$ctrl',
    };
}]);

@@ -1284,21 +1284,20 @@
  vm.
-template
+get
 Popup
+Data
  = f
@@ -1308,13 +1308,8 @@
 ion(
-loc, 
 row)
@@ -1552,64 +1552,9 @@
 urn 
-vm.createTemplatePopup(%0A            loc.properties.name,
+%5B
 %0A   
@@ -1562,17 +1562,16 @@
         
-%5B
 %7B%0A      
@@ -1984,17 +1984,16 @@
        %7D
-%5D
 %0A       
@@ -1993,17 +1993,17 @@
         
-)
+%5D
 ;%0A    %7D;

use superclass's templatePopup function