<?php

namespace ProxiumWXfrm\Http;

class Response
{
    protected int $status=200;
    protected array $headers=[];
    protected ?string $content=null;

    public function status():int {
        return $this->status;
    }

    public function setStatus(int $status) : self {
        $this->status=$status;
        return $this;
    }

    public function headers():array {
        return $this->headers;
    }

    public function setHeader(string $header,string $value):self {
        $this->headers[strtolower($header)]=$value;
        return $this;
    }

    public function setContentType(string $value):self {
        $this->setHeader("Content-Type",$value);
        return $this;
    }

    public function removeHeader(string $header){
        unset($this->headers[strtolower($header)]);
    }

    public function content():?string {
        return $this->content;
    }

    public function setContent(string $content):self {
        $this->content=$content;
        return $this;
    }

    public function prepare(){
        // PHP sends Content-Type header by default, but it has to be removed
        // if the response has no content. Content-Type header can't be removed
        // unless it is set to some value before, this is way the first line and
        // after the second
        header("Content-Type: None");
        header_remove("Content-Type");

        if (is_null($this->content)){
            $this->removeHeader("Content-Type");
            $this->removeHeader("Content-Length");
        } else {
            $this->setHeader("Content-Length",strlen($this->content));
        }
    }

    public static function json(array $data):self {
        return (new self)
        ->setContentType("application/json")
        ->setContent(json_encode($data));
    }

    public static function text(string $text):self {
        return (new self)
        ->setContentType("text/plain")
        ->setContent($text);
    }

    public static function redirect(string $uri):self {
        return (new self)
        ->setStatus(302)
        ->setHeader("Location",$uri);
    }

    public static function jsonString(string $cadena):self {
        return (new self)
        ->setContentType("application/json")
        ->setContent(json_decode($cadena));
    }

    
    public static function downloadBinary(array $data):self {
        //$archivo=array($archivoNombre,$archivoTamanyo,$archivoContenido);
        return (new self)
        ->setContentType("application/force-download")
        ->setHeader("Content-Transfer-Encoding","binary")
        ->setHeader("Content-Disposition","attachment; filename=".$data[0])
        ->setHeader("Content-Length",$data[1])
        ->setContent($data[2]);

    }
    

}
