/**
 * active module by slug
 * @param $module one or more module data
 * @param int|string $pos
 */
function hw_activate_modules($module, $pos = '')
{
    static $actived_modules;
    $modules = array();
    if (!$actived_modules) {
        $actived_modules = hw_get_modules(true);
    }
    $count = count($actived_modules);
    if (isset($module[0]) && !is_array($module[0])) {
        $modules[] = $module;
    } else {
        $modules = $module;
    }
    foreach ($modules as $_module) {
        if (!isset($actived_modules[$_module[0]]) && HW_TGM_Module_Activation::get_modules($_module[0])) {
            //get register module meta data
            $info = HW_TGM_Module_Activation::get_modules($_module[0]);
            if (empty($info['position'])) {
                $info['position'] = count($actived_modules);
            }
            //$actived_modules[$_module[0]] = $_module;
            $actived_modules = hwArray::add_item_keyval($actived_modules, $_module[0], $_module, $info['position']);
            //load new module file for initialize
            HW_HOANGWEB::load_module($_module[0]);
            //activation hook for module
            $activation_hooks = HW_HOANGWEB::register_activation_hook(realpath(HW_HOANGWEB_PLUGINS . '/' . $_module[0]));
            if (is_array($activation_hooks)) {
                foreach ($activation_hooks as $activation_hook) {
                    if (is_callable($activation_hook)) {
                        call_user_func($activation_hook);
                        usleep(200);
                        //sleep in miliseconds
                    }
                }
            }
        }
    }
    //save option to db
    if ($count !== count($actived_modules)) {
        update_option('hw_install_modules', $actived_modules);
        HW_Cache::reset_wp_menus_caches();
        return true;
    }
    return false;
}
    /**
     * Render the settings page
     */
    public function _wxr_settings_page()
    {
        $options = get_option('hw_wxr_settings');
        ?>
        <div class="wrap">
            <div id="icon-tools" class="icon32"><br /></div>
            <h2><?php 
        screen_icon();
        _e('HOANGWEB Settings');
        ?>
</h2>

            <form method="post" action="options.php" class="options_form">
                <?php 
        settings_fields('hw_wxr_settings_group');
        ?>
                <table class="form-table">
                    <!--
                    <tr valign="top">
                        <th scop="row">
                            <label for="hw_wxr_settings[text]"><?php 
        _e('Plugin Text');
        ?>
</label>
                        </th>
                        <td>
                            <input class="regular-text" type="text" id="hw_wxr_settings[text]" style="width: 300px;" name="hw_wxr_settings[text]" value="<?php 
        if (isset($options['text'])) {
            echo esc_attr($options['text']);
        }
        ?>
"/>
                            <p class="description"><?php 
        _e('Enter some text for the plugin here.');
        ?>
</p>
                        </td>
                    </tr>
                    <tr>
                        <th scop="row">
                            <label for="hw_wxr_settings[label]"><?php 
        _e('Label Text');
        ?>
</label>
                        </th>
                        <td>
                            <input class="regular-text" type="text" id="hw_wxr_settings[label]" style="width: 300px;" name="hw_wxr_settings[label]" value="<?php 
        if (isset($options['label'])) {
            echo esc_attr($options['label']);
        }
        ?>
"/>
                            <p class="description"><?php 
        _e('Enter some text for the label here.');
        ?>
</p>
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scop="row">
                            <span><?php 
        _e('Enable Feature');
        ?>
</span>
                        </th>
                        <td>
                            <input class="checkbox" type="checkbox" id="hw_wxr_settings[enabled]" name="hw_wxr_settings[enabled]" value="1" <?php 
        checked(1, isset($options['enabled']));
        ?>
/>
                            <label for="hw_wxr_settings[enabled]"><?php 
        _e('Enable some feature in this plugin?');
        ?>
</label>
                        </td>
                    </tr>
                    -->
                </table>
                <?php 
        #submit_button();
        ?>
            </form>

            <div class="metabox-holder">
                <div class="postbox">
                    <h3><span><?php 
        _e('Export Settings');
        ?>
</span></h3>
                    <div class="inside">
                        <form method="post">
                            <p><label><input type="radio" name="module" value="all" /> Tất cả</label></p>
                        <?php 
        foreach (HW_TGM_Module_Activation::get_modules() as $slug => $plugin) {
            if (empty($plugin['can_export']) || !HW_Module::is_active($slug)) {
                continue;
            }
            ?>
                            <p><label><input type="radio" name="module" value="<?php 
            echo esc_attr($plugin['slug']);
            ?>
" /> <?php 
            echo esc_html($plugin['name']);
            ?>
</label></p>
                        <?php 
        }
        ?>

                        <p><?php 
        _e('Export the plugin settings for this site as a .json file. This allows you to easily import the configuration into another site.');
        ?>
</p>

                            <p><input type="hidden" name="hw_wxr_action" value="export_settings" /></p>
                            <p>
                                <?php 
        wp_nonce_field('hw_wxr_export_nonce', 'hw_wxr_export_nonce');
        ?>
                                <?php 
        submit_button(__('Export'), 'secondary', 'submit', false);
        ?>
                            </p>
                        </form>
                    </div><!-- .inside -->
                </div><!-- .postbox -->

                <div class="postbox">
                    <h3><span><?php 
        _e('Import Settings');
        ?>
</span></h3>
                    <div class="inside">
                        <p><?php 
        _e('Import the plugin settings from a .json file. This file can be obtained by exporting the settings on another site using the form above.');
        ?>
</p>
                        <!--
                        <form method="post" enctype="multipart/form-data">
                            <p>
                                <input type="file" name="import_file"/>
                            </p>
                            <p>
                                <input type="hidden" name="hw_wxr_action" value="import_settings" />
                                <?php 
        wp_nonce_field('hw_wxr_import_nonce', 'hw_wxr_import_nonce');
        ?>
                                <?php 
        submit_button(__('Import'), 'secondary', 'submit', false);
        ?>
                            </p>
                        </form>
                        -->
                        Nhấn <a href="<?php 
        echo admin_url('admin.php?import=hw-wordpress');
        ?>
">vào đây</a> để tiến hành Import dữ liệu.
                    </div><!-- .inside -->
                </div><!-- .postbox -->
            </div><!-- .metabox-holder -->
        </div>
    <?php 
    }
    /**
     * Widget Backend
     * @param $instance
     */
    public function form($instance)
    {
        //widget title
        if (isset($instance['title'])) {
            $title = $instance['title'];
        } else {
            $title = __('Chữ chạy', 'hoangweb');
        }
        //$library  = isset($instance['library'])? $instance['library'] : ''; //library
        $current_source = isset($instance['source']) ? $instance['source'] : 'content';
        //data source
        $data = isset($instance['data']) ? $instance['data'] : array();
        //data
        //content scrolling
        $content = isset($data['content']) ? $data['content'] : '';
        //skin
        $skin_setting = isset($instance['skin_settings']) ? $instance['skin_settings'] : '';
        $skin = isset($instance['skin']) ? $instance['skin'] : '';
        // Widget admin form
        ?>
        <p><strong>Lưu ý</strong>: nhấn nút lưu widget trong lần đầu tiên khởi tạo widget.</p>
        <p>
            <label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Title:');
        ?>
</label>
            <input class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" type="text" value="<?php 
        echo esc_attr($title);
        ?>
" />
        </p>
        <!--
        <p>
            <label for="<?php 
        //echo $this->get_field_id('library')
        ?>
"><?php 
        _e('Thư viện');
        ?>
</label><br/>
            <select name="<?php 
        //echo $this->get_field_name('library')
        ?>
" id="<?php 
        //echo $this->get_field_id('library')
        ?>
">
                <?php 
        /*foreach($this->libraries as $lib => $text) {
              $selected = ($library == $lib)? 'selected="selected"' : '';
              printf( '<option value="%s" %s>%s</option>', $lib, $selected, $text);
          }*/
        ?>

            </select>
        </p>
        -->
        <?php 
        if (!empty($this->skin)) {
            ?>
        <p>
            <label for="<?php 
            ?>
"><?php 
            _e('Thư viện');
            ?>
</label>
            <?php 
            echo $this->skin->get_skins_select_tag('skin', null, array('class' => 'widefat'), false);
            ?>
            <!-- show skin options if exists -->
            <?php 
            echo $this->skin->prepare_skin_options_fields('skin_settings', $skin_setting, $skin);
            ?>
        </p>
        <?php 
        }
        ?>
        <p>
            <label for="<?php 
        echo $this->get_field_id('source');
        ?>
"><?php 
        _e('Nguồn');
        ?>
</label><br/>
            <select name="<?php 
        echo $this->get_field_name('source');
        ?>
" id="<?php 
        echo $this->get_field_id('source');
        ?>
">
                <?php 
        foreach ($this->sources as $source => $text) {
            $selected = selected($source, $current_source, false);
            printf('<option %s value="%s">%s</option>', $selected, $source, $text);
        }
        ?>
            </select>
        </p>

        <!-- slides data -->
        <?php 
        if ($current_source == 'slides') {
            $module = HW_TGM_Module_Activation::get_modules('hw-ml-slider', OBJECT);
            if (hw_is_active_module('hw-ml-slider')) {
                $current_slider = isset($data['slider']) ? $data['slider'] : '';
                $slides = hwmq_get_all_mlsliders();
                ?>
        <p id="<?php 
                echo $this->get_field_id('source_slides');
                ?>
" style="">
            <label for="<?php 
                echo $this->get_field_id('slider');
                ?>
"><?php 
                _e('Slider');
                ?>
</label><br/>
            <select name="<?php 
                echo $this->get_field_name('data');
                ?>
[slides]" id="<?php 
                echo $this->get_field_id('slides');
                ?>
">
                <?php 
                foreach ($slides as $id => $name) {
                    $selected = selected($id, $current_slider, false);
                    printf('<option %s value="%s">%s</option>', $selected, $id, $name);
                }
                ?>
            </select>
        </p>
        <?php 
            } else {
                echo __('Vui lòng kích hoạt module ' . $module->name);
            }
        } elseif ($current_source == 'content') {
            //default
            ?>
        <!-- content text -->
        <p id="<?php 
            echo $this->get_field_id('source_content');
            ?>
" style="">
            <label for="<?php 
            echo $this->get_field_id('content');
            ?>
"><?php 
            _e('Nội dung chạy');
            ?>
</label><br/>
            <textarea class="" style="width:100%; min-height:200px" name="<?php 
            echo $this->get_field_name('data');
            ?>
[content]" id="<?php 
            echo $this->get_field_id('content');
            ?>
"><?php 
            echo isset($data['content']) ? $data['content'] : '';
            ?>
</textarea>
        </p>
            <script>
                jQuery(function($){
                    CKEDITOR.config.extraPlugins = "hw_wp_media_button";
                    //CKEDITOR.config.width = 700;
                    var editor = '<?php 
            echo $this->get_field_id('content');
            ?>
',
                        id = '<?php 
            echo $this->number;
            ?>
',
                        editor_obj = CKEDITOR.instances[editor];

                    var config = {
                        language: "vi",
                        //uiColor: "#9AB8F3",
                        width: 700,
                        extraPlugins : "hw_wp_media_button",
                        resize_enabled : true,
                        resize_minWidth: 300,
                        resize_minHeight: 300,
                        resize_maxWidth : 2000,
                        resize_maxHeight: 2000,
                        resize_dir: 'both'
                    };
                    // Replace the <textarea id="editor1"> with a CKEditor
                    // instance, using default configuration.
                    /*if (editor_obj) {
                     editor_obj.destroy(true);
                     CKEDITOR.remove(editor_obj);
                     }*/
                    if(!(!jQuery.isNumeric(id) && editor_obj)) CKEDITOR.replace( editor ,config);

                    $('#<?php 
            echo $this->get_field_id('savewidget');
            ?>
').bind('click', function(e){
                        console.log('savewidget');
                        $('#'+editor).val( CKEDITOR.instances[editor].getData());
                    });
                });

            </script>
            <?php 
        }
        ?>

    <?php 
    }