<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

function score777_render($atts) {
    $settings = get_option('cfg777_settings_options');
    $cfg = require('cfg.php');

    $attsCfg = shortcode_atts(array('path' => ''), $atts, '777score');

    $path = $attsCfg['path'];
    $ver = esc_attr($cfg['ver']);

    $fields = array(
        'main_font',
        'main_bg_color',
        'main_color',
        'main_size',
        'main_weight',
        'mtr_bg_color',
        'mtr_bg_color_category_logo',
        'mtr_dividers_color',
        'mtr_category_name_color',
        'mtr_category_name_size',
        'mtr_category_name_weight',
        'mtr_tournament_name_color',
        'mtr_tournament_name_size',
        'mtr_tournament_name_weight',
        'mtr_date_color',
        'mtr_date_size',
        'mtr_date_weight',
        'msdr_bg_color',
        'msd_dividers_color',
        'msdr_font_color',
        'msdr_font_size',
        'msdr_font_weight',
        'mmr_bg_color',
        'mmr_dividers_color',
        'mmr_hover_color',
        'mmr_team_name_color',
        'mmr_team_name_size',
        'mmr_team_name_weight',
        'mmr_status_finished_color',
        'mmr_status_finished_size',
        'mmr_status_finished_weight',
        'mmr_status_live_color',
        'mmr_status_live_size',
        'mmr_status_live_weight',
        'mmr_score_win_team_color',
        'mmr_score_win_team_size',
        'mmr_score_win_team_weight',
        'mmr_details_activation',
        'mmr_details_bg_color',
        'mmr_details_button_bg_color',
        'mmr_details_button_size',
        'mmr_details_button_weight',
        'mmr_score_team_color',
        'mmr_score_team_size',
        'mmr_score_team_weight',
        'mmr_time_color',
        'mmr_time_size',
        'mmr_time_weight',
        'mmr_score_team_color_basket',
        'md_commands_info_bg_color',
        'md_commands_info_color',
        'md_commands_info_divider_color',
        'md_commands_info_size',
        'md_commands_info_weight',
        'md_bg_color',
        'md_dividers_color',
        'md_1st_player_name_color',
        'md_1st_player_name_size',
        'md_1st_player_name_weight',
        'md_2nd_player_name_color',
        'md_2nd_player_name_size',
        'md_2nd_player_name_weight',
        'md_score_color',
        'md_score_size',
        'md_score_weight',
        'md_points_color',
        'md_points_size',
        'md_points_weight',
        'md_time_goals_color',
        'md_time_goals_size',
        'md_time_goals_weight',
        'md_time_other_color',
        'md_time_other_size',
        'md_time_other_weight',
        'md_period_name_bg_color',
        'md_period_divider_color',
        'md_period_name_color',
        'md_period_name_size',
        'md_period_name_weight',
        'md_placeholder_bg_color',
        'md_placeholder_divider_color',
        'md_placeholder_font_color',
        'md_placeholder_font_size',
        'md_placeholder_font_weight',
        'md_loader_bg_color',
        'md_loader_dividers_color',
        'str_bg_color',
        'str_divider_color',
        'str_bg_color_category_logo',
        'str_category_name_color',
        'str_category_name_size',
        'str_category_name_weight',
        'str_tournament_name_color',
        'str_tournament_name_size',
        'str_tournament_name_weight',
        'str_season_color',
        'str_season_size',
        'str_season_weight',
        'sn_bg_color',
        'sn_btn_bg_color',
        'sn_btn_active_color',
        'sn_btn_color',
        'sn_divider_color',
        'sb_bg_color',
        'sb_team_row_dividers_color',
        'sb_team_row_hover',
        'sb_standing_name_color',
        'sb_standing_name_size',
        'sb_standing_name_weight',
        'sb_header_bg_color',
        'sb_header_divider_color',
        'sb_columns_and_numbering_color',
        'sb_columns_and_numbering_size',
        'sb_columns_and_numbering_weight',
        'sb_team_name_color',
        'sb_team_name_size',
        'sb_team_name_weight',
        'sb_standing_value_color',
        'sb_standing_value_size',
        'sb_standing_value_weight',
        'sb_standing_legend_bg_color',
        'sb_standing_legend_divider_color',
        'sb_standing_legend_color',
        'sb_standing_legend_size',
        'sb_standing_legend_weight'
    );
    $result = '';
    $js = array();

    foreach ($fields as $val) {
        $tmpVal = isset($settings[$val]) ? $settings[$val] : 'null';

        if (!empty($tmpVal)) {
            $js[] = "window.{$val} = '{$tmpVal}';";
        }
    }

    if (!empty($js)) {
        $jsVars = implode(PHP_EOL.'            ', $js);
        $result = "

        <script id='skin'>
            {$jsVars}
        </script>
        ";
    }

    $lang = isset($settings['777_lang']) ? $settings['777_lang'] : '';

    // check if lang supported in case not do it en
    if (!in_array($lang, array_keys($cfg['domainMap']))) {
        $locale = substr(get_locale(), 0, 2);

        $lang = in_array($locale, array_keys($cfg['domainMap'])) ? $locale : 'en';
    }

    $pathParts = explode('/', $path);
    $isStanding = isset($pathParts[5]) && 'standings' === $pathParts[5];
    $leagueName = isset($pathParts[4]) ? ucwords(str_replace('-', ' ', $pathParts[4])) : '';
    $textPathKey = empty($path) ? '/today' : $path;
    $textPathKey = $leagueName ? '{tournament}' : $textPathKey;
    $textPathKey = $isStanding ? 'standings' : $textPathKey;

    $pathToText = ucwords(str_replace('-', ' ', str_replace('/', ' ', $path)));
    $defText = '777score'.($pathToText ? ' '.$pathToText : '');

    $rnd = isset($settings['777_your_word_num']) ? $settings['777_your_word_num'] : 0;
    $text = isset($cfg['domainMap'][$lang][$textPathKey]) ? $cfg['domainMap'][$lang][$textPathKey][$rnd] : $defText;
    $text = false !== strpos($text, '{tournament}') ? str_replace('{tournament}', $leagueName, $text) : $text;

    $iframePath = empty($path) ? '' : "path=".esc_attr($path)."&";
    $url = esc_url($cfg['domainMap'][$lang]['base']);
    $lang = isset($cfg['domainMap'][$lang]) && isset($cfg['domainMap'][$lang]['lang']) ? $cfg['domainMap'][$lang]['lang'] : '';
    $iframeLang = $lang ? '&lang='.esc_attr($lang) : '';
    $result = "

        <iframe
            src='{$url}/777plugin{$ver}/?{$iframePath}word=".esc_attr($text)."{$iframeLang}'
            marginheight='0'
            marginwidth='0'
            scrolling='auto'
            height='5000'
            width='100'
            frameborder='0'
            id='777scoreframe'
            style='width: 100%; height: 5000px; max-width: 100%'
        ></iframe>
        ";

    return $result;
}
