<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();

class DtaResult {

    public $packagename;
    public $classname;
    public $name;

    /**
     * State is defined like below
     *
     *  0 UNKNOWN
     *  1 SUCCESS
     *  2 FAILURE
     *  3 COMPILATIONERROR
     */
    public $state;

    public $failuretype;
    public $failurereason;
    public $stacktrace;

    public $columnnumber;
    public $linenumber;
    public $position;

    /**
     * @return name of state like defined
     */
    public static function getStateName(int $state): string {
        if ($state == 1) {
            return "success";
        } else if ($state == 2) {
            return "failed";
        } else if ($state == 3) {
            return "compilation error";
        } else {
            return "unknown";
        }
    }
}

class DtaResultSummary {

    public $timestamp;
    public $globalstacktrace;
    public $successfultestcompetencies;
    public $overalltestcompetencies;
    public $results;

    /**
     * @param string $jsonString jsonString containing DtaResultSummary
     * @return DtaResultSummary
     */
    public static function decodejson($jsonstring): DtaResultSummary {
        $response = json_decode($jsonstring);

        $summary = new DtaResultSummary();
        $summary->timestamp = $response->timestamp;
        $summary->globalstacktrace = $response->globalstacktrace;

        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile;
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile;

        $summary->results = self::decodejsonresultarray($response->results);

        return $summary;
    }

    /**
     * @param array $jsonArray decoded json array of results array
     * @return array of DtaResult
     */
    private static function decodejsonresultarray($jsonarray): array {
        $ret = array();
        foreach ($jsonarray as $entry) {
            $value = new DtaResult();
            $value->packagename = $entry->packageName;
            $value->classname = $entry->className;
            $value->name = $entry->name;

            $value->state = $entry->state;

            $value->failuretype = $entry->failureType;
            $value->failurereason = $entry->failureReason;
            $value->stacktrace = $entry->stacktrace;

            $value->columnnumber = $entry->columnNumber;
            $value->linenumber = $entry->lineNumber;
            $value->position = $entry->position;

            $ret[] = $value;
        }
        return $ret;
    }

    public function resultcount(): int {
        return count($this->results);
    }

    /**
     * @param int $state state ordinal number
     * @return int count of occurences provided state has
     */
    public function stateoccurencecount(int $state): int {
        $num = 0;
        foreach ($this->results as $r) {
            if ($r->state == $state) {
                $num++;
            }
        }
        return $num;
    }

    public function compilationerrorcount(): int {
        return $this->stateoccurencecount(3);
    }

    public function failedcount(): int {
        return $this->stateoccurencecount(2);
    }

    public function successfulcount(): int {
        return $this->stateoccurencecount(1);
    }

    public function unknowncount(): int {
        return $this->stateoccurencecount(0);
    }

}