<?php

namespace core_next;

class ShortCode
{
    static function init()
    {
        self::addShortCode('title-plane', 'titlePlane');
        self::addShortCode('start-plane', 'startPlane');
        self::addShortCode('icon-url', 'iconUrl');
        self::addShortCode('zd-plane', 'zdPlane');
        self::addShortCode('loginshow', 'loginShow');
        self::addShortCode('clickshow', 'clickShow');
        self::addShortCode('c-alert', 'cAlert');
        self::addShortCode('c-downbtn', 'cDownbtn');
        self::addShortCode('pwdshow', 'pwdShow');
        self::addShortCode('wxshow', 'wxshow');
        self::addShortCode('reply', 'replyShow');
        self::addShortCode('dh', 'dh');
        self::addShortCode('postcard', 'postcard');
        self::addShortCode('instance_card', 'instanceCard');
        self::addShortCode('cn_btn', 'cnBtn');
        self::addShortCode('selectbox', 'selectBox');


    }

    static function selectBox($atts, $content = '')
    {
        $parameter = shortcode_atts([
            'type' => ''
        ], $atts);
        if ($parameter['type'] == 1) {
            return '<span class="selectbox selectbox-yes"><img class="not-light-box not-shadow" src="' . Config::$img_url . '/selectbox.svg"></span>';
        } else {
            return '<span class="selectbox selectbox-not"><img class="not-light-box not-shadow" src="' . Config::$img_url . '/selectbox-not.svg"></span>';
        }
    }

    static function addShortCode($tag, $fun)
    {
        add_shortcode($tag, [static::class, $fun]);
    }


    static function replyShow($atts, $content = null)
    {
        $is_reply = false;
        $post_id = get_the_ID();
        $mail = '';
        if (WordPress::isLogin()) {
            $mail = WordPress::getUserMail();
        } else {
            if (isset($_COOKIE['core_comment_mail'])) {
                $mail = $_COOKIE['core_comment_mail'];
            }
        }

        if (WordPress::isAdmin()) {
            $is_reply = true;
        } else if (WordPress::postCommentHaveMail($mail, $post_id) && $mail !== '') {
            $is_reply = true;
        }

        if ($is_reply) {
            $content = do_shortcode($content);
            $reply_content = '<div class="core-reply-show"><div class="reply-notice">以下为回复可见内容</div>' . $content . '</div>';
        } else {
            $reply_content = '<div class="core-reply-show core-need-reply">此处内容需要<b><span style="cursor: pointer" class="core-need-reply-click">回复</b></span>后并刷新才能查看</div>';
        }
        return $reply_content;
    }

    static function wxshow($atts, $content = null)
    {
        global $core_next_set;

        $data['content'] = do_shortcode($content);
        if (WordPress::isAdmin()) {
            return Template::getTemplateHtml('wxshow-show', $data);
        }

        if (isset($_COOKIE['wx_show_pwd'])) {
            $cookie_pwd = $_COOKIE['wx_show_pwd'];
            if ($cookie_pwd === $core_next_set['wxshow_pwd']) {
                return Template::getTemplateHtml('wxshow-show', $data);
            }
        }
        if (isset($_POST['wx_show_pwd'])) {
            $pwd = $_POST['wx_show_pwd'];
            if ($pwd === $core_next_set['wxshow_pwd']) {
                //$expire = time() + ();
                $end_time = $core_next_set['wxshow_cookie_day'] * 24 * 60 * 60;
                $html = '<script>document.cookie = "wx_show_pwd=' . $pwd . '; expires=' . gmdate('D, d M Y H:i:s \G\M\T', time() + $end_time) . '";</script>';
                return Template::getTemplateHtml('wxshow-show', $data) . $html;
            }
        }
        $data['wxshow_description'] = $core_next_set['wxshow_description'];
        $data['wxshow_qrcode'] = $core_next_set['wxshow_qrcode'];
        return Template::getTemplateHtml('wxshow-not-show', $data);
    }

    static function titlePlane($atts, $content = null)
    {
        $par = shortcode_atts(array(
            'title' => '',
        ), $atts);
        $title = '';
        if ($par['title'] != '') {
            $title = "<p>{$par['title']}</p>";
        }
        $html = '<div class="title-plane">' . $title . '<div>' . do_shortcode($content) . '</div></div>';
        return $html;
    }

    static function startPlane($atts, $content = null)
    {
        $par = shortcode_atts(array(
            'type' => '1',
        ), $atts);
        $html = '<div class="start-plane" type="' . $par['type'] . '"><div>' . do_shortcode($content) . '</div></div>';
        return $html;
    }

    static function iconUrl($atts, $content = null)
    {
        $par = shortcode_atts([
            'target' => '',
            'href' => '',
        ], $atts);
        if ($content == null) {
            $content = $par['href'];
        }
        $html = '<a class="icon-url" href="' . $par['href'] . '" target="' . $par['target'] . '">' . do_shortcode($content) . '</a>';
        return $html;
    }

    static function zdPlane($atts, $content = null)
    {
        $par = shortcode_atts([
            'title' => '',
        ], $atts);

        if ($par['title'] == '') {

            $title = "折叠内容，点击展开";
        } else {
            $title = $par['title'];
        }
        $html = '<div class="zd-plane"><div class="zd-plane-title"><i class="czs-add zd-icon"></i>' . $title . '</div><div class="zd-plane-content">' . do_shortcode($content) . '</div></div>';
        return $html;
    }

    static function loginShow($atts, $content = null)
    {
        if (WordPress::isLogin()) {
            return '<div class="loginshow-logind"><div class="note">您已经登录，内容已为您显示</div><div class="login-show-content">' . do_shortcode($content) . '</div></div>';
        } else {
            return '<div class="loginshow"><div class="note">本内容需要登录后查看</div><div style="text-align: center;margin-bottom: 10px"><button class="el-button el-button--primary el-button--small need-login-btn">登录</button></div></div>';
        }
    }

