<?php

namespace core_next;

class Options
{

    static function echoJSData($name, $data)
    {
        echo "<script>let $name='$data'; </script>";
    }

    static function getAdminOptions()
    {
        $data['ajax_url'] = admin_url('admin-ajax.php');
        $data['ajax_name'] = Config::$theme_name;
        $data['theme_version'] = Config::$theme_version;
        $data['theme_version_name'] = Config::$theme_version_name;
        return $data;
    }

    static function getFrontOptions()
    {
        global $core_next_set;
        global $paged, $wp_query;
        $data['ajax_url'] = admin_url('admin-ajax.php');
        $data['ajax_name'] = Config::$theme_name;
        $data['site_name'] = get_bloginfo('name');

        $data['logo'] = $core_next_set['logo'];
        $data['is_login'] = WordPress::isLogin();
        $data['search_url'] = home_url('/?s={word}');
        $data['search_word'] = get_search_query();
        $data['go_top_img'] = Config::$img_url . "/rocket.svg";
        $data['phone_menu_header_img'] = $core_next_set['phone_menu_header_img'];
        $data['img_url'] = Config::$img_url;
        $data['user_window_img'] = $core_next_set['user_window_img'];
        if (is_home()) {
            $data['swiper'] = $core_next_set['swiper'];
            $data['card_list'] = $core_next_set['card_list'];
            $data['card_open'] = $core_next_set['card_open'];
            $data['card_list_row_number'] = $core_next_set['card_list_row_number'];
            $data['tab_cat_list'] = WordPress::getCatInfoList($core_next_set['home_post_list_id']);

            $data['partners_open'] = $core_next_set['partners_open'];
            $data['partners_description'] = $core_next_set['partners_description'];
            $data['partners_application_url'] = $core_next_set['partners_application_url'];
            $data['partners_list'] = $core_next_set['partners_list'];

        }
        if (is_single() || is_page()) {
            $data['post_id'] = get_the_ID();
            $data['post_content_url_new_window'] = $core_next_set['post_content_url_new_window'];
            $data['post_like'] = (int)get_post_meta($data['post_id'], 'cp_like', true);
            $data['pwd_show_count'] = WordPress::getShortcodeCount('pwdshow', get_the_content());
            $data['face_list'] = Theme::getFaceList();
            $data['is_single'] = is_single();
            $data['post_title'] = get_the_title();
            $data['single_end_btn_like'] = $core_next_set['single_end_btn_like'];
            $data['single_end_btn_share'] = $core_next_set['single_end_btn_share'];
            $data['single_end_btn_qrcode'] = $core_next_set['single_end_btn_qrcode'];
            $data['single_end_btn_poster'] = $core_next_set['single_end_btn_poster'];
            $data['single_end_btn_tip'] = $core_next_set['single_end_btn_tip'];
            $data['single_tip_wx'] = $core_next_set['single_tip_wx'];
            $data['single_tip_zfb'] = $core_next_set['single_tip_zfb'];
            if ($core_next_set['single_end_btn_share']) {
                $excerpt = WordPress::getPostExcerpt(get_post());
                $data['qq_url'] = Tools::getShareUrl('qq', get_the_title(), $excerpt);
                $data['weibo_url'] = Tools::getShareUrl('weibo', get_the_title(), $excerpt);
                $data['qzone_url'] = Tools::getShareUrl('qzone', get_the_title(), $excerpt);
            }
            $data['single_directory'] = $core_next_set['single_directory'];
            $data['comment_open'] = $core_next_set['comment_open'];
            if (comments_open()) {
                $data['post_comment_open'] = '1';
            } else {
                $data['post_comment_open'] = '0';
            }
            $data['comment_location_remove_text'] = $core_next_set['comment_location_remove_text'];
        } else {
            $data['is_single'] = 0;
        }

        if (is_author()) {
            $author_obj = get_queried_object();
            $data['user'] = $author_obj->user_login;
        }
        $data['total_page'] = $wp_query->max_num_pages;
        $data['now_page'] = $paged;
        if ($data['now_page'] == 0) {
            $data['now_page'] = 1;
        }
        $data['page_url'] = get_pagenum_link(10086);
        if (isset(get_nav_menu_locations()['header_menu'])) {
            $data['header_menu'] = WordPress::getMenuArray('header_menu');
        } else {
            $data['header_menu'] = [];
        }

        $data['menu_url_new_window'] = $core_next_set['menu_url_new_window'];
        $data['menu_position'] = $core_next_set['menu_position'];
        if ($data['is_login']) {
            $user_id = get_current_user_id();
            $user = get_userdata($user_id);
            $data['user_mail'] = $user->user_email;
            $data['user_name'] = $user->nickname;
            $data['display_name'] = $user->display_name;
            $data['user_description'] = $user->user_description;
            $data['avatar_url'] = Theme::getUserAvatar($data['user_mail']);
            $data['login_out_url'] = wp_logout_url(get_bloginfo('url'));
            $data['author_url'] = get_author_posts_url($user_id);
            $data['is_admin'] = WordPress::isAdmin();
            if ($data['is_admin']) {
                $data['admin_url'] = admin_url();
            }
            if (get_user_meta($user_id, 'corepress_thirdparty_qq', true)) {
                $data['user_bind_qq'] = true;
            } else {
                $data['user_bind_qq'] = false;
            }
            $data['user_url'] = $user->user_url;
        } else {
            $data['avatar_url'] = Config::$img_url . '/user.png';
        }
        $data['login_out_icon'] = Config::$img_url . "/icon/login-out.svg";
        $data['can_reg'] = get_option('users_can_register');
        $data['show_login_btn'] = $core_next_set['show_login_btn'];
        $data['user_center_url'] = $core_next_set['user_center_url'];
        $data['user_center_icon'] = Config::$img_url . "/icon/user.png";
        $data['dark_css'] = Config::$css_url . "/dark.css?v=" . Config::$theme_version;
        $data['dark_open'] = $core_next_set['dark_open'];
        $data['dark_logo'] = $core_next_set['dark_logo'];
        $data['default_dark'] = $core_next_set['default_dark'];
        $data['auto_change_dark'] = $core_next_set['auto_change_dark'];

        $data['notice_open'] = $core_next_set['notice_open'];
        $data['notice_position'] = $core_next_set['notice_position'];
        $data['notice_content'] = $core_next_set['notice_content'];
        $data['notice_scroll'] = $core_next_set['notice_scroll'];

        $data['close_reg_mail_verify'] = $core_next_set['close_reg_mail_verify'];
        $data['single_post_lightbox'] = $core_next_set['single_post_lightbox'];


        $data['site_window_open'] = $core_next_set['site_window_open'];
        $data['site_window_title'] = $core_next_set['site_window_title'];
        $data['site_window_content'] = $core_next_set['site_window_content'];
        $data['site_window_btns'] = do_shortcode($core_next_set['site_window_btns']);
        $data['site_window_time'] = $core_next_set['site_window_time'];
        $data['site_window_code'] = $core_next_set['site_window_code'];

        $data['need_login_comment'] = get_option('comment_registration') && !WordPress::isLogin();

        $data['other_login_open'] = $core_next_set['other_login_open'];
        $data['other_login_auth_url'] = $core_next_set['other_login_auth_url'];
        $data['other_login_qq_open'] = $core_next_set['other_login_qq_open'];
        $data['full_header'] = $core_next_set['full_header'];
        $data['user_custom_menu'] = self::getUserMenuList();
        $data['open_user_website_url'] = $core_next_set['open_user_website_url'];
        $data['open_user_update_avatar'] = $core_next_set['open_user_update_avatar'];
        return $data;
    }

