<?php

namespace App\Exceptions;

use App\Facades\WechatService;
use App\Http\Response\CatchError;
use App\Http\Response\ResponseJson;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class Handler extends ExceptionHandler
{
    use ResponseJson, CatchError;
    /**
     * A list of exception types with their corresponding custom log levels.
     *
     * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
     */
    protected $levels = [
        //
    ];

    /**
     * A list of the exception types that are not reported.
     *
     * @var array<int, class-string<\Throwable>>
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            DB::rollBack();
            $this->getError($e);
            $msg = $e->getMessage();
//            Log::info($msg);
            if (strstr($msg, 'access_token is invalid or not latest')) {
                Log::info("刷新access_token");
                $this->refreshToken();
            }
            return false;
        });
        $this->renderable(function (AuthenticationException $e) {
            $msg = $e->getMessage();
            Log::error('AuthenticationException msg:'.$msg);
            return $this->authFail();
        });
        $this->renderable(function (Throwable $e) {
            if (method_exists($e, 'getStatusCode')) {
                if ($e->getStatusCode() == 405) {
                    return $this->failure("没有对应的路由方法");
                }elseif ($e->getStatusCode() == 404) {
                    return $this->failure("没有对应的路由");
                }
            }
            $msg = $e->getMessage();
            if ($e->getCode() == 5000) {
                return $this->failure($e->getMessage());
            }
            $this->getError($e);
            Log::error('Throwable msg:'.$msg);
            if ($msg == 'Route [login] not defined.') return $this->authFail();
            if (config('app.env') != 'production') return;
            $msg = trans('auth.server_error')??'服务器休息,请稍后重试';
            return $this->failure($msg);
        });
    }
    public function refreshToken()
    {
        $app = WechatService::app();
        $accessToken = $app->getAccessToken();
        $token = $accessToken->refresh();
//        Log::info($token);
        $app = WechatService::officialApp();
        $accessToken = $app->getAccessToken();
        $token = $accessToken->refresh();
//        Log::info($token);
    }

}