    static function clickShow($atts, $content = null)
    {
        $par = shortcode_atts([
            'type' => '1',
        ], $atts);

        if ($par['type'] == 1) {
            return '<span class="clickshow" title="点击显示或隐藏">' . do_shortcode($content) . '</span>';
        } else {
            return '<div class="clickshow clickshow-show" title="点击显示或隐藏">' . do_shortcode($content) . '</div>';
        }
    }

    static function cAlert($atts, $content = null)
    {
        $par = shortcode_atts([
            'type' => 'info',
        ], $atts);
        $class = 'c-alert-info';
        $icon = 'el-icon-info';
        if ($par['type'] == 'warning') {
            $class = 'c-alert-warning';
            $icon = 'el-icon-warning';
        } elseif ($par['type'] == 'success') {
            $class = 'c-alert-success';
            $icon = 'el-icon-success';
        } elseif ($par['type'] == 'error') {
            $class = 'c-alert-error';
            $icon = 'el-icon-error';
        }
        return '<div class="c-alert ' . $class . '"><i class="' . $icon . '"></i>' . do_shortcode($content) . '</div>';
    }

    static function cDownbtn($atts, $content = '资源文件')
    {
        $par = shortcode_atts([
            'type' => 'default',
            'pwd' => '',
            'url' => '',
        ], $atts);
        $data['img'] = Config::$img_url . "/short-code/download-plane/" . $par['type'] . ".svg";
        $data['url'] = $par['url'];
        if ($par['pwd'] == '') {
            $data['pwd'] = '';
        } else {
            $data['pwd'] = ' | 提取码:' . $par['pwd'] . ' <i title="复制提取码" class="copy el-icon-copy-document copy-pwd" pwd="' . $par['pwd'] . '"></i>';
        }
        $wp_list = [
            ['default', '默认下载'], ['bd', '百度网盘'],
            ['al', '阿里云盘'], ['ty', '天翼云盘'],
            ['ct', '诚通云盘'], ['lz', '蓝奏云'],
            ['123', '123盘'], ['xl', '迅雷云盘'],
            ['wy', '微云'], ['360', '360云盘'],
            ['quark', '夸克网盘'], ['uc', 'UC网盘'],
            ['github', 'GitHub'], ['OneDrive', 'OneDrive'],
            ['wss', '文叔叔']
        ];
        $data['wp_name'] = '默认下载';
        $data['title'] = $content;
        foreach ($wp_list as $item) {
            if ($item[0] == $par['type']) {
                $data['wp_name'] = $item[1];
            }
        }
        return Template::getTemplateHtml('download-plane', $data);
    }

    static function pwdShow($atts, $content = '')
    {
        $par = shortcode_atts([
            'pwd' => ''
        ], $atts);
        if ($par['pwd'] == '') {
            return '<div class="pwd-show">' . do_shortcode($content) . '</div>';
        }
        if (isset($_POST['pwd-show']) && $_POST['pwd-show'] == $par['pwd']) {
            return '<div class="pwd-show">' . do_shortcode($content) . '</div>';
        }

        return Template::getTemplateHtml('password-show', []);
    }

    static function dh($atts, $content = '')
    {
        $par = shortcode_atts([
            'url' => '',
            'icon' => '',
            'des' => ''
        ], $atts);
        $colors = ['#E57373', '#F06292', '#BA68C8', '#9575CD', '#7986CB', '#64B5F6', '#4DD0E1', '#4DB6AC', '#81C784', '#FFD54F', '#FF8A65', '#A1887F', '#90A4AE'];

        if ($par['icon'] == null) {
            if ($content == '') {
                $content = '网址';
            }
            $img_html = '<div class="corepress-img-none" style="background-color: ' . $colors[array_rand($colors, 1)] . '">' . Tools::substr($content, 0, 1) . '</div>';
        } else {
            $img_html = ' <img class="not-shadow" src="' . $par['icon'] . '">';
        }
        $data['name'] = $content;
        $data['des'] = $par['des'];
        $data['img'] = $img_html;
        $data['url'] = $par['url'];
        return Template::getTemplateHtml('shortcode-dh', $data);
    }

    static function postcard($atts, $content = '')
    {
        $par = shortcode_atts([
            'type' => 'list',
        ], $atts);

        $ids = explode(',', $content);
        $html = '';
        foreach ($ids as $item) {
            if (get_post($item)) {
                $post_info = WordPress::getPostMetaData(get_post($item), false);
                if ($par['type'] === 'list') {
                    $post_info['hide_card'] = 'true';
                } else {
                    $post_info['hide_item'] = 'true';
                }
                $html .= Template::getTemplateHtmlByDOMDocument('shortcode-post-card-item', $post_info);
            }
        }
        return $html;
    }

    static function instanceCard($atts, $content = null)
    {
        $par = shortcode_atts([
            'url' => '',
            'name' => '',
            'color' => '#83a9ff'
        ], $atts);
        $par['content'] = $content;
        $par['bg1'] = Config::$img_url . "/widget/instance-card-bg-1.svg";
        if ($par['url'] === '') {
            $par['hide_url'] = 'true';
        }
        return Template::getTemplateHtmlByDOMDocument('shortcode-instance-card', $par);
    }

    static function cnBtn($atts, $content = '打开')
    {
        $par = shortcode_atts([
            'url' => '',
            'bg' => '#3a8ee6',
            'color' => '#000'
        ], $atts);
        return '<a class="site-window-btn" href="' . $par['url'] . '" target="_blank" style="color:' . $par['color'] . ';background-color:' . $par['bg'] . '">' . $content . '</a>';
    }
}
