<?php

/**
 * Auto create .css file from Theme Options
 * @author Fox
 * @version 1.0.0
 */
class CMSSuperHeroes_StaticCss
{

    public $scss;
    
    function __construct()
    {
        global $smof_data;

        if(!class_exists('scssc'))
            return;
        
        /* scss */
        $this->scss = new scssc();
        
        /* set paths scss */
        $this->scss->setImportPaths(get_template_directory() . '/assets/scss/');
        /* generate css over time */
        add_action('wp', array($this, 'generate_over_time'));

        /* save option generate css */
        add_action("redux/options/smof_data/saved", array($this,'generate_file'));
    }

    public function generate_over_time(){
        
        global $smof_data;

        if (!empty($smof_data) && $smof_data['dev_mode']){
            $this->generate_file();
        }
    }

    /**
     * generate css file.
     *
     * @since 1.0.0
     */
    public function generate_file()
    {
        global $smof_data, $wp_filesystem;
        
        if (empty($wp_filesystem) || !isset($smof_data))
            return;
            
        $options_scss = get_template_directory() . '/assets/scss/options.scss';

        /* delete files options.scss */
        $wp_filesystem->delete($options_scss);

        /* write options to scss file */
        $wp_filesystem->put_contents($options_scss, $this->css_render(), FS_CHMOD_FILE); // Save it

        /* minimize CSS styles */
        if (!$smof_data['dev_mode'])
            $this->scss->setFormatter('scss_formatter_compressed');

        /* compile scss to css */
        $css = $this->scss_render();

        $file = "static.css";

        $file = get_template_directory() . '/assets/css/' . $file;

        /* delete files static.css */
        $wp_filesystem->delete($file);

        /* write static.css file */
        $wp_filesystem->put_contents($file, $css, FS_CHMOD_FILE); // Save it
    }
    
    /**
     * scss compile
     * 
     * @since 1.0.0
     * @return string
     */
    public function scss_render(){
        /* compile scss to css */
        return $this->scss->compile('@import "master.scss"');
    }
    
    /**
     * main css
     *
     * @since 1.0.0
     * @return string
     */
    public function css_render()
    {
        global $smof_data, $haswell_base;
        
        ob_start();
        
        /* google fonts. */
        if(isset($smof_data['google-font-1'])){
            $haswell_base->setGoogleFont($smof_data['google-font-1'], $smof_data['google-font-selector-1']);
        }
        if(isset($smof_data['google-font-2'])){
            $haswell_base->setGoogleFont($smof_data['google-font-2'], $smof_data['google-font-selector-2']);
        }
         if(isset($smof_data['google-font-3'])){
            $haswell_base->setGoogleFont($smof_data['google-font-3'], $smof_data['google-font-selector-3']);
        }
        /* forward options to scss. */
        
        if(!empty($smof_data['primary_color'])){
            echo '$primary_color:'.esc_attr($smof_data['primary_color']).';';
        }
        if ( !empty($smof_data['link_color']) ) {
            echo '$link_color:'.esc_attr($smof_data['link_color']).';';
        }
        if ( !empty($smof_data['link_color_hover']) ) {
            echo '$link_color_hover:'.esc_attr($smof_data['link_color_hover']).';';
        }

        if ($smof_data['sticky_border_bottom'] != 0) {
            echo '$sticky_border_bottom: block;';
        }
        else {
            echo '$sticky_border_bottom: none;';
        }

        /* style from Theme Option */
        if ( !empty($smof_data['header_height']['height']) ) {
            echo '$header_height:'.esc_attr($smof_data['header_height']['height']).';';
        }
        if ( !empty($smof_data['menu_sticky_height']['height']) ) {
            echo '$header_sticky_height:'.esc_attr($smof_data['menu_sticky_height']['height']).';';
        }
        if ( !empty($smof_data['link_color_hover']) ) {
            echo '$link_color_hover:'.esc_attr($smof_data['link_color_hover']).';';
        }

        /* Menu color */
        if ( !empty($smof_data['menu_color_first_level']) ) {
            echo '$menu_color_first_level:'.esc_attr($smof_data['menu_color_first_level']).';';
        }
        if ( !empty($smof_data['menu_color_first_level_hover']) ) {
            echo '$menu_color_first_level_hover:'.esc_attr($smof_data['menu_color_first_level_hover']).';';
        }
        
        /* Menu sticky color */
        if ( !empty($smof_data['sticky_menu_color_first_level']) ) {
            echo '$sticky_menu_color_first_level:'.esc_attr($smof_data['sticky_menu_color_first_level']).';';
        }
        if ( !empty($smof_data['sticky_menu_color_first_level_hover']) ) {
            echo '$sticky_menu_color_first_level_hover:'.esc_attr($smof_data['sticky_menu_color_first_level_hover']).';';
        }



        return ob_get_clean();
    }
}

new CMSSuperHeroes_StaticCss();