/*******************************************************************************
Copyright (c) 2021, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <string>
#include <vector>

#include <netplus/socket.h>
#include <netplus/connection.h>

#include <httppp/http.h>
#include <htmlpp/html.h>

#include <dbpp/database.h>
#include <json-c/json.h>

#include "conf.h"
#include "auth.h"

#pragma once

namespace blogi {
    enum ThemeFilesTypes {
        GENERIC=0,
        TEXT=3,
        IMAGE=4,
        JAVASCRIPT=5
    };

    struct ThemeFiles{
        std::string Path;
        std::string Content;
        std::string Ending;
        std::string Compressed;
        int         Type;
    };

    struct ThemeConfig{
        ThemeConfig(
            const std::string &theme,
            std::vector<std::unique_ptr<dbpp::Database>> &db,
            const Config &cfg
        ) : Theme(theme),TDatabase(db),config(cfg){};
        const std::string                                                                Theme;
        const std::vector<std::unique_ptr<dbpp::Database>> &TDatabase;
        const Config                                                                   &config;
    };

    class Theme {
    public:
        Theme(ThemeConfig &config,bool formated);
        ~Theme();

        void renderPage(const int tid,const char *name,libhtmlpp::HtmlPage &page,libhtmlpp::HtmlElement &index);

        bool Controller(const int tid,libhttppp::HttpRequest &req);

        bool Response(const int tid,libhttppp::HttpRequest &req);

        void printSite(const int tid,libhtmlpp::HtmlString &output,libhtmlpp::HtmlElement *index,const std::string &crrurl,bool login,const char *meta=nullptr,const char *title=nullptr);

        /// Build a <style> block with CSS custom properties from the active theme profile
        void injectThemeColors(const int tid, libhtmlpp::HtmlElement *index);

        /// Return dark + light theme color variables as JSON
        json_object* getThemeColors(const int tid);

        /// Settings UI for theme colors + logo (returns JSON form descriptor)
        json_object* SettingsTheme(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid);

        /// Get the logo URL (configured or theme default)
        std::string getLogoUrl(const int tid) const;

    private:
        ThemeConfig                                        _Config;
        std::vector<ThemeFiles>                             _PublicFiles;
        bool                                               _Formated;
    };
};
