// CUSTOM MADE PLUGINS FOR DISPLAYING DATA ABOUT GRAPHS AND CHARTS

// CALCULATING FONTSIZE USING MATH TO MAKE IT DYNAMIC WITH THE WINDOW SIZE
// MINIMUM SIZE IS 12 PX IN THE LOGIC

// SHOWING REVENUE / EXPENSE IN LACS OR THOUSANDS BASED ON SIZE
function showDataLabels(multiplierThousands = 1000, multiplierLakhs = multiplierThousands*100, multiplierCrores = multiplierLakhs*100) {
    return {
        id: 'showDataLabels_' + Math.random().toString(36).substring(2, 9),
        afterDatasetsDraw(chart) {
            const { ctx } = chart;
            const fontSize = Math.max(0.008 * Math.max(window.innerWidth, window.innerHeight), 11);

            chart.data.datasets.forEach((dataset, i) => {
                const meta = chart.getDatasetMeta(i);
                if (!chart.isDatasetVisible(i)) return;

                meta.data.forEach((bar, index) => {
                    let value = dataset.data[index];
                    if (value == null) return;

                    value = parseFloat(value);
                    ctx.fillStyle = 'white';
                    ctx.font = `bold ${fontSize}px sans-serif`;
                    ctx.textAlign = 'center';

                    if (value / multiplierCrores > 1) {
                        value = parseFloat(value / multiplierCrores).toFixed(2) + " Cr";
                    } else if (value / multiplierLakhs > 1) {
                        value = parseFloat(value / multiplierLakhs).toFixed(2) + " L";
                    } else if (value / multiplierThousands > 1) {
                        value = parseFloat(value / multiplierThousands).toFixed(2) + " K";
                    }

                    ctx.fillText(formatIndianNumber(value), bar.x, bar.y);
                });
            });
        }
    };
}

function showClientsDataLabels() {
    return {
        id: 'showClientsDataLabels_' + Math.random().toString(36).substring(2, 9),
        afterDatasetsDraw(chart) {
            const { ctx } = chart;
            const fontSize = Math.max(0.008 * Math.max(window.innerWidth, window.innerHeight), 11);

            chart.data.datasets.forEach((dataset, i) => {
                const meta = chart.getDatasetMeta(i);
                if (!chart.isDatasetVisible(i)) return;

                meta.data.forEach((bar, index) => {
                    const value = dataset.data[index];
                    if (value == null) return;

                    ctx.fillStyle = 'white';
                    ctx.font = `${fontSize}px sans-serif`;
                    ctx.textAlign = 'center';
                    ctx.fillText(formatIndianNumber(value), bar.x, bar.y);
                });
            });
        }
    };
}

function showProductLabels() {
    return {
        id: 'showProductLabels_' + Math.random().toString(36).substring(2, 9),
        afterDatasetsDraw(chart) {
            const { ctx } = chart;
            const fontSize = 14;

            chart.data.datasets.forEach((dataset, i) => {
                const meta = chart.getDatasetMeta(i);
                if (!chart.isDatasetVisible(i)) return;

                meta.data.forEach((bar) => {
                    ctx.save();
                    ctx.translate(bar.x, bar.y + 65);
                    ctx.rotate(-Math.PI / 2);
                    ctx.fillStyle = 'white';
                    ctx.font = `${fontSize}px sans-serif`;
                    ctx.textAlign = 'center';
                    ctx.textBaseline = 'middle';
                    ctx.fillText(dataset.label, 0, 0);
                    ctx.restore();
                });
            });
        }
    };
}

function showPiePercentages() {
    return {
        id: 'showPiePercentages_' + Math.random().toString(36).substring(2, 9),
        afterDatasetsDraw(chart) {
            const { ctx, data } = chart;
            const fontSize = Math.max(0.008 * Math.max(window.innerWidth, window.innerHeight), 11);
            const dataset = data.datasets[0];
            const meta = chart.getDatasetMeta(0);

            if (!chart.isDatasetVisible(0)) return;

            const total = dataset.data.reduce((sum, val) => sum + val, 0);
            ctx.save();
            ctx.fillStyle = 'white';
            ctx.font = `${fontSize}px sans-serif`;
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';

            meta.data.forEach((arc, index) => {
                const value = dataset.data[index];
                if (value == null) return;

                const percentage = ((value / total) * 100).toFixed(2) + ' %';
                const angle = (arc.startAngle + arc.endAngle) / 2;
                const radius = arc.outerRadius;
                const x = chart.width / 2 + Math.cos(angle) * (radius * 0.6);
                const y = chart.height / 2 + Math.sin(angle) * (radius * 0.6);

                ctx.fillText(percentage, x, y);
            });
        }
    };
}

function formatIndianNumber(x) {
    return x.toLocaleString('en-IN');
}

window.addEventListener('resize', () => {
    if (typeof myChart !== 'undefined') {
        myChart.update();
    }
});
