//
// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
// LICENSE: Atomic Game Engine Editor and Tools EULA
// Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
// license information: https://github.com/AtomicGameEngine/AtomicGameEngine
//

import EditorEvents = require("editor/EditorEvents");
import EditorUI = require("ui/EditorUI");
import ModalWindow = require("../ModalWindow");
import ProgressModal = require("../ProgressModal");

import WindowsSettingsWidget = require("./platforms/WindowsSettingsWidget");
import MacSettingsWidget = require("./platforms/MacSettingsWidget");
import AndroidSettingsWidget = require("./platforms/AndroidSettingsWidget");
import IOSSettingsWidget = require("./platforms/IOSSettingsWidget");
import WebSettingsWidget = require("./platforms/WebSettingsWidget");

export interface BuildSettingsWidget {

    storeValues();

}

export class BuildSettingsWindow extends ModalWindow {

    constructor() {

        super();

        this.init("Build Settings", "AtomicEditor/editor/ui/buildsettings.tb.txt");

        this.settingsContainer = <Atomic.UILayout>this.getWidget("settingscontainer");
        var platformcontainer = <Atomic.UILayout>this.getWidget("platformcontainer");
        this.platformIndicator = <Atomic.UISkinImage>this.getWidget("current_platform_indicator");

        var platformSelect = this.platformSelect = new Atomic.UISelectList();

        var platformSource = new Atomic.UISelectItemSource();

        platformSource.addItem(new Atomic.UISelectItem("Windows", "WindowsBuildSettings", "LogoWindows"));
        platformSource.addItem(new Atomic.UISelectItem("Mac", "MacBuildSettings", "LogoMac"));
        platformSource.addItem(new Atomic.UISelectItem("Android", "AndroidBuildSettings", "LogoAndroid"));
        platformSource.addItem(new Atomic.UISelectItem("iOS", "iOSBuildSettings", "LogoIOS"));
        platformSource.addItem(new Atomic.UISelectItem("WebGL", "WebGLBuildSettings", "LogoHTML5"));

        platformSelect.setSource(platformSource);

        var lp = new Atomic.UILayoutParams();

        lp.minWidth = 160;
        lp.minHeight = 370;
        lp.maxHeight = 370;

        platformSelect.layoutParams = lp;
        platformSelect.gravity = Atomic.UI_GRAVITY_ALL;

        platformcontainer.addChild(platformSelect);

        this.addPlatformWidget("WINDOWS", new WindowsSettingsWidget(), "LogoWindows", 0);
        this.addPlatformWidget("MAC", new MacSettingsWidget(), "LogoMac", 1);
        this.addPlatformWidget("ANDROID", new AndroidSettingsWidget(), "LogoAndroid", 2);
        this.addPlatformWidget("IOS", new IOSSettingsWidget(), "LogoIOS", 3);
        this.addPlatformWidget("WEB", new WebSettingsWidget(), "LogoHTML5", 4);

        var currentPlatform = ToolCore.toolSystem.currentPlatform;
        this.setDisplayPlatform(currentPlatform);

        this.platformIndicator.skinBg = this.platformInfo[currentPlatform.name].logo;

        this.resizeToFitContent();
        this.center();

        this.subscribeToEvent("PlatformChanged", (ev: ToolCore.PlatformChangedEvent) => {

            this.platformIndicator.skinBg = this.platformInfo[ev.platform.name].logo;

        });

        this.subscribeToEvent(this, "WidgetEvent", (ev) => this.handleWidgetEvent(ev));
    }

    commitBuildSettings() {

        for (var name in this.platformInfo) {

            <BuildSettingsWidget>(this.platformInfo[name].widget).storeValues();
        }

        ToolCore.toolSystem.project.saveBuildSettings();


    }

    handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {

        if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {

            var toolSystem = ToolCore.toolSystem;

            if (ev.target.id == "cancel") {
                this.hide();
                return true;
            }

            if (ev.target.id == "ok") {

                this.commitBuildSettings();
                this.hide();
                return true;
            }

            if (ev.target.id == "build") {

                this.commitBuildSettings();
                this.hide();
                EditorUI.getModelOps().showBuild();
                return true;

            }

            if (ev.target.id == "set_current_platform") {

                var index = this.platformSelect.value;

                for (var name in this.platformInfo) {

                    var info: { widget: Atomic.UIWidget, index: number, logo: string } = this.platformInfo[name];

                    if (info.index == index) {

                        var platform = toolSystem.getPlatformByName(name);

                        if (platform.platformID == ToolCore.PLATFORMID_IOS) {

                            if (Atomic.platform == "Windows") {

                                var message = "\niOS Deployment requires running the Atomic Editor on MacOSX\n\n";
                                new Atomic.UIMessageWindow(this, "modal_error").show("MacOSX Required", message, Atomic.UI_MESSAGEWINDOW_SETTINGS_OK, true, 640, 260);
                                return true;

                            }

                        }

                        if (!platform.license) {
                            this.hide();
                            EditorUI.getModelOps().showProPlatformWindow();
                            return true;
                        }

                        toolSystem.setCurrentPlatform(platform.platformID);

                        return true;
                    }

                }

                return true;
            }

            if (ev.refid == "WindowsBuildSettings") {
                this.setDisplayPlatform(toolSystem.getPlatformByName("WINDOWS"));
                return true;
            }
            if (ev.refid == "MacBuildSettings") {
                this.setDisplayPlatform(toolSystem.getPlatformByName("MAC"));
                return true;
            }
            if (ev.refid == "AndroidBuildSettings") {
                this.setDisplayPlatform(toolSystem.getPlatformByName("ANDROID"));
                return true;
            }
            if (ev.refid == "iOSBuildSettings") {
                this.setDisplayPlatform(toolSystem.getPlatformByName("IOS"));
                return true;
            }
            if (ev.refid == "WebGLBuildSettings") {
                this.setDisplayPlatform(toolSystem.getPlatformByName("WEB"));
                return true;
            }

        }

        return false;


    }

    addPlatformWidget(name: string, widget: Atomic.UIWidget, logo: string, index: number) {

        this.platformInfo[name] = { widget: widget, index: index, logo: logo };
        this.settingsContainer.addChild(widget);

    }

    setDisplayPlatform(platform: ToolCore.Platform) {

        for (var name in this.platformInfo) {

            this.platformInfo[name].widget.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;

        }

        if (!platform) return;

        var info: { widget: Atomic.UIWidget, index: number, logo: string } = this.platformInfo[platform.name];
        info.widget.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
        if (this.platformSelect.value != info.index)
            this.platformSelect.value = info.index;

    }

    platformInfo: {} = {};

    settingsContainer: Atomic.UILayout;
    platformSelect: Atomic.UISelectList;
    platformIndicator: Atomic.UISkinImage;

}
