//******************************************************************************************************
// 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.Collections.Generic;
using System.Text;

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

namespace SQS
{
    public class SQSMessage
    {
        public DateTime _timeStamp;
        public string   _machine;
        public string   _queue;
        public string   _payload;
    
        public SQSMessage(string rQ, string message)
        {
            _timeStamp   = DateTime.Now;
            _payload     = message;
            _queue       = rQ;
            _machine     = machineName();
        }
    
        public SQSMessage(string message)
        {
            _timeStamp   = DateTime.Now;
            _payload     = message;
            _queue       = "";
            _machine     = machineName();
        }
    
        public SQSMessage(Amazon.SQS.Model.Message message)
        {
            try
            {
                int len     = message.Body.IndexOf(';');
                string s    = message.Body.Substring(0, len);
                _timeStamp  = DateTime.FromBinary(Convert.ToInt64(s));

                s           = message.Body.Substring(len + 1);
                len         = s.IndexOf(';');
                _machine    = s.Substring(0, len);

                s           = s.Substring(len + 1);
                len         = s.IndexOf(';');
                _queue       = s.Substring(0, len);
            
                _payload     = s.Substring(len + 1);
            }
            catch
            {
                _queue       = null;
                _machine     = null;
                _payload     = null;
            }
        }
    
        public string Serialize()
        {
            StringBuilder sb = new StringBuilder();
            string        t  = _timeStamp.ToBinary().ToString();
        
            //
            // Create serialized string...            
            //
            sb.Append(t);
            sb.Append(";");
            sb.Append(_machine);
            sb.Append(";");
            sb.Append(_queue);
            sb.Append(";");
            sb.Append(_payload);
        
            return sb.ToString();
        }

        private string machineName()
        {
            string m = System.Environment.MachineName;

            if (m.Length > 100) m = m.Substring(0, 100);

            return m;
        }
    }
}
