<?php
namespace UiCore\Blocks;

class Core {
    public function __construct() {
       add_action('uicore_block_styles_updated', [$this, 'write_styles_to_file'],10,2);
       add_action('uicore_block_styles_deleted', [$this, 'delete_styles_file']);
    }

    public static function write_styles_to_file($post_id, $styles) {

        if( !is_array($styles) || empty($styles)) {
            return;
        }
        
        \UiCore\Helper::ensure_assets_manager_exists();

        $upload_dir = wp_upload_dir();
        $base_dir = $upload_dir['basedir'].'/uicore-blocks';
        if (!file_exists($base_dir)) {
            wp_mkdir_p($base_dir);
        }
        foreach ($styles as $name => $css) {
            $minifier = new \MatthiasMullie\Minify\CustomCSS();
            $minifier->add($css);
            $content = $minifier->minify();
            $file = $base_dir."/uicore-".$post_id."-".$name.'.css';
            $fp = fopen($file, 'w');
            fwrite($fp, $content);
            fclose($fp);
        }
    }

    public function delete_styles_file($post_id) {
        $upload_dir = wp_upload_dir();
        $base_dir = $upload_dir['basedir'].'/uicore-blocks';
        if (!file_exists($base_dir)) {
            return;
        }
        $files = glob($base_dir.'/uicore-'.$post_id.'-*.css');
        foreach ($files as $file) {
            unlink($file);
        }
    }
}
new Core();