<?php

namespace plugin\control\app\control\controller;

use app\Basic;
use app\expose\build\config\Action;
use app\expose\build\config\Web;
use app\expose\enum\Action as EnumAction;
use app\expose\enum\Filesystem;
use app\expose\enum\State;
use app\expose\helper\Captcha;
use app\expose\helper\Config;
use plugin\control\expose\helper\Control;
use app\expose\helper\Vcode;
use loong\oauth\facade\Auth;
use plugin\control\app\model\PluginChannelsRole;
use support\Request;

class PublicController extends Basic
{
    /**
     * 不需要登录的方法
     * @var string[]
     */
    protected $notNeedLogin = ['config', 'menus', 'vcode', 'outLogin', 'unlock'];
    protected $notNeedAuth = ['config', 'menus', 'vcode', 'outLogin'];
    public function config(Request $request)
    {
        $lang = $request->lang;
        $domain = 'control';
        $config = new Config('basic', '');
        $config = new Web($config->toArray());
        $captcha_config = new Config('captcha', '');
        $config->useLogin([
            'url' => 'Login/login',
            'title' => trans('Login Title', [], $domain, $lang),
            'captcha' => $captcha_config->state,
            'link_text' => trans('Login Link Text', [], $domain, $lang),
            'user_agreement_config' => [
                'title' => trans('Login User Agreement Title', [], $domain, $lang),
                'label' => trans('Login User Agreement Label', [], $domain, $lang)
            ]
        ]);
        $config->useVcode([
            'url' => 'Login/vcode',
            'title' => trans('Vcode Login', [], $domain, $lang)
        ]);
        $config->useRegister([
            'url' => 'Login/register',
            'title' => trans('Register Title', ['%web_name%' => $config['web_name']], $domain, $lang),
            'link_text' => trans('Register Link Text', [], $domain, $lang),
            'user_agreement_config' => [
                'title' => trans('Register User Agreement Title', [], $domain, $lang),
                'label' => trans('Register User Agreement Label', [], $domain, $lang)
            ]
        ]);
        $wechat_official_account_config = new Config('wechat_official_account', '');
        if ($wechat_official_account_config->state && $wechat_official_account_config->message_state) {
            $config->useQrcodeLogin([
                'url' => 'Login/qrcode',
                'check' => 'Login/checkQrcode',
                'title' => trans('Qrcode Login', [], $domain, $lang)
            ]);
        }
        $config->useApis([
            'userinfo' => '/app/control/control/User/getInfo',
            'lock' => '/app/control/control/User/lock',
            'unlock' => '/app/control/control/Public/unlock',
            'menus' => '/app/control/control/Public/menus',
            'vcode' => '/app/control/control/Public/vcode',
            'outLogin' => '/app/control/control/Public/outLogin',
        ]);
        $toolbar = new Action();
        $toolbar->add(EnumAction::LOCK['value'], [
            'icon' => 'Lock',
            'tips' => trans('toolbar Lock', [], $domain, $lang)
        ]);
        $toolbar->add(EnumAction::SEARCH['value'], [
            'icon' => 'Search',
            'tips' => trans('toolbar Search', [], $domain, $lang)
        ]);
        $toolbar->add(EnumAction::NOTIFICATION['value'], [
            'icon' => 'Notification',
            'tips' => trans('toolbar Notification', [], $domain, $lang)
        ]);
        $toolbar->add(EnumAction::FULL_SCREEN['value'], [
            'icon' => 'FullScreen',
            'tips' => trans('toolbar FullScreen', [], $domain, $lang)
        ]);
        $config->useToolbar($toolbar->toArray());
        $userDropdownMenu = new Action();
        $userDropdownMenu->add(EnumAction::DIALOG['value'], [
            'path' => '/app/control/control/User/update',
            'label' => trans('userDropdownMenu updateSelf', [], $domain, $lang),
            'icon' => 'User',
            'props' => [
                'title' => trans('userDropdownMenu updateSelf', [], $domain, $lang)
            ]
        ]);
        $config->useUserDropdownMenu($userDropdownMenu->toArray());
        $config->storage = Filesystem::getOptions(function ($item) {
            return !in_array($item['value'], [Filesystem::PUBLIC['value'], Filesystem::LOCAL['value']]);
        });
        $pluginConfig = glob(base_path("plugin/*/api/{$request->app}/PublicController.php"));
        foreach ($pluginConfig as $path) {
            $plugin_name = basename(dirname(dirname(dirname($path))));
            if ($plugin_name == 'control') {
                continue;
            }
            $class = 'plugin\\' . $plugin_name . "\\api\\{$request->app}\\PublicController";
            if (!class_exists($class)) {
                continue;
            }
            $plugin = new $class;
            if (method_exists($plugin, 'config')) {
                $plugin->config($config);
            }
        }
        $updateVersionContent = file_get_contents(base_path('update/VERSION'));
        $updateVersionArr = explode("\n", $updateVersionContent);
        $config->version_name=$updateVersionArr[1];
        $config->version=$updateVersionArr[0];
        return $this->resData($config);
    }
    public function menus(Request $request)
    {
        $Control = new \plugin\control\api\Control;
        $menus = new Control($Control);
        return $this->resData($menus);
    }


    // private function filterMenu(array $menus, array $allowPaths): array
    // {
    //     $result = [];
    //     foreach ($menus as $menu) {
    //         // 递归处理 children
    //         if (!empty($menu['children'])) {
    //             $menu['children'] = $this->filterMenu($menu['children'], $allowPaths);
    //         }
    //         // 是否保留当前节点
    //         $keep = in_array($menu['path'], $allowPaths, true)
    //             || !empty($menu['children']);

    //         if ($keep) {
    //             $result[] = $menu;
    //         }
    //     }
    //     return $result;
    // }



    public function outLogin(Request $request)
    {
        $token = $request->header('Authorization');
        if ($token) {
            try {
                Auth::setPrefix('CONTROL')->delete($token);
            } catch (\Throwable $th) {
            }
        }
        return $this->success(trans('Logout Success', [], $request->app, $request->lang));
    }
    public function vcode(Request $request)
    {
        $username = $request->post('username');
        $token = $request->post('token');
        $captcha = $request->post('captcha');
        if (!Captcha::check($captcha, $token)) {
            return $this->fail(trans('Captcha Incorrect', [], $request->app, $request->lang));
        }
        $scene = $request->post('scene');
        if (!$scene) {
            return $this->fail(trans('Scene Cannot Be Empty', [], $request->app, $request->lang));
        }
        try {
            Vcode::send($username, $scene, $token);
            return $this->success(trans('Vcode Send Success', [], $request->app, $request->lang));
        } catch (\Throwable $th) {
            return $this->exception($th);
        }
    }
    public function unlock(Request $request)
    {
        $password = $request->post('password');
        try {
            $token = $request->header('Authorization');
            Auth::setPrefix('CONTROL')->unlock($token, $password);
            return $this->success(trans('Unlock Success', [], $request->app, $request->lang));
        } catch (\Throwable $th) {
            return $this->exception($th);
        }
    }
}
