<?php

function getThemeVersions() {
    return array('prestashop' => _PS_VERSION_);
}

function setThemeHashes($current_theme_path, $data = array()) {
    $hashesfile = $current_theme_path . '/export/hashes.json';
    $hashes_content = file_exists($hashesfile) ? file_get_contents($hashesfile) : '';
    $hashes = json_decode($hashes_content, true);

    foreach ($data as $file => $content) {
        if ('[DELETED]' === $content) {
            if (isset($hashes[$file]))
                unset($hashes[$file]);
        } else {
            $hashes[$file] = $content;
        }
    }

    file_put_contents($hashesfile, json_encode($hashes));
}

function getThemeHashes($current_theme_path) {
    $hashesfile = $current_theme_path . '/export/hashes.json';
    return json_decode(file_exists($hashesfile) ? file_get_contents($hashesfile) : '{}');
}

function setThemeCache($current_theme_path, $data = array()) {
    $cachefile = $current_theme_path . '/export/cache.json';
    $cache_content = file_exists($cachefile) ? file_get_contents($cachefile) : '';
    $cache = json_decode($cache_content, true);

    if (!is_array($data))
        return;

    foreach ($data as $control => $storage) {
        if (!is_array($storage))
            continue;

        foreach ($storage as $file => $content) {
            if ('[DELETED]' === $content) {
                if (isset($cache[$control]) && isset($cache[$control][$file]))
                    unset($cache[$control][$file]);
            } else {
                $cache[$control][$file] = $content;
            }
        }
    }

    file_put_contents($cachefile, json_encode($cache));
}

function getThemeCache($current_theme_path) {
    $cachefile = $current_theme_path . '/export/cache.json';
    return json_decode(file_exists($cachefile) ? file_get_contents($cachefile) : '{}');
}

function getPreviewChangedFiles($current_theme_path) {
    $changed_files_file = $current_theme_path . '/export/preview_changed_files.json';
    if (!is_file($changed_files_file))
        return null;
    $changed_files_file_content = file_get_contents($changed_files_file);
    if (!$changed_files_file_content)
        return null;
    $changed_files = json_decode($changed_files_file_content, true);
    return $changed_files;
}

function setPreviewChangedFiles($base_theme_path, $preview_theme_path, $changed_files) {
    $changed_files_file = $base_theme_path . '/export/preview_changed_files.json';
    $_changed_files = $changed_files;
    $changed_files = array();
    foreach($_changed_files as $path) {
        $file = FilesHelper::normalize_path($path);
        if (substr($file, 0, strlen($preview_theme_path)) == $preview_theme_path)
            $changed_files[] = substr($file, strlen($preview_theme_path));
        else
            $changed_files[] = $file;
    }
    $changed_files_file_content = json_encode(array_unique($changed_files));
    file_put_contents($changed_files_file, $changed_files_file_content);
}

function saveProject($themeName, $projectData, $domain) {
    $projectPath = getProjectPath($themeName);
    $saveData = array(
        'projectData' => $projectData,
        'domain' => $domain
    );
    FilesHelper::write($projectPath, json_encode($saveData));
}

function getProject($themeName) {
    $projectPath = getProjectPath($themeName);
    return json_decode(file_exists($projectPath) ? file_get_contents($projectPath) : '{}');
}

function getThemeTemplates($baseUrl, $themeName)
{
    $templates = array();

    $baseUrl = $baseUrl . '?theme_name=' . $themeName;
    $templates['home'] = $baseUrl;
    $templates['default'] = $baseUrl . '&controller=address';

    $categoryId = Designer::getFirstChildCategory();
    $categoryPath = $categoryId > 0 ? "&id_category=$categoryId&controller=category" : "''";
    $templates['products'] = $baseUrl . $categoryPath;

    $productPath = '&id_product=1&controller=product'; // TODO: do the same as with categoryID
    $templates['productOverview'] = $baseUrl . $productPath;

    $cmsPagePath = '&id_cms=1&controller=cms&id_lang=1'; // TODO: do the same as with categoryID
    $templates['pageTemplate'] = $baseUrl . $cmsPagePath;

    $shoppingCart = '&controller=order';
    $templates['shoppingCartTemplate'] = $baseUrl . $shoppingCart;

    $notFound = '&controller=404';
    $templates['template404'] = $baseUrl . $notFound;

    return $templates;
}

/*
 * Gets Urls for PrestaShop CMS
 */
function getPSUrls($baseUrl, $themeName, $adminDir, $token, $domain)
{
    return array(
        'admin' => $baseUrl . $adminDir
                    . '/index.php?controller=AdminAjax'
                    . '&token=' . $token
                    . '&domain=' . $domain,
        'preview' => $baseUrl . '?theme_name=' . $themeName
    );
}