    static function getUserMenuList()
    {
        global $core_next_set;
        $list = $core_next_set['user_custom_menu'];

        $is_admin = WordPress::isAdmin();
        $arr = [];
        foreach ($list as $item) {
            if (in_array('admin', $item['show'])) {
                if ($is_admin) {
                    $can_add = true;
                }
            }
            if (in_array('user', $item['show'])) {
                $can_add = true;
            }
            if ($can_add) {
                unset($item['show']);
                $arr[] = $item;
            }
        }
        return $arr;
    }

    static function getOptions()
    {
        $default = self::getDefaultOptions();
        $set = get_option(Config::$set_name, false);

        if ($set === false) {
            return $default;
        }
        $set_obj = json_decode(base64_decode(self::unlockSet($set)), true);
        if ($set_obj === false) {
            return $default;
        } else {
            return self::updateOptions($set_obj, $default);
        }
    }

    private static function getDefaultPostMetaBoxOptions()
    {
        $data['custom_thumbnail'] = '';
        $data['custom_seo'] = false;
        $data['seo_title'] = '';
        $data['seo_description'] = '';
        $data['seo_keyword'] = '';

        return $data;
    }


    static function getPostMetaBoxOptions($post_id)
    {
        $data = get_post_meta($post_id, 'core_next_post_meta', true);
        $default_set = self::getDefaultPostMetaBoxOptions();
        if ($data) {
            $data = self::updateOptions(json_decode(base64_decode($data), true), $default_set);
        } else {
            $data = $default_set;
        }
        return $data;
    }


