/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, bitwise: true */
/*global define: true, graphite: true, Image: true, window: true*/

define([
    "jquery",
    "underscore",
    "backbone",
    "ccweb.files.extract/models/UserSettingsModel",
    "ccweb.files.extract/models/PSDSettingsModel"
], function ($, _, Backbone, UserSettingsModel, PSDSettingsModel) {
    "use strict";
    var PreferenceController = Backbone.Model.extend({
        
        defaults: {
            preferredFontUnit: "px",
            preferredFileExtension: "png",
            baseFontSizeValue: 16,
            preferredImageQuality: 80
        },
        
        updatePreferences: function (result) {
            this.set("preferredFontUnit", result.preferredFontUnit);
            this.set("preferredFileExtension", this.identifyFileExtensionFromPreference(result.preferredFileExtension));
            this.set("baseFontSizeValue", result.baseFontSizeValue);
            this.set("preferredImageQuality", result.preferredImageQuality);
            this.propagatePreferences();
        },
        
        identifyFileExtensionFromPreference: function (prefString) {
            if (prefString.indexOf("jpg") !== -1 || prefString.indexOf("jpeg") !== -1) {
                return "jpg";
            } else if (prefString.indexOf("png8") !== -1) {
                return "png8";
            } else {
                return "png32";
            }
        },
        
        propagatePreferences: function () {
            var preferredFontUnits = this.get("preferredFontUnit");
            if (preferredFontUnits && preferredFontUnits !== UserSettingsModel.get('preferredFontUnits')) {
                UserSettingsModel.set('preferredFontUnits', preferredFontUnits);
                graphite.events.trigger("updateFontCSS");
                graphite.events.trigger("preferredFontUnitsChanged", {preferredUnits: preferredFontUnits});
            }
            
            var baseFontSizeValue = this.get("baseFontSizeValue"),
                baseFontSizeUnits = "px";
            if (baseFontSizeValue) {
                if (baseFontSizeValue !== PSDSettingsModel.get("baseFontSizeValue")) {
                    PSDSettingsModel.set('baseFontSizeValue', baseFontSizeValue);
                    if (baseFontSizeUnits !== PSDSettingsModel.get("baseFontSizeUnits")) {
                        PSDSettingsModel.set('baseFontSizeUnits', baseFontSizeUnits);
                    }
                    graphite.events.trigger("updateFontCSS");
                    graphite.events.trigger("baseFontSizeChanged", {newBaseUnits: baseFontSizeValue + baseFontSizeUnits});
                }
            }
            this.trigger("change:Preferences");
        }
        
    });

    return new PreferenceController();

});
