/**
 * Add site styles
 * 
 * @return void
 */
function mob_site_styles()
{
    $basedir = str_replace('http://', '//', mob_get_assets_directory());
    $baseurl = str_replace('http://', '//', mob_get_assets_directory('baseurl'));
    if (file_exists($basedir . 'css/styles.css')) {
        wp_enqueue_style('site-stylesheet', $baseurl . 'css/styles.css', array(MOB_NS . '-stylesheet'));
    }
    if (file_exists($basedir . 'js/scripts.js')) {
        wp_enqueue_script('site-scripts', $baseurl . 'js/scripts.js', array('jquery'));
    }
}
/**
 * Called when settings are saved - compiles the scss
 */
function mob_admin_settings_save()
{
    $url = wp_nonce_url('themes.php?page=mob_options', 'mob_options');
    if (false === ($creds = request_filesystem_credentials($url))) {
        // if we get here, then we don't have credentials yet,
        // but have just produced a form for the user to fill in,
        // so stop processing for now
        return true;
        // stop the normal page form from displaying
    }
    // now we have some credentials, try to get the wp_filesystem running
    if (!WP_Filesystem($creds)) {
        // our credentials were no good, ask the user for them again
        request_filesystem_credentials($url);
        return true;
    }
    if (isset($_GET['settings-updated']) && $_GET['settings-updated']) {
        $messages = array();
        $assets_directory = mob_get_assets_directory();
        $template_directory = get_template_directory();
        $scss_path = $assets_directory . 'css/src/styles.scss';
        $js_path = $assets_directory . 'js/src/scripts.js';
        $iconset = get_option('mob_scss_iconset');
        $scss = '';
        // Include salsa
        $path = mob_admin_get_relative_path($assets_directory . 'css/src/', $template_directory . '/css/src/salsa');
        $scss .= '@import "' . $path . '/salsa";' . PHP_EOL;
        // Include bones mixins
        $path = mob_admin_get_relative_path($assets_directory . 'css/src/', $template_directory . '/css/src/partials');
        $scss .= '@import "' . $path . '/functions";' . PHP_EOL;
        $scss .= '@import "' . $path . '/mixins";' . PHP_EOL;
        // Include an iconset
        if ($iconset) {
            switch ($iconset) {
                case 'font-awesome':
                    $path = mob_admin_get_relative_path($assets_directory . 'css/src/', $template_directory . '/fonts/' . $iconset . '/scss');
                    $scss .= '@import "' . $path . '/' . $iconset . '";' . PHP_EOL;
                    break;
                default:
                    $path = mob_admin_get_relative_path($assets_directory . 'css/src/', $template_directory . '/fonts/' . $iconset);
                    $scss .= '@import "' . $path . '/' . $iconset . '";' . PHP_EOL;
                    break;
            }
        }
        // Collate the media queries
        foreach (array('base' => '', 'mobile' => 481, 'tablet' => 768, 'desktop' => 1030, 'large_desktop' => 1240) as $name => $breakpoint) {
            $option = get_option('mob_scss_' . $name);
            if ($option) {
                if ($breakpoint) {
                    $scss .= '@media only screen and (min-width: ' . $breakpoint . 'px) {';
                    $scss .= $option;
                    $scss .= '}';
                } else {
                    $scss .= $option;
                }
            }
        }
        if (mob_file_read($scss_path) != $scss) {
            if (mob_file_write($scss_path, $scss) !== false) {
                if ($error = mob_admin_compile_scss($assets_directory . 'css/src/', $assets_directory . 'css/', 'scss_formatter_compressed')) {
                    $messages[] = '<span class="icon icon-warning"></span> scss not compiled. ' . $error;
                } else {
                    $messages[] = '<span class="icon icon-checkmark"></span> scss compiled';
                }
            } else {
                $messages[] = '<span class="icon icon-warning"></span> could not save css';
            }
        }
        // Cache the css path as a setting so that we can style the login screen
        update_option('mob_scss_url', mob_get_assets_directory('baseurl') . 'css/styles.css');
        // Now update the Javascript
        $js = get_option('mob_js');
        $previous_version_js = '';
        if (file_exists($js_path)) {
            $previous_version_js = mob_file_read($js_path);
        }
        if ($previous_version_js != $js) {
            try {
                $minified = JSMin::minify($js);
                $messages[] = false === mob_file_write($js_path, $js) || false === mob_file_write($assets_directory . 'js/scripts.js', $minified) ? '<span class="icon icon-cross"></span> js could not be saved' : '<span class="icon icon-checkmark"></span> js saved';
            } catch (Exception $e) {
                $messages[] = '<span class="icon icon-warning"></span> js not compiled. ' . $e->getMessage();
            }
        }
        // Cache the css/js path as a setting so that we can style the login screen
        update_option('mob_scss_url', mob_get_assets_directory('baseurl') . 'css/styles.css');
        update_option('mob_js_url', mob_get_assets_directory('baseurl') . 'js/scripts.js');
        $_GET['settings-updated-message'] = implode(', ', $messages);
    }
}