namespace mf {


class SourceAssetFactory {
public:
	// This factory is a little bit different.
	typedef SourceAssetPtr (*fnCreateHandler)(bool& handled, SourceAsset::AssetType type, const Json::Value& data);
	typedef std::vector<fnCreateHandler> HandlerContainer;
	SourceAssetFactory() { }
    
    // Class cannot be subclassed
	~SourceAssetFactory() { }
	
	bool registerHandler(fnCreateHandler handler) {
		for (auto iter = _handlers.begin(); iter != _handlers.end(); ++iter) {
			if (*iter == handler)
				return false;
		}
        
		_handlers.push_back(handler);
        
		return true;
	}
	
    // Create an asset of a specific type.  "data" is the dictionary used
    // to initialize the asset.  It should contain well-known keys defined in SourceAsset.hpp
	// TODO: If we only have specific types for each platfrom, we can use a builder pattern
	// for those items.
	SourceAssetPtr create(SourceAsset::AssetType type, const Json::Value& data) {
		bool handled = false;
		SourceAssetPtr asset;

        for (auto iter = _handlers.begin(); iter != _handlers.end() && !handled; ++iter) {
			fnCreateHandler aHandler = *iter;
			asset = aHandler(handled, type, data);
		}
        
		return asset;	
	}

	size_t getCount() const { return _handlers.size(); }
	
private:
	HandlerContainer _handlers;
};

}

#endif

// MIT License
//
// Copyright (c) 2016 Caring Labs, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
