<?php
defined('BASEPATH') or exit('No direct script access allowed');

hooks()->add_action('app_admin_head', 'perfex_office_theme_head_component');
hooks()->add_action('app_admin_footer', 'perfex_office_theme_footer_js__component');
hooks()->add_action('admin_init', 'office_theme_settings_tab');


// Check if customers theme is enabled
if (get_option('perfex_office_theme_customers') == '1') {
    hooks()->add_action('app_admin_authentication_head', 'perfex_office_theme_customer_head');
    hooks()->add_action('app_external_form_head', 'app_client_office_head_custom_includes');
    hooks()->add_action('app_customers_head', 'perfex_office_theme_includes');
    hooks()->add_action('app_customers_footer', 'perfex_office_theme_customers_footer_js__component');
}


/**
 * [office_theme_settings_tab net menu item in setup->settings]
 *
 * @return void
 */
function office_theme_settings_tab()
{
    $CI = &get_instance();
    $CI->app_tabs->add_settings_tab('admin-theme-settings', [
        'name'     => '' . _l('perfex_office_theme_settings_first') . '',
        'view'     => 'perfex_office_theme/perfex_office_theme_settings',
        'position' => 48,
    ]);
}

/**
 * Theme customers login includes
 *
 * @return stylesheet / script
 */
function perfex_office_theme_customer_head()
{
    echo '<link href="' . base_url('modules/perfex_office_theme/assets/css/staff_login_styles.css') . '"  rel="stylesheet" type="text/css" >';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/sign_in.js') . '"></script>';
}


/**
 * Theme clients head includes
 *
 * @return stylesheet
 */
function perfex_office_theme_includes()
{
    echo '<link href="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/css/clients/clients.css') . '"  rel="stylesheet" type="text/css" >';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/third-party/nanobar.js') . '"></script>';
}

/**
 * Injects theme CSS
 *
 * @return null
 */
function perfex_office_theme_head_component()
{
    // Prevent "flash of dark theme" (FOUC) by setting the theme BEFORE CSS loads.
    // Reads user preference from localStorage, otherwise falls back to system preference.
    echo '<script>(function(){try{var t=localStorage.getItem("perfex_office_theme");if(!t){t=(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches)?"dark":"light";}document.documentElement.setAttribute("data-theme",t);}catch(e){}})();</script>';
    // Load CSS Variables first (before theme styles)
    echo '<link href="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/css/theme-variables.css') . '"  rel="stylesheet" type="text/css" >';
    echo '<link href="' . base_url('modules/perfex_office_theme/assets/css/theme_styles.css') . '"  rel="stylesheet" type="text/css" >';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/third-party/nanobar.js') . '"></script>';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/third-party/waves076.min.js') . '"></script>';
}

function app_client_office_head_custom_includes()
{
    echo '<link href="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/css/clients/clients.css') . '"  rel="stylesheet" type="text/css" >';
    echo '<link href="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/css/clients/common.css') . '"  rel="stylesheet" type="text/css" >';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/third-party/waves076.min.js') . '"></script>';
}

/**
 * Injects staff theme js components in footer
 *
 * @return null
 */
function perfex_office_theme_footer_js__component()
{
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/main.js') . '"></script>';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/theme-switcher.js') . '"></script>';
}


/**
 * Injects customer theme js components in footer
 *
 * @return null
 */
function perfex_office_theme_customers_footer_js__component()
{
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/third-party/waves076.min.js') . '"></script>';
    echo '<script src="' . module_dir_url(PERFEX_OFFICE_THEME, 'assets/js/clients.js') . '"></script>';
}


/**
 * Changes sidebar menu icons
 *
 * @return array
 */
hooks()->add_filter('sidebar_menu_items', 'perfex_office_change_sidebar_icons');

