<?php

namespace classes;

class RDTResponse
{
    /**
     * Outputs a success message and terminates the script.
     *
     * @param string $message The message to be displayed.
     * @return void
     */
    public static function ok(string $message): void
    {
        echo "OK-  {$message}";
        exit;
    }

    /**
     * Terminates the script execution and outputs a formatted failure response.
     *
     * @param string $message A message that specifies the reason for failure.
     * @param string $data Additional data to be included in the failure response (optional).
     * @param string $skipToField Specifies a field to skip to, appending additional information to the response (optional).
     *
     * @return void This method does not return a value; it terminates the script execution.
     */
    public static function fail(string $message): void
    {
        echo "FAIL- {$message}";
        exit;
    }

    /**
     * Outputs a data message and terminates the script.
     *
     * @param string $data
     * @param string $rdtMessage The rdt message to display.
     * @param string $skipToField
     * @param bool $automaticProcess
     * @return void
     */
    public static function data(string $data, string $rdtMessage = '', string $skipToField = '', bool $automaticProcess = false): void
    {
        if (!empty($skipToField))
        {
            $skipStr = $automaticProcess ? "::skipap" : "::skip";
            $data = $data . $skipStr . $skipToField;
        }

        if (empty($rdtMessage))
        {
            echo "DATA:$data";
        }
        else
        {
            echo "DATA:rdtMessage:$rdtMessage,$data";
        }

        exit;
    }

    /**
     * Outputs a success message with optional data and terminates the script.
     *
     * @param string $message The success message to display.
     * @param string $data Optional additional data to include in the output.
     * @param string $skipToField Optional identifier to append to the data.
     * @return void
     */
    public static function okMessage(string $message, string $data = '', string $skipToField = ''): void
    {
        $response = "OKMS- $message";

        if (!empty($skipToField))
        {
            $data = $data . ':I:skip' . $skipToField;
        }

        if (!empty($data))
        {
            $response .= ",DATA:$data";
        }

        echo $response;
        exit;
    }

    /**
     * Outputs a success completion message, optionally including additional data, and terminates the script.
     *
     * @param string $message The completion message to display.
     * @param string $data Optional additional data to include in the message.
     */
    public static function okComplete(string $message, string $data = ''): void
    {
        $response = "OKMS-Complete - $message";

        if (!empty($data))
        {
            $response .= ",DATA:$data";
        }

        echo $response;
        exit;
    }

    /**
     * Processes and outputs a dropdown array in JSON format, with optional automatic processing.
     *
     *  Dropdown array example:
     *  $dropdown[] = [
     *       'value' => "allocationId:{$row['pi_header_id']},partNumber:{$row['part_number']}",
     *       'text' => $row['part_number']
     *   ];
     *
     * @param array $dropdownArray The array containing the dropdown data to be processed.
     * @param bool $automaticProcess Whether to automatically process the dropdown array by modifying its values.
     */
    public static function dropdown(array $dropdownArray, bool $automaticProcess = false): void
    {
        $dropdownArrayUpdated = [];
        if ($automaticProcess && !empty($dropdownArray))
        {
            foreach ($dropdownArray as $row)
            {
                $dropdownArrayUpdated[] = [
                    'value' => str_replace(':', 'skipap:', $row['value']),
                    'text' => $row['text'],
                    'colour' => $row['colour'] ?? ''
                ];
            }
            $dropdownArray = $dropdownArrayUpdated;
        }

        echo 'DROP-' . json_encode($dropdownArray);
        exit;
    }

    /**
     * Constructs and outputs a datalist specific to the target field.
     *
     * @param string $targetField The name of the field for which the datalist is being generated.
     * @param array $dropdownArray An array of dropdown options to be included in the datalist output.
     * @return void This method does not return a value but outputs the formatted datalist and terminates the script.
     */
    public static function list(string $targetField, array $dropdownArray): void
    {
        echo 'LIST:' . $targetField . ':' . json_encode($dropdownArray);
        exit;
    }

    /**
     * Displays a confirmation panel and prepares a response for the frontend.
     *
     * @param string $message The confirmation message to display.
     * @param string $yesAction The action to execute when "YES" is selected (optional).
     * @param string $noAction The action to execute when "NO" is selected (optional).
     * @return void
     */
    public static function confirmPanel(string $message, string $yesAction = '', string $noAction = ''): void
    {
        $stringResponse = "$message:YES:NO";

        // Adds action for YES answer if provided
        if (!empty($yesAction))
        {
            $stringResponse .= ":$yesAction";
        }

        // Adds action for NO answer if provided
        if (!empty($noAction))
        {
            $stringResponse .= ";$noAction";
        }

        // Prepares response for frontend
        $stringResponse = str_replace(':', '|COLON|', $stringResponse);
        $stringResponse = str_replace(',', '|COMMA|', $stringResponse);
        echo 'OKCC-' . $stringResponse;
        exit;
    }

    /**
     * Outputs a table reference and JSON-encoded data array, then terminates the script.
     *
     * @param string $reference The reference for the table.
     * @param array $dataArray The data array to be JSON-encoded and displayed.
     * @return void
     */
    public static function table(string $reference, array $dataArray): void
    {
        echo "TABLE:$reference:" . json_encode($dataArray);
        exit;
    }

    /**
     * Outputs a text message and terminates the script.
     *
     * @param string $message The text message to display.
     * @return void
     */
    public static function text(string $message): void
    {
        echo "TEXT-$message";
        exit;
    }

    /**
     * Outputs a transaction change message and terminates the script.
     *
     * @param string $trandefFileName The name of the transaction definition file to process.
     * @return void
     */
    public static function changeTransaction(string $trandefFileName): void
    {
        $trandefFileName = strtoupper($trandefFileName);
        echo "CHGTRAN:$trandefFileName?";
        exit;
    }
}
