<?php

namespace Lib;

class App {

    /**
     * @var App
     */
    public static App $instance;

    /**
     * 当前日期
     * @var string
     */
    private string $date = '';

    /**
     * 当前时间
     * @var string
     */
    private string $dateTime = '';

    /**
     * @var Route
     */
    private Route $route;

    /**
     * 请求的参数
     * @var array
     */
    private array $request = [];

    /**
     * 输出到前端的数据
     * @var array
     */
    private array $data = [];

    public function __construct()
    {
        header("Content-Type:application/json;Charset=utf8;");

        $this->date = date('Y-m-d');
        $this->dateTime = date('Y-m-d H:i:s');


        // 路由
        $this->route = new Route();

        // 请求参数 TODO::不允许其他类型的请求参数
        $this->request = my_filter($_POST);


    }


    /**
     * @return App
     * @author:dc
     * @time 2023/2/13 10:37
     */
    public static function instance(){
        if(empty(static::$instance)){
            static::$instance = new App();
        }
        return static::$instance;
    }


    /**
     * 开始运行
     * @author:dc
     * @time 2023/2/13 11:15
     */
    public static function run() {
        $app = self::instance();
        try {
            if ($_SERVER['REQUEST_METHOD'] != 'POST'){
                $app->e('need_post_request');
            }

            // 取到路由 控制器
            $route = $app->route->get(trim($_SERVER['REQUEST_URI']));
            if(!$route){
                $app->e('route_not_found');
            }
            list($class,$action) = $route;
            $controller = new $class();
            $data = $controller->{$action}();
            if($data){
                $app->data = $data;
            }
            // end 控制器


        }catch (\Throwable $exception){

            if(!($exception instanceof Err)){
                // 记录日志
                $filename = LOG_PATH.'/'.$app->nowDate().'.log';
                logs(
                    $exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
                    $filename
                );
            }
        }

    }

    /**
     * 请求的参数
     * @param string $name
     * @param null $default
     * @return array|false|float|int|mixed|null
     * @author:dc
     * @time 2023/2/13 12:00
     */
    public function request($name='*',$default=null){
        if($name == '*'){
            return $this->request;
        }
        if (is_string($name)){
            return $this->request[$name]??$default;
        }
        $data = [];
        foreach ($this->request as $key=>$value){
            if(in_array($key,$name)){
                $data[$key] = $value;
            }
        }
        return $data;
    }


    /**
     * @return string
     * @author:dc
     * @time 2023/2/13 11:17
     */
    public function nowDate():string {
        return $this->date;
    }

    /**
     * @return string
     * @author:dc
     * @time 2023/2/13 11:18
     */
    public function nowDateTime():string{
        return $this->dateTime;
    }

    /**
     * 错误
     * @param $message
     * @param int $status
     * @throws Err
     * @author:dc
     * @time 2023/2/13 10:57
     */
    public function e($message,$status=400){
        $this->data['message'] = __($message);
        $this->data['status'] = $status;
        throw new Err($message,500);
    }

    /**
     * 成功消息
     * @param $data
     * @param string $message
     * @throws Err
     * @author:dc
     * @time 2023/2/13 11:03
     */
    public function _json($data,$message=''){
        $this->data['data'] = $data;
        $this->data['message'] = __($message);
        throw new Err($message,200);
    }


    /**
     * 结束
     * @author:dc
     * @time 2023/2/13 10:54
     */
    public static function end()
    {
        // 这里可以做其他事

        /**
         * 这里写
         */

        // end code



        echo json_encode(self::instance()->data,JSON_UNESCAPED_UNICODE);
    }




}