function perfex_office_change_sidebar_icons($items)
{
    foreach ($items as $id => $item) {
        if ($id === 'dashboard') {
            $items[$id]['icon'] = 'fa fa-desktop';
        }
        if ($id === 'customers') {
            $items[$id]['icon'] = 'fa fa-id-card';
        }
        if ($id === 'sales') {
            $items[$id]['icon'] = 'fa fa-university';
        }
        if ($id === 'subscriptions') {
            $items[$id]['icon'] = 'fa-brands fa-cc-mastercard';
        }
        if ($id === 'contracts') {
            $items[$id]['icon'] = 'fa fa-clipboard';
        }
        if ($id === 'projects') {
            $items[$id]['icon'] = 'fa fa-indent';
        }
        if ($id === 'tasks') {
            $items[$id]['icon'] = 'fa fa-th';
        }
        if ($id === 'support') {
            $items[$id]['icon'] = 'fa fa-users';
        }
        if ($id === 'reports') {
            $items[$id]['icon'] = 'fa fa-pie-chart';
        }
        if ($id === 'knowledge-base') {
            $items[$id]['icon'] = 'fa fa-folder-open';
        }
        if ($id === 'leads') {
            $items[$id]['icon'] = 'fa fa-user-secret';
        }
    }
    return $items;
}

/**
 * Changes task filters color and background
 *
 * @return array
 */
hooks()->add_action('app_init', 'initialize_theme_task_filters');

/**
 * Changes system favorite colors
 *
 * @return array
 */
hooks()->add_filter('system_favourite_colors', 'system_colors_theme_hook');

function system_colors_theme_hook($colors)
{
    foreach ($colors as $key => $color) {
        if ($color == '#ff2d42') {
            $colors[$key] = '#ff0019';
        }
        if ($color == '#28B8DA') {
            $colors[$key] = '#5d78ff';
        }
        if ($color == '#03a9f4') {
            $colors[$key] = '#2a44c5';
        }
        if ($color == '#757575') {
            $colors[$key] = '#595959';
        }
        if ($color == '#8e24aa') {
            $colors[$key] = '#cc0099';
        }
        if ($color == '#d81b60') {
            $colors[$key] = '#8533ff';
        }
        if ($color == '#0288d1') {
            $colors[$key] = '#3333ff';
        }
        if ($color == '#7cb342') {
            $colors[$key] = '#2eb82e';
        }
        if ($color == '#fb8c00') {
            $colors[$key] = '#e67e00';
        }
        if ($color == '#84C529') {
            $colors[$key] = '#71a923';
        }
        if ($color == '#fb3b3b') {
            $colors[$key] = '#fa1e1e';
        }
    }

    return $colors;
}

function initialize_theme_task_filters()
{
    hooks()->add_filter('before_get_task_statuses', 'dashboard_label_task_colors');
}


function dashboard_label_task_colors($statuses)
{
    $CI = &get_instance();

    if (!class_exists('tasks_model', false)) {
        $CI->load->model('tasks_model');
    }

    foreach ($statuses as $key => $status) {
        $id = $status['id'];
        switch ($id) {
            case $id == Tasks_model::STATUS_NOT_STARTED:
                $statuses[$key]['color'] = '#ffb822';
                break;
            case $id == Tasks_model::STATUS_AWAITING_FEEDBACK:
                $statuses[$key]['color'] = '#0abb87';
                break;
            case $id == Tasks_model::STATUS_IN_PROGRESS:
                $statuses[$key]['color'] = '#5d78ff';
                break;
            case $id == Tasks_model::STATUS_COMPLETE:
                $statuses[$key]['color'] = '#84c529';
                break;
        }
    }
    return $statuses;
}

/**
 * Changes project filters color
 *
 * @return array
 */
hooks()->add_action('app_init', 'initialize_theme_project_filters');

function initialize_theme_project_filters()
{
    hooks()->add_filter('before_get_project_statuses', 'dashboard_label_project_colors');
}

