#   Copyright (c) 2006-2007 Open Source Applications Foundation
#   Copyright (c) 2008-2009 Mikeal Rogers <mikeal.rogers@gmail.com>
#
#   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.

"""This module provides the communication and management between the various 
server interfaces and the browser's js interface"""

import copy, os, sys
try:
    import json as simplejson
except:
    import simplejson
import logging
from uuid import uuid1
import windmill
from time import sleep

test_results_logger = logging.getLogger('test_results')

class ControllerQueue(object):
    
    def __init__(self, command_resolution_suite, test_resolution_suite): 
        self.resolution_suites = {'test': test_resolution_suite,
                                  'command': command_resolution_suite}
        self.queue = []
        self.current_suite = None
        
    def add_command(self, command, suite_name=None):
        """Add Command to the controller queue"""
        if suite_name is None and not command.get('suite_name'):
            suite_name = self.current_suite
        command['suite_name'] = suite_name
        
        if command['params'].get('priority', None):
            priority = command['params'].pop('priority')
        else:
            priority = None
        
        command['type'] = 'command'
            
        if type(priority) is int:
            self.queue.insert(priority, command)
        else:
            self.queue.append(command)
    
    def add_test(self, test, suite_name=None):
        if suite_name is None and not test.get('suite_name'):
            suite_name = self.current_suite
        test['suite_name'] = suite_name
        
        test['type'] = 'test'
        
        self.queue.append(test)
        
    def start_suite(self, suite_name):
        self.current_suite = suite_name

    def stop_suite(self):
        self.current_suite = None
        
    # def command(self, command):        
    #     self.command_queue.insert(0, command)
        
    def next_action(self):