<?php

// Documentation
// https://www.chartjs.org/docs/2.6.0/

class chart
{

    public static function printCanvas(array $_config)
    {
        $id = $_config["id"];
        $width = (isset($_config["width"])) ? $_config["width"] : 600;
        $height = (isset($_config["height"])) ? $_config["height"] : 400;

        echo '<canvas id="' . $id . '" width="' . $width . '" height="' . $height . '" style="display: block; width: ' . $width . 'px; height: ' . $height . 'px;" class="chartjs-render-monitor"></canvas>';

        switch ($_config["charts"]) {
            case "doughnut":
                echo self::javascriptDoughnut($_config);
                break;
            case "bar":
                echo self::javascriptBar($_config);
                break;
            case "line":
                echo self::javascriptLine($_config);
                break;
            default:
                break;
        }
    }

    private static function constructData(array $_data)
    {
        $label = $nb = $backgroundColor = $borderColor = "";
        $opacity = 0.5;
        $nbData = count($_data);

        $data["backgroundColor"] = array(
            "rgba(255, 99, 132, " . $opacity . ")", // Red
            "rgba(54, 162, 235, " . $opacity . ")", // Blue
            "rgba(255, 206, 86, " . $opacity . ")", // Yellow
            "rgba(75, 192, 192, " . $opacity . ")", // Green,
            "rgba(153, 102, 255, " . $opacity . ")", // Purple
            "rgba(255, 159, 64, " . $opacity . ")" // Orange
        );

        $data["borderColor"] = array(
            "rgba(255,99,132,1)", // Red
            "rgba(54, 162, 235, 1)", // Blue
            "rgba(255, 206, 86, 1)", // Yellow
            "rgba(75, 192, 192, 1)", // Green
            "rgba(153, 102, 255, 1)", // Purple
            "rgba(255, 159, 64, 1)" // Orange
        );

        for ($i = 0; $i < $nbData; $i++) {
            $backgroundColor .= '"' . $data["backgroundColor"][$i] . '"';
            if ($i != $nbData) {
                $backgroundColor .= ", ";
            }
        }

        for ($i = 0; $i < $nbData; $i++) {
            $borderColor .= '"' . $data["borderColor"][$i] . '"';
            if ($i != $nbData) {
                $borderColor .= ", ";
            }
        }

        for ($i = 0; $i < $nbData; $i++) {
            $label .= '"' . $_data[$i]["label"] . '"';
            if ($i != $nbData) {
                $label .= ", ";
            }
        }

        for ($i = 0; $i < $nbData; $i++) {
            $nb .= '"' . $_data[$i]["nb"] . '"';
            if ($i != $nbData) {
                $nb .= ", ";
            }
        }

        return array("label" => $label, "nb" => $nb, "backgroundColor" => $backgroundColor, "borderColor" => $borderColor);
    }

    private static function javascriptDoughnut(array $_config)
    {

        $sort = self::constructData($_config["data"]);

        return '<script>
                var ctx = document.getElementById("' . $_config["id"] . '");
                var myChart = new Chart(ctx, {
                    type: "doughnut",
                    data: {
                        labels: [' . $sort["label"] . '],
                        datasets: [{
                            label: "' . $_config["label"] . '",
                            data: [' . $sort["nb"] . '],
                            backgroundColor: [' . $sort["backgroundColor"] . '],
                            borderColor: [' . $sort["borderColor"] . '],
                            borderWidth: 1
                        }]
                    }
                });
            </script>';
    }

    private static function javascriptBar(array $_config)
    {

        $sort = self::constructData($_config["data"]);

        return '<script>
                var ctx = document.getElementById("' . $_config["id"] . '");
                var myChart = new Chart(ctx, {
                    type: "bar",
                    data: {
                        labels: [' . $sort["label"] . '],
                        datasets: [{
                            label: "' . $_config["label"] . '",
                            data: [' . $sort["nb"] . '],
                            backgroundColor: [' . $sort["backgroundColor"] . '],
                            borderColor: [' . $sort["borderColor"] . '],
                            borderWidth: 1
                        }]
                    }
                });
            </script>';
    }

    private static function constructDataLine(array $_data)
    {
        $return["labels"] = $return["data"] = NULL;
        foreach ($_data[0] as $key => $value) {
            $return["labels"] .= '"' . $key . '", ';
            $return["data"] .= $value . ', ';
        }
        return $return;
    }

    private static function javascriptLine(array $_config)
    {

        $sort = self::constructDataLine($_config["data"]);

        return '<script>
                var ctx = document.getElementById("' . $_config["id"] . '");
                var myChart = new Chart(ctx, {
                    type: "line",
                    data: {
                            "labels":[' . $sort["labels"] . '],
                            datasets: [{
                                "label":"' . $_config["label"] . '", 
                                "data":[' . $sort["data"] . '], 
                                "fill":false, 
                                "borderColor":"rgb(75, 192, 192)", 
                                "lineTension":0.1
                            }],
                            "options":{}
                    }
                });
            </script>';
    }
}
