<?php

namespace Khofmann\Response;

use Pecee\SimpleRouter\SimpleRouter;
use Pecee\Http\Response as PResponse;

class Response
{
  private function __construct()
  {
  }

  public static function response(): PResponse
  {
    return SimpleRouter::response();
  }

  public static function json($value, int $options = 0, int $dept = 512): void
  {
    if (is_bool($value)) {
      Response::response()->header('Content-Type: application/json; charset=utf-8');
      echo json_encode($value, $options, $dept);
      exit(0);
    }
    SimpleRouter::response()->json($value, $options, $dept);
  }

  public static function apiError(string $value, int $code): void
  {
    Response::response()->header('Content-Type: application/json; charset=utf-8')->httpCode($code);
    echo $value;
    exit(0);
  }

  public static function redirect(string $url, ?int $code = null): void
  {
    if ($code !== null) {
      Response::response()->httpCode($code);
    }

    Response::response()->redirect($url);
  }
}
