var DashRegional = SuperWidget.extend({

    chartTotal: null,

    init: function () {
        var self = this;
        this.render([]);

        window.addEventListener("dashboardData", function (e) {
            self.render(e.detail || []);
        });
    },

    render: function (dados) {
        var mapa = {};

        dados.forEach(function (item) {
            var regiao = String(item.regional || "Sem regiao").trim();
            if (!mapa[regiao]) {
                mapa[regiao] = 0;
            }
            mapa[regiao] += 1;
        });

        var labels = Object.keys(mapa);
        if (!labels.length) {
            this.renderGrafico(["Aguardando filtro"], [0]);
            return;
        }

        labels.sort(function (a, b) {
            return a.localeCompare(b);
        });

        var totais = labels.map(function (l) { return mapa[l]; });
        this.renderGrafico(labels, totais);
    },

    renderGrafico: function (labels, totais) {
        var ctxTotal = document.getElementById("graficoRegionalTotal_" + this.instanceId);
        if (!ctxTotal) return;

        if (this.chartTotal) this.chartTotal.destroy();

        var valueLabelsPlugin = {
            id: "valueLabelsPlugin",
            afterDatasetsDraw: function (chart) {
                var ctx = chart.ctx;
                ctx.save();
                ctx.font = "700 10px Arial";
                ctx.textAlign = "center";

                chart.data.datasets.forEach(function (dataset, datasetIndex) {
                    var meta = chart.getDatasetMeta(datasetIndex);
                    meta.data.forEach(function (bar, index) {
                        var value = dataset.data[index];
                        if (value == null) return;

                        var y = bar.y - 4;
                        var baseline = "bottom";
                        if (y < chart.chartArea.top + 12) {
                            y = bar.y + 10;
                            baseline = "top";
                        }

                        ctx.textBaseline = baseline;
                        ctx.fillStyle = baseline === "top" ? "#ffffff" : "#111111";
                        ctx.fillText(String(value), bar.x, y);
                    });
                });

                ctx.restore();
            }
        };

        this.chartTotal = new Chart(ctxTotal, {
            type: "bar",
            plugins: [valueLabelsPlugin],
            data: {
                labels: labels,
                datasets: [{
                    data: totais,
                    backgroundColor: "#0a5d82"
                }]
            },
            options: {
                responsive: true,
                layout: {
                    padding: {
                        top: 14
                    }
                },
                plugins: { legend: { display: false } },
                scales: {
                    x: {
                        grid: {
                            display: false
                        },
                        ticks: {
                            autoSkip: false,
                            maxRotation: 22,
                            minRotation: 0,
                            callback: function (value, index) {
                                var txt = labels[index] || "";
                                return txt.length > 18 ? txt.substring(0, 18) + "..." : txt;
                            }
                        }
                    },
                    y: {
                        beginAtZero: true,
                        precision: 0,
                        grid: {
                            display: false
                        }
                    }
                }
            }
        });
    }

});
