//******************************************************************************************************
// Copyright 2008 Amazon Technologies, Inc.  
// 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://aws.amazon.com/apache2.0  This file 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. 
//******************************************************************************************************using System;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;

//
// References for SQS operations...
//
using Amazon.SQS;
using Amazon.SQS.Mock;
using Amazon.SQS.Model;
using SQS_Client;

namespace SQS
{
	[DefaultEvent("requestEvent")]
    
    /// <summary>
	/// Summary description for SQSControl.
	/// </summary>
    public class Worker : SQSManager
    {    
        #region Events
        /// <summary>
		/// A delegate that allows subscribing methods to process message.
		/// </summary>
		public delegate bool ProcessEvent(Worker worker, SQSMessage message);
		/// <summary>
		/// </summary>
		/// <remarks>
		/// </remarks>
		[	Bindable( true ), Category( "Events" ), 
		Description( "Event to process incoming requests" )]
		public event ProcessEvent requestEvent = null;
		/// <summary>
		/// </summary>
        #endregion Events    
        #region Public Methods
    
        public string PutResponse(SQSMessage message, string response)
        {
            SQSMessage r = new SQSMessage(response);
            
            //
            // Enqueue message
            //
            return Client.SendMessage(message._queue, r.Serialize());
        }
        
        #endregion Public Methods
        #region Private Methods
    
        protected override bool MessageReceived(Amazon.SQS.Model.Message msg)
        {
            SQSMessage  request = new SQSMessage(msg);
            bool        result  = false;
        
            //
            // Does the event live in a GUI object?
            //
            if (requestEvent.Target is System.Windows.Forms.Form)
            {                   
                //
                // GUI objects need to be called differently - as they are single threaded
                //
                System.Windows.Forms.Form parent = (System.Windows.Forms.Form)requestEvent.Target;
            
                //
                // Allocate an object array for parameters and Invoke the event method
                //
                object[] p  = new object[2];
                p[0]        = this;
                p[1]        = request;
                result      = (bool)parent.Invoke(requestEvent, p);
            }
            else 
            {
                //
                // Otherwise - we assume the method is thread safe...
                //
                result = requestEvent(this, request);
            }
        
            return result;
        }
        #endregion Private Methods
    }
}
