/**
 * Window icon support.
 *
 * Copyright: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
 * Authors: $(LINK2 http://cattermole.co.nz, Richard Andrew Cattermole)
 */
module cf.spew.ui.window.features.icon;
import cf.spew.ui.window.defs;
import devisualization.image : ImageStorage;
import std.experimental.color : RGBA8;
import devisualization.util.core.memory.managed;

interface Have_Icon {
    Feature_Icon __getFeatureIcon();
}

interface Feature_Icon {
    @property {
        ImageStorage!RGBA8 getIcon();
        void setIcon(ImageStorage!RGBA8);
    }
}

@property {
    /// Sets the icon on a window[creator] if capable
    void icon(T)(managed!T self, ImageStorage!RGBA8 to)
            if (is(T : IWindow) || is(T : IWindowCreator)) {
        if (self.capableOfWindowIcon) {
            (cast(managed!Have_Icon)self).__getFeatureIcon().setIcon(to);
        }
    }

    void icon(T)(managed!T self, ImageStorage!RGBA8 to)
            if (!(is(T : IWindow) || is(T : IWindowCreator))) {
        static assert(0,
                "I do not know how to handle " ~ T.stringof ~
                " I can only use IWindow or IWindowCreator.");
    }

    /// Retrives the window icon if capable or null if not
    managed!(ImageStorage!RGBA8) icon(T)(managed!T self)
            if (is(T : IWindow) || is(T : IWindowCreator)) {
        if (!self.capableOfWindowIcon)
            return (managed!(ImageStorage!RGBA8)).init;
        else {
            auto ret = (cast(managed!Have_Icon)self).__getFeatureIcon().getIcon();
            return managed!(ImageStorage!RGBA8)(ret, managers(), self.allocator);
        }
    }

    managed!(ImageStorage!RGBA8) icon(T)(managed!T self)
            if (!(is(T : IWindow) || is(T : IWindowCreator))) {
        static assert(0,
                "I do not know how to handle " ~ T.stringof ~
                " I can only use IWindow or IWindowCreator.");
    }

    /**
	 * Does the given window[creator] support icons?
	 *
	 * Params:
	 * 		self	=	The window[creator] instance
	 *
	 * Returns:
	 * 		If the window[creator] supports having an icon
	 */
    bool capableOfWindowIcon(T)(managed!T self)
            if (is(T : IWindow) || is(T : IWindowCreator)) {
        if (self is null)
            return false;
        else {
            auto ss = cast(managed!Have_Icon)self;
            return ss !is null && ss.__getFeatureIcon() !is null;
        }
    }

    bool capableOfWindowIcon(T)(managed!T self)
            if (!(is(T : IWindow) || is(T : IWindowCreator))) {
        static assert(0,
                "I do not know how to handle " ~ T.stringof ~
                " I can only use IWindow, IWindowCreator types.");
    }
}