    private static function getExpandDefaultOptions()
    {
        $data['need_update'] = false;
        $data['last_check_time'] = time();

        $data['update_home_post_list_cache'] = false;

        return $data;
    }

    static function getExpandOptions()
    {
        $default = self::getExpandDefaultOptions();
        $set = get_option(Config::$expand_set_name, false);

        if ($set === false) {

            return $default;
        }
        return self::updateOptions($set, $default);
    }


    private static function getDefaultOptions()
    {

        $data['logo'] = Config::$img_url . "/stulogo.png";
        $data['dark_logo'] = Config::$img_url . "/stulogo1.png";
        $data['icp'] = '';
        $data['icp_police'] = '';
        $data['site_icon'] = '';
        $data['menu_url_new_window'] = true;
        $data['post_list_url_new_window'] = true;
        $data['post_content_url_new_window'] = true;
        $data['force_ssl'] = false;
        $data['close_copyright'] = false;
        //home

        $data['home_post_list_type'] = 'page';
        $data['home_post_list_id'] = [];
        $data['home_post_exclude_id'] = [];

        $data['swiper_open'] = false;
        $data['swiper'] = [];
        $data['card_open'] = false;
        $data['card_list_row_number'] = 4;
        $data['card_list'] = [];
        $data['friends_open'] = false;
        $data['friends_list'] = [];
        $data['friends_description'] = '';
        $data['friends_theme'] = 'link';
        $data['friends_application_url'] = '';
        $data['partners_open'] = false;
        $data['partners_description'] = '';
        $data['partners_application_url'] = '';
        $data['partners_list'] = [];

        //theme
        $data['theme_main_color'] = '#409eff';
        $data['text_select_color'] = '#3390ff';
        $data['box_border_radius'] = 5;
        $data['aside_type'] = 'theme';
        $data['menu_position'] = 'left';
        $data['aside_style'] = 'classic';
        $data['aside_box_img'] = '';
        $data['post_img_shadow'] = true;
        $data['phone_menu_header_img'] = Config::$img_url . "/phone-header.svg";
        $data['background_image'] = '';
        $data['background_image_type'] = 'repeat';
        $data['background_image_dark'] = '';
        $data['background_image_dark_type'] = 'repeat';
        $data['font_name'] = 'default';
        $data['footer_wave'] = false;
        $data['loading_animations'] = false;
        $data['loading_animations_type'] = 'yuanhuanfangda';
        $data['full_header'] = true;


        //single
        $data['post_excerpt_len'] = 150;
        $data['default_thumbnail'] = Config::$img_url . "/slt2.png";
        $data['close_thumbnail'] = false;
        $data['random_thumbnail_api'] = false;
        $data['auto_thumbnail'] = true;
        $data['single_copyright_open'] = true;
        $data['single_copyright_custom'] = false;
        $data['single_copyright_text'] = '';
        $data['single_code_highlight_open'] = true;
        $data['single_code_highlight_theme'] = 'light';
        $data['single_end_btn_like'] = true;
        $data['single_end_btn_share'] = true;
        $data['single_end_btn_qrcode'] = true;
        $data['single_end_btn_poster'] = true;
        $data['single_end_btn_tip'] = true;
        $data['single_tip_wx'] = '';
        $data['single_tip_zfb'] = '';
        $data['single_post_lightbox'] = true;
        $data['single_directory'] = true;

        $data['loading_open'] = true;
        $data['loading_img'] = 'blocks.svg';
        $data['loading_img_url'] = '';

        $data['open_related_articles'] = true;
        $data['open_related_title'] = '相关推荐';
        $data['related_articles_theme'] = 'list';
        $data['related_articles_num'] = 10;

        $data['open_post_page_card'] = true;

        $data['read_more_content'] = true;

        //comment
        $data['open_ua_parser'] = true;
        $data['comment_approve'] = false;
        $data['comment_open'] = true;
        $data['comment_approve_type'] = 'all';
        $data['comment_approve_word'] = '';
        $data['comment_blacklist'] = '';


        //module
        $data['dark_open'] = true;
        $data['default_dark'] = false;
        $data['auto_change_dark'] = false;

        $data['wxshow_qrcode'] = '';
        $data['wxshow_pwd'] = '1234';
        $data['wxshow_cookie_day'] = '7';
        $data['wxshow_description'] = '扫描二维码关注微信，回复<span style="color: red">密码</span>，即可获取密码';

        $data['mail_open'] = false;
        $data['mail_user'] = '';
        $data['mail_password'] = '';
        $data['mail_host'] = '';
        $data['mail_port'] = '465';
        $data['mail_encrypt'] = 'ssl';
        $data['mail_from_name'] = '';
        $data['mail_test_mail'] = '';

        $data['notice_open'] = false;
        $data['notice_position'] = 'header';
        $data['notice_content'] = '';
        $data['notice_scroll'] = true;

        $data['site_window_open'] = false;
        $data['site_window_title'] = '网站公告';
        $data['site_window_content'] = '';
        $data['site_window_btns'] = '[cn_btn url="https://www.lovestu.com" bg="#3a8ee6" color="#fff"]示例按钮[/cn_btn]';
        $data['site_window_time'] = 0;
        $data['site_window_code'] = 1234;

        $data['phone_nav_open'] = false;
        $data['phone_nav_list'] = [];
        $data['phone_nav_show_home'] = true;
        $data['phone_nav_show_single'] = true;
        $data['phone_nav_show_page'] = true;
        $data['phone_nav_show_cate'] = true;

        $data['comment_location_open'] = false;
        $data['comment_location_show'] = false;
        $data['comment_location_type'] = '365';
        $data['comment_location_365_key'] = '';
        $data['comment_location_remove_text'] = '中国';
        $data['comment_location_show_type'] = ['国家', '省份', '城市'];


        $data['other_login_open'] = false;
        $data['other_login_auth_url'] = '';
        $data['other_login_qq_open'] = false;
        $data['other_login_qq_appid'] = '';
        $data['other_login_qq_appkey'] = '';


        //insertCode
        $data['icon_font_js_url'] = '';
        $data['head_code'] = '';
        $data['footer_code'] = '';
        $data['custom_css'] = '';
        //safe
        $data['mail_white_list_open'] = false;
        $data['mail_white_list'] = 'qq.com,mail.com';
        $data['close_reg_mail_verify'] = false;
        //performance
        $data['home_post_list_cache'] = false;

        $data['widget_post_list_cache'] = false;
        $data['widget_comment_cache'] = false;

        $data['avatar_cdn'] = 'no';
        $data['theme_cdn'] = 'bytedance';

        //seo
        $data['seo_title_type'] = 'title_site';
        $data['seo_title_delimiter'] = '-';
        $data['seo_title_delimiter_del_space'] = false;

        $data['seo_open'] = false;
        $data['seo_keywords'] = '';
        $data['seo_description'] = '';
        $data['open_auto_seo'] = true;
        //ad
        $data['ad_home1'] = '';
        $data['ad_home1_phone'] = '';
        $data['ad_home2'] = '';
        $data['ad_home2_phone'] = '';
        $data['ad_home3'] = '';
        $data['ad_home3_phone'] = '';

        $data['ad_post1'] = '';
        $data['ad_post1_phone'] = '';
        $data['ad_post2'] = '';
        $data['ad_post2_phone'] = '';
        $data['ad_post3'] = '';
        $data['ad_post3_phone'] = '';
        $data['ad_post4'] = '';
        $data['ad_post4_phone'] = '';
        $data['ad_post5'] = '';
        $data['ad_post5_phone'] = '';
        //user center
        $data['show_login_btn'] = true;
        $data['user_center_url'] = '';
        $data['user_window_img'] = Config::$img_url . '/login/user_window_bg1.webp';
        $data['user_custom_menu'] = [];
        $data['open_user_website_url'] = true;
        $data['open_user_update_avatar'] = false;
        $data['local_avatar'] = false;
        $data['default_avatar'] = Config::$img_url . '/default_avatar.png';


        //function Switch
        $data['close_gutenberg_editor'] = true;
        return $data;
    }

    private static function updateOptions($set, $default_set)
    {
        foreach ($default_set as $key => &$item) {
            if (isset($set[$key])) {
                $item = $set[$key];
            }
        }
        return $default_set;
    }

    static function getAuthSetData()
    {
        $data['theme_version'] = Config::$theme_version;
        $data['set_data'] = self::getOptions();
        $data['cat_list'] = WordPress::getCatList();
        $json = base64_encode(json_encode($data));
        return $json;
    }

    static function saveSet($data)
    {
        return update_option(Config::$set_name, $data);
    }

    static function saveExpandSet($data)
    {
        return update_option(Config::$expand_set_name, $data);
    }

    static function unlockSet($txt)
    {
        $key = 'lovestu.com';
        $txt = urldecode($txt);
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        $ch = $txt[0];
        $nh = strpos($chars, $ch);
        $mdKey = md5($key . $ch);
        $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
        $txt = substr($txt, 1);
        $tmp = '';
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < strlen($txt); $i++) {
            $k = $k == strlen($mdKey) ? 0 : $k;
            $j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
            while ($j < 0) {
                $j += 64;
            }
            $tmp .= $chars[$j];
        }
        return base64_decode($tmp);
    }
}