#ifndef HEADER_FILTER
#define HEADER_FILTER

#include <stdlib.h>
#include <vector>
#include "../../source/effects/XFilter.hpp"
#include "../../source/util/XFilterParser.hpp"
#include "XLogger.hpp"

USING_NS_GAODING_MEDIA

class Param {
    private:
        XParam param;
        std::string name;
    public:
        Param() { }

        Param(std::string paramName, XParam xParam) {
            param = xParam;
            name = paramName;
        }

        ~Param() { }

        int getType() {
            return (int)(param.type);
        }

        long getArray() {
            return reinterpret_cast<long>(param.array);
        }

        long getMin() {
            return reinterpret_cast<long>(param.min);
        }

        long getMax() {
            return reinterpret_cast<long>(param.max);
        }

        int getDimension() {
            return param.dimension;
        }

        int getCount() {
            return param.count;
        }

        std::string getName() {
            return name;
        }
};

class BFilter {
    private:
        std::shared_ptr<XFilter> filter;
        std::vector<Param*> params;

        void initParams() {
            for(auto iter: filter->getParams()) {
                std::string name = iter.first;
                Param* param = new Param(name, iter.second);
                params.insert(params.end(), param);
            }
        }

    public:
        BFilter(std::shared_ptr<XFilter> _filter) {
            filter = _filter;
            initParams();
        }

        BFilter(XFilterType type) {
            filter = std::make_shared<XFilter>(type);
            initParams();
        }

        BFilter(const std::string vs, const std::string fs, XFilterType type, std::string name) {
            filter = std::make_shared<XFilter>(vs, fs, type, name);
            initParams();
        }

        ~BFilter() {
            std::vector<Param*>().swap(params);
            filter.reset();
        }

        static BFilter* createByFilterType(int type) {
            return new BFilter(static_cast<XFilterType>(type));
        }

        static BFilter* createByShader(std::string vs, std::string fs, int type, std::string name) {
            return new BFilter(vs, fs, static_cast<XFilterType>(type), name);
        }

        static std::vector<BFilter*> createByConfig(std::string config, std::string shaderDir, int type, bool dependOnVersion) {
            XFilterParser::Result result = XFilterParser::parseFromConfig(config, shaderDir, static_cast<XParserType>(type), dependOnVersion);
            std::vector<std::shared_ptr<XFilter>> xFilters = result.filters;
            std::vector<BFilter*> filters;
            for(std::shared_ptr<XFilter> xFilter: xFilters) {
                XLogger::info("filter name: {}", xFilter->getName());
                BFilter* filter = new BFilter(xFilter);
                filters.push_back(filter);
            }
            return filters;
        }

        std::shared_ptr<XFilter> getInstance() {
            return filter;
        };

        void updateValueBool(std::string name, bool x) {
            filter->updateValueBool(name, x);
        }

        void updateValue1fv(std::string name, long arrayPtr, int count, float min = -1.0f, float max = 1.0f) {
            filter->updateValue1fv(name, reinterpret_cast<float *>(arrayPtr), count, min, max);
        }

        void updateValue1iv(std::string name, long arrayPtr, int count, int min = 0, int max = 255) {
            filter->updateValue1iv(name, reinterpret_cast<int *>(arrayPtr), count, min, max);
        }

        std::vector<Param*> getParams() {
            return params;
        }

        std::string getName() {
            return filter->getName();
        }

        int getType() {
            return static_cast<int>(filter->getType());
        }
};

#endif
