<?php

class RmMultilangTextElement extends OxyEl {

    function name() {
        return 'RM Multilang Text';
    }

    function slug() {
        return "rm-multilang-text";
    }

    function icon() {
        return CT_FW_URI . '/toolbar/UI/oxygen-icons/add-icons/text.svg';
    }

    function button_place() {
        return "other";
    }

    function tag() {
        return array('default' => 'p', 'choices' => 'div,h1,h2,h3,h4,h5,h6,p,span' );
    }

    function render($options, $defaults, $content) {
        $languages      = pll_languages_list();
        $current_lang   = function_exists('pll_current_language') ? pll_current_language() : '';
        $text_by_lang   = isset($options["text_{$current_lang}"]) ? $options["text_{$current_lang}"] : '';
        $content_source = isset($options["content_source"]) ? $options["content_source"] : '';
        
        switch($content_source) {
            case "direct":
                $text = $text_by_lang;
                break;
            case "options_one_lang":
                $text = (isset ($languages[0]) && isset($options["text_{$languages[0]}"]) ) ? get_option($options["text_{$languages[0]}"]) : '..';
                break;
            case "options_multi":
                $text = get_option($text_by_lang, '....');
                break;
        }

        echo nl2br($text);
    }

    function controls() {
        if (!function_exists('pll_languages_list')) {
            return;
        }
        
        
        $this->addOptionControl([
            "type" => 'dropdown',
            "name" => 'Content source',
            "slug" => 'content_source',
        ])->setValue([
            "direct" => "Direct",
            "options_multi" => "Multi lang from settings page",
            "options_one_lang" => "From settings page",
        ])->setDefaultValue("direct")->rebuildElementOnChange();
        

        $languages = pll_languages_list();
        
        // اللغة
        if($languages) {
            
            foreach ($languages as $lang_code) {
                $this->addOptionControl([
                    "type" => 'textarea',
                    "name" => strtoupper($lang_code) . ' Text',
                    "slug" => "text_{$lang_code}",
                    "default" => 'Enter text here ' . $lang_code,
                ])->rebuildElementOnChange();
            }           
        }
        
        
        $this->addTagControl();
    }

    function defaultCSS() {
        return "";
    }

}

new RmMultilangTextElement();
