<?php

namespace addons\shopro;

use think\Addons;
use app\common\library\Menu;
use app\admin\model\AuthRule;
use addons\shopro\library\Hook;

/**
 * Shopro插件 v3.0.0
 */
class Shopro extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        // 创建菜单
        $menu = self::getMenu();
        Menu::create($menu['new']);

        // 插入 {$VUE_ELEMENTPLUS} 标识
        self::addScript();

        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        // 删除菜单
        Menu::delete('shopro');

        // 移除 {$VUE_ELEMENTPLUS} 标识
        self::delScript();

        return true;
    }

    /**
     * 插件启用方法
     */
    public function enable()
    {
        // 启用菜单
        Menu::enable('shopro');

        // 插入 {$VUE_ELEMENTPLUS} 标识
        self::addScript();

        return true;
    }

    /**
     * 插件更新方法
     */
    public function upgrade()
    {
        // 更新菜单
        $menu = self::getMenu();
        Menu::upgrade('shopro', $menu['new']);

        // 插入 {$VUE_ELEMENTPLUS} 标识
        self::addScript();

        return true;
    }

    /**
     * 插件禁用方法
     */
    public function disable()
    {
        // 禁用菜单
        Menu::disable('shopro');

        // 移除 {$VUE_ELEMENTPLUS} 标识
        self::delScript();

        return true;
    }


    /**
     * 应用初始化
     */
    public function appInit()
    {
        // 公共方法
        require_once __DIR__ . '/helper/helper.php';

        // 覆盖队列 redis 参数
        $queue = \think\Config::get('queue');
        $redis = \think\Config::get('redis');
        if ($queue && strtolower($queue['connector']) == 'redis' && $redis) {
            $queue = array_merge($redis, $queue);       // queue.php 中的配置,覆盖 redis.php 中的配置
            \think\Config::set('queue', $queue);
        }

        // database 增加断线重连参数
        $database = \think\Config::get('database');
        $database['break_reconnect'] = true;        // 断线重连
        \think\Config::set('database', $database);

        // 设置 vue element script
        \think\View::share('VUE_ELEMENTPLUS', '
            <link rel="stylesheet" href="/assets/addons/shopro/libs/element-plus/index.css">
            <script src="/assets/addons/shopro/libs/vue.js"></script>
            <script src="/assets/addons/shopro/libs/element-plus/index.js"></script>
            <script src="/assets/addons/shopro/libs/element-plus/zh-cn.js"></script>
            <script src="/assets/addons/shopro/libs/element-plus/icons-vue.js"></script>
            <script src="/assets/addons/shopro/libs/sortable.js"></script>
            <script src="/assets/addons/shopro/libs/vuedraggable.js"></script>
            <script src="/assets/addons/shopro/libs/socket.io.js"></script>
            <link rel="stylesheet" href="//at.alicdn.com/t/c/font_2385137_b8qygb2jne.css">
        ');

        // 全局注册行为事件
        Hook::register();

        if (request()->isCli()) {
            \think\Console::addDefaultCommands([
                'addons\shopro\console\ShoproChat',
                'addons\shopro\console\ShoproHelp'
            ]);
        }
    }

    private static function getMenu()
    {
        $newMenu = [];
        $config_file = ADDON_PATH . "shopro" . DS . 'config' . DS . "menu.php";
        if (is_file($config_file)) {
            $newMenu = include $config_file;
        }
        $oldMenu = AuthRule::where('name', 'like', "shopro%")->select();
        $oldMenu = array_column($oldMenu, null, 'name');
        return ['new' => $newMenu, 'old' => $oldMenu];
    }


    private static function addScript()
    {
        $script_path = APP_PATH . 'admin/view/common/script.html';
        $script = file_get_contents($script_path);
        if (strpos($script, '{$VUE_ELEMENTPLUS}') === false) {
            $script = '{$VUE_ELEMENTPLUS}' . $script;
            file_put_contents($script_path, $script);
        }
    }


    private static function delScript()
    {
        $script_path = APP_PATH . 'admin/view/common/script.html';
        $script = file_get_contents($script_path);
        if (strpos($script, '{$VUE_ELEMENTPLUS}') !== false) {
            $script = str_replace('{$VUE_ELEMENTPLUS}', '', $script);
            file_put_contents($script_path, $script);
        }
    }
}