function dashboard_label_project_colors($statuses)
{
    foreach ($statuses as $key => $status) {
        $id = $status['id'];

        switch ($id) {
            case $id == 3:
                $statuses[$key]['color'] = '#ffb822';
                break;
            case $id == 4:
                $statuses[$key]['color'] = '#0abb87';
                break;
            case $id == 1:
                $statuses[$key]['color'] = '#777777';
                break;
            case $id == 2:
                $statuses[$key]['color'] = '#5d78ff';
                break;
            case $id == 5:
                $statuses[$key]['color'] = '#fa1e1e';
                break;
        }
    }
    return $statuses;
}

/**
 * Helper function to convert hex colors #333333 to rgba() colors
 *
 * @return array
 */
function hexToRgb($hex, $alpha = false)
{
    $hex = str_replace('#', '', $hex);
    $length = strlen($hex);
    $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
    $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
    $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
    if ($alpha) {
        $rgb['a'] = $alpha;
    }
    return $rgb;
}

/**
 * Get theme color by name
 * Returns CSS variable compatible color value
 *
 * @param string $color_name Color name (primary, success, danger, warning, info)
 * @param string $variant Variant (dark, light, lighter, lightest)
 * @param string $default Default color if not found
 * @return string
 */
function get_theme_color($color_name, $variant = '', $default = '')
{
    $colors = [
        'primary' => [
            '' => '#60a5fa',
            'dark' => '#3b82f6',
            'darker' => '#2563eb',
            'light' => '#93c5fd',
            'lighter' => '#dbeafe',
            'lightest' => '#eff6ff',
        ],
        'success' => [
            '' => '#10b981',
            'dark' => '#059669',
            'light' => '#34d399',
            'lighter' => '#a7f3d0',
            'lightest' => '#d1fae5',
        ],
        'danger' => [
            '' => '#f43f5e',
            'dark' => '#e11d48',
            'light' => '#fb7185',
            'lighter' => '#fda4af',
            'lightest' => '#ffe4e6',
        ],
        'warning' => [
            '' => '#f59e0b',
            'dark' => '#d97706',
            'light' => '#fbbf24',
            'lighter' => '#fde68a',
            'lightest' => '#fef3c7',
        ],
        'info' => [
            '' => '#06b6d4',
            'dark' => '#0891b2',
            'light' => '#22d3ee',
            'lighter' => '#a5f3fc',
            'lightest' => '#cffafe',
        ],
    ];

    if (isset($colors[$color_name])) {
        $color_variants = $colors[$color_name];
        if (isset($color_variants[$variant])) {
            return $color_variants[$variant];
        }
        if (isset($color_variants[''])) {
            return $color_variants[''];
        }
    }

    return $default ?: '#000000';
}

/**
 * Get theme color mapping for status colors
 * Maps old status colors to new theme colors
 *
 * @param string $old_color Old color hex code
 * @return array New color information
 */
function get_theme_status_color($old_color)
{
    $mappings = [
        '#84c529' => [
            'color' => get_theme_color('success'),
            'background' => 'rgba(10, 187, 135, .1)',
            'border' => '#e6f8f3',
        ],
        '#ff2d42' => [
            'color' => get_theme_color('danger', 'light'),
            'background' => 'rgba(253,57,122,.1)',
            'border' => 'rgb(255, 235, 241)',
        ],
        '#0000ff' => [
            'color' => get_theme_color('primary'),
            'background' => 'rgb(247, 248, 250)',
            'border' => 'rgb(247, 248, 250)',
        ],
        '#03a9f4' => [
            'color' => '#2a8db9',
            'background' => 'rgba(11, 165, 239, 0.17)',
            'border' => 'rgb(213, 240, 253)',
        ],
        '#c0c0c0' => [
            'color' => '#8c8c8c',
            'background' => 'rgba(202, 202, 202, 0.38)',
            'border' => 'rgb(235, 235, 235)',
        ],
    ];

    return isset($mappings[$old_color]) ? $mappings[$old_color] : [
        'color' => $old_color,
        'background' => 'rgba(0, 0, 0, 0.1)',
        'border' => 'transparent',
    ];
}
