/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*global define, console, document, tau*/
/*jslint plusplus: true*/

/**
 * Recording format page module
 */

define({
    name: 'views/recordingFormat',
    requires: [
        'core/event',
        'core/template',
        'models/camera'
    ],
    def: function viewsRecordingFormat(req) {
        'use strict';

        var page = null,
            e = req.core.event,
            t = req.core.template,
            camera = req.models.camera,

            formatList = null;

        /**
         * Handles views.settings.show event.
         */
        function show() {
            tau.changePage('#recording-format');
        }

        /**
         * Renders page UI.
         */
        function renderView() {
            var formats = camera.getAvailableRecordingFormats(),
                currentFormat = camera.getRecordingFormat(),
                i = 0,
                len = formats.length,
                content = [],
                format = null,
                checked = false;

            for (i = 0; i < len; i += 1) {
                format = formats[i];
                checked = currentFormat === format;
                content.push(t.get('recordingFormatRow', {
                    format: format,
                    checked: checked
                }));
            }

            formatList.innerHTML = content.join('');
        }

        /**
         * Handles pagebeforeshow event.
         */
        function onPageBeforeShow() {
            renderView();
        }

        /**
         * Handles click event on format list.
         */
        function onFormatListClick() {
            var input = formatList.querySelectorAll('input:checked')[0];

            camera.setRecordingFormat(input.getAttribute('data-format'));
        }

        /**
         * Registers view event listeners.
         */
        function bindEvents() {
            page.addEventListener('pagebeforeshow', onPageBeforeShow);
            formatList.addEventListener('click', onFormatListClick);
        }

        /**
         * Initiates module.
         */
        function init() {
            page = document.getElementById('recording-format');
            formatList = document.getElementById('recording-format-list');
            bindEvents();
        }

        e.listeners({
            'views.settings.show.recordingFormat': show
        });

        return {
            init: init
        };
    }

});
