/**
 *    Copyright 2006 - 2011 Opera Software ASA
 *
 *    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.
 *
 **/


window.cls || (window.cls = {});

/**
  * @constructor 
  * @extends ViewBase
  */

cls.ThreadsView = function(id, name, container_class)
{
  var self = this;
  var __container = null;
  var cursor = 0;
  const INDENT = ' ', NL = '\n';

  this.createView = function(container)
  {
    var threads = runtimes.getThreads();
    if (!__container || !container.firstChild)
    {
      container.innerHTML = '';
      __container = container.render(['div', ['pre'], 'class', 'padding']).firstElementChild;
    }
    __container.textContent += threads.slice(cursor).join('\n') + '\n';
    cursor = threads.length;
    container.scrollTop = container.scrollHeight;
  }

  this.ondestroy = function()
  {
    __container = null;
    cursor = 0;
  }

  this.resetCursor = function()
  {
    cursor = 0;
  }

  this.exportLog = function()
  {
  }





  this.init(id, name, container_class);
}

cls.ThreadsView.create_ui_widgets = function()
{

  new Settings
  (
    // id
    'threads', 
    // key-value map
    {
      'log-threads': false
    }, 
    // key-label map
    {
      'log-threads': ui_strings.S_BUTTON_LABEL_LOG_THREADS
    },
    // settings map
    {
      checkboxes:
      [
        'log-threads'
      ]
    },
    null,
    "script"
  );

  new ToolbarConfig
  (
    'threads',
    [
      {
        handler: 'threads-clear-log',
        title: ui_strings.S_BUTTON_LABEL_CLEAR_LOG
      },
      {
        handler: 'threads-export-log',
        title: ui_strings.S_BUTTON_LABEL_EXPORT_LOG
      }
    ]
  );

  // button handlers
  eventHandlers.click['threads-clear-log'] = function(event, target, container)
  {
    runtimes.clearThreadLog();
    views.threads.resetCursor();
    if( container && container.firstChild )
    {
      container.firstChild.innerHTML = '';
    }
    
  }

  eventHandlers.click['threads-export-log'] = function(event, target, container)
  {
    views.threads.exportLog();
  }

}

