<?php

class Edocman_Lite_Helper {
    
    public static function get_config($key, $default = '') {
        $database = new Edocman_Lite_Database();
        $value = $database->get_config($key);
        
        return $value !== null ? $value : $default;
    }

    public static function format_file_size($bytes) {
        if ($bytes >= 1073741824) {
            $bytes = number_format($bytes / 1073741824, 2) . ' GB';
        } elseif ($bytes >= 1048576) {
            $bytes = number_format($bytes / 1048576, 2) . ' MB';
        } elseif ($bytes >= 1024) {
            $bytes = number_format($bytes / 1024, 2) . ' KB';
        } elseif ($bytes > 1) {
            $bytes = $bytes . ' bytes';
        } elseif ($bytes == 1) {
            $bytes = $bytes . ' byte';
        } else {
            $bytes = '0 bytes';
        }

        return $bytes;
    }

    public static function sanitize_filename($filename) {
        // Remove any special characters
        $filename = sanitize_file_name($filename);
        
        // Generate unique filename
        $info = pathinfo($filename);
        $ext = isset($info['extension']) ? '.' . $info['extension'] : '';
        $name = basename($filename, $ext);
        
        return $name . '_' . time() . $ext;
    }

    public static function get_file_extension($filename) {
        $info = pathinfo($filename);
        return isset($info['extension']) ? strtolower($info['extension']) : '';
    }

    public static function get_file_icon($filename) {
        $ext = self::get_file_extension($filename);
        
        $icons = array(
            'pdf' => 'dashicons-pdf',
            'doc' => 'dashicons-media-document',
            'docx' => 'dashicons-media-document',
            'xls' => 'dashicons-media-spreadsheet',
            'xlsx' => 'dashicons-media-spreadsheet',
            'ppt' => 'dashicons-media-document',
            'pptx' => 'dashicons-media-document',
            'txt' => 'dashicons-media-text',
            'zip' => 'dashicons-media-archive',
            'rar' => 'dashicons-media-archive',
            'jpg' => 'dashicons-format-image',
            'jpeg' => 'dashicons-format-image',
            'png' => 'dashicons-format-image',
            'gif' => 'dashicons-format-image',
            'mp4' => 'dashicons-format-video',
            'avi' => 'dashicons-format-video',
            'mp3' => 'dashicons-format-audio',
        );
        
        return isset($icons[$ext]) ? $icons[$ext] : 'dashicons-media-default';
    }

    public static function create_alias($title) {
        $alias = sanitize_title($title);
        return $alias;
    }

    public static function get_category_tree($parent_id = 0, $published_only = false) {
        $database = new Edocman_Lite_Database();
        $categories = $database->get_categories($parent_id, $published_only);
        
        $tree = array();
        foreach ($categories as $category) {
            $tree[] = array(
                'category' => $category,
                'children' => self::get_category_tree($category->id, $published_only)
            );
        }
        
        return $tree;
    }
}
