<?php


namespace App\Libs;

use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Response;

trait ApiResponse
{
    /**
     * @var int
     */
    protected $statusCode = FoundationResponse::HTTP_OK;

    /**
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * @param $statusCode
     * @return $this
     */
    public function setStatusCode($statusCode, $httpCode = null)
    {
        $httpCode         = $httpCode ?? $statusCode;
        $this->statusCode = $statusCode;
        return $this;
    }

    /**
     * @param       $data
     * @param array $header
     * @return mixed
     */
    public function respond($data, $header = [])
    {
        return Response::json($data, $this->getStatusCode(), $header, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @param       $status
     * @param array $data
     * @param null  $code
     * @return mixed
     */
    public function status($status, $data, $code = null)
    {
        if ($code) {
            $this->setStatusCode($code);
        }
        $res = [
            'msg'  => $status,
            'code' => 0,
            'data' => $data
        ];

        return $this->respond($res);
    }

    /**
     * @param        $message
     * @param int    $code
     * @param string $status
     * @return mixed
     */
    /*
     * 格式
     * data:
     *  code:422
     *  message:xxx
     *  status:'error'
     */
    public function failed($message, $code = FoundationResponse::HTTP_BAD_REQUEST, $status = 'error')
    {

        return $this->setStatusCode($code)->message($message, $status);
    }

    /**
     * @param        $message
     * @param string $status
     * @return mixed
     */
    public function message($message, $status = 'success')
    {

        return $this->status($status, [
            'message' => $message
        ]);
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function internalError($message = 'Internal Error!')
    {

        return $this->failed($message, FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function created($message = 'created')
    {
        return $this->setStatusCode(FoundationResponse::HTTP_CREATED)
            ->message($message);

    }

    /**
     * @param $data
     * @param $callFunc
     * @return mixed
     */
    public function success($data, $callFunc = [])
    {
        if ($callFunc) {
            return $this->status('', call_user_func($callFunc, $data));
        }

        return $this->status('', $data);
    }

    /**
     * @param $data
     * @param $callFunc
     * @return mixed
     */
    public function paginate($data, $callFunc = [])
    {
        $result = [
            'meta' => getMeta($data),
            'list' => $data->items()
        ];

        if ($callFunc) {
            $result['list'] = call_user_func($callFunc, $data->items());
        }

        return $this->status('', $result);
    }

    /**
     * @param $errorData
     * @param $data
     * @return mixed
     */
    public function error($errorData, $data = '')
    {
        // 分解错误码、错误信息
        $arr  = explode(':', (string)$errorData);
        $code = (int)$arr[0];
        $msg  = (string)$arr[1];

        $res = [
            'msg'  => $msg,
            'code' => $code,
            'data' => $data
        ];

        return $this->respond($res);
    }

    public function appResponse($res)
    {
        return $this->respond($res);
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function notFond($message = 'Not Fond!')
    {
        return $this->failed($message, Foundationresponse::HTTP_NOT_FOUND);
    }
}