/*
 * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
 *  
 * 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.
 * 
 */

define(function (require) {
    "use strict";

    var React = require("react"),
        Fluxxor = require("fluxxor"),
        Promise = require("bluebird");

    var Designshop = require("jsx!js/jsx/DesignShop"),
        photoshopEvent = require("adapter/lib/photoshopEvent"),
        stores = require("./stores/index"),
        actions = require("./actions/index"),
        descriptor = require("adapter/ps/descriptor"),
        ui = require("adapter/ps/ui");
        
    if (window.__PG_DEBUG__ === true) {
        Promise.longStackTraces();
        Promise.onPossiblyUnhandledRejection(function (err) {
            throw err;
        });
    }

    /**
     * Register event listeners for tool selection change events, and initialize
     * the currently selected tool.
     * 
     * @private
     * @param {Fluxxor} flux
     * @return {Promise}
     */
    var _initTools = function (flux) {
        descriptor.addListener("select", function (event) {
            var target = photoshopEvent.targetOf(event),
                toolIndex = target.indexOf("Tool");

            if (toolIndex > -1) {
                var newTool = target.substr(0, toolIndex);
                flux.actions.tools.select(newTool);
            }
        });

        flux.actions.tools.initialize();
    };

    /**
     * Register event listeners for active and open document change events, and
     * initialize the active and open document lists.
     * 
     * @private
     * @param {Fluxxor} flux
     * @return {Promise}
     */
    var _initDocuments = function (flux) {
        descriptor.addListener("make", function (event) {
            if (photoshopEvent.targetOf(event) === "document") {
                flux.actions.documents.updateDocumentList();
            }
        });
        
        descriptor.addListener("select", function (event) {
            if (photoshopEvent.targetOf(event) === "document") {
                flux.actions.documents.updateDocumentList();
            }
        });
        
        flux.actions.documents.updateDocumentList();
    };

    var _setup = function () {
        var flux = new Fluxxor.Flux(stores, actions),
            props = {
                flux: flux
            };

        // Hide OWL UI
        ui.setClassicChromeVisibility(false);

        _initTools(flux);
        _initDocuments(flux);
        
        React.renderComponent(new Designshop(props), document.body);
    };

    if (document.readyState === "complete") {
        _setup();
    } else {
        window.addEventListener("load", _setup);
    }
});

@@ -1584,24 +1584,62 @@
 scriptor%22),%0A
+        log = require(%22js/util/log%22),%0A
         ui =
@@ -2095,32 +2095,42 @@
 nction (flux) %7B%0A
+        %0A%0A
         descript
@@ -2190,21 +2190,73 @@
    var t
-arget
+oolStore = flux.store(%22tool%22),%0A                psToolName
  = photo
@@ -2306,37 +2306,41 @@
 tool
-Index = target.indexOf(%22Tool%22
+ = toolStore.inferTool(psToolName
 );%0A%0A
@@ -2359,22 +2359,13 @@
 if (
+!
 tool
-Index %3E -1
 ) %7B%0A
@@ -2384,55 +2384,137 @@
     
-var newTool = target.substr(0, toolIndex
+log.warn(%22Failed to infer tool from native tool%22, psToolName);%0A                tool = toolStore.getDefaultTool(
 );%0A    
+        %7D%0A%0A
     
@@ -2551,32 +2551,15 @@
 ect(
-newT
+t
 ool);%0A
-            %7D%0A
     

Fix native tool change event listener