/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2013-2014 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 */
/*global define: true, graphite: true*/

define([
    'jquery',
    'underscore',
    'backbone',
    'ccweb.files.extract/views/popup/BasePopupView',
    'controllers/UserAccountController',
    'utils/TemplateUtil'
], function ($, _, Backbone, BasePopupView, UserAccountController, TemplateUtil) {
    "use strict";

    var UserSettingsPopupView = BasePopupView.extend({

        className: "user-profile-popup popup",

        userAccountController: null,
        authModel: null,

        events: {
            "click #analyticsOptInCheckBox": "analyticsOptInCheckBoxClick"
        },

        initialize: function (authModel) {
            this.userAccountController = new UserAccountController();
            this.authModel = authModel;

            // events
            graphite.events.on("show-user-settings-popup", this.handleShow, this);
            graphite.events.on("hide-user-settings-popup", this.closePopup, this);

            BasePopupView.prototype.initialize.apply(this, arguments);
        },

        render: function () {
            BasePopupView.prototype.render.apply(this, arguments);

            var contents = this.$el.find(".popup-contents").html(
                TemplateUtil.createTemplate("#user-profile-template")
            );

            return this;
        },

        //------------------------------------------------
        // Handlers
        //------------------------------------------------
        analyticsOptInCheckBoxClick: function () {
            var isChecked = this.$el.find('#analyticsOptInCheckBox').is(':checked');
            this.authModel.setAnalyticsOptIn(isChecked);
        },

        handleShow: function (params) {

            this.userAccountController.getStorageQuota(
                this.getUserStorageQuota_success,
                this.getUserStorageQuota_error,
                this
            );

            if (this.authModel.isAnalyticsOptIn()) {
                this.$el.find('#analyticsOptInCheckBox').prop('checked', true);
            } else {
                this.$el.find('#analyticsOptInCheckBox').prop('checked', false);
            }

            BasePopupView.prototype.handleShow.apply(this, arguments);


        },

        getUserStorageQuota_success: function (response) {
            $("#userQuotaProgress").css("display", "none");
            $("#userQuotaContainer").css("display", "block");

            var storageData = JSON.parse(JSON.parse(response.responseText));

            var quota = parseFloat(storageData.storage_quota, 10).toFixed(1),
                used = parseFloat(storageData.storage_used, 10),
                displayString;

            if (used < 1.0) {
                displayString = ((1073741824 * used) / 1048576).toFixed(2).toString(10);
                displayString = displayString + "MB / ";
            } else {
                displayString = used.toFixed(1).toString(10) + "GB / ";
            }
            displayString = displayString + quota.toString(10) + "GB";
            $("#quotaDisplay").html(displayString);
            $("#usage-bar").css("width", ((used / quota) * 100).toString() + "%");
        },

        getUserStorageQuota_error: function (response) {
            $("#userQuotaProgress").css("display", "none");
            $("#userQuota_error").html("Error retrieving user information");
            if (response.status === 401) {
                graphite.events.trigger("reauthenticate");
            }
        }

    });

    return UserSettingsPopupView;
});