using System;
using System.Text;
using FBPLib;
using System.Threading;

namespace FBPVerbs
{

    [InPort("IN")] 
    public class Tune : Component
    {
        internal static string _copyright =
                "Copyright 2007, 2008, J. Paul Morrison.  At your option, you may copy, " +
                "distribute, or make derivative works under the terms of the Clarified Artistic License, " +
                "based on the Everything Development Company's Artistic License.  A document describing " +
                "this License may be found at http://www.jpaulmorrison.com/fbp/artistic2.htm. " +
                "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";


        IInputPort _inport;

        // make it a non-looper 
        public override void Execute() /* throws Throwable */  {
            //
            // Note: _thread (used by SetPriority) is null before Execute()
            //
            SetPriority(ThreadPriority.Highest);
            Thread.Sleep(750);   // 3/4 sec
            Packet p;
            while ((p = _inport.Receive()) != null)
            {
                string text = p.Content as string;

                string[] parts = text.Split(',');
                string note = parts[0].Trim();
                string duration = parts[1].Trim();
                Console.Beep(Int32.Parse(note), Int32.Parse(duration));
                Thread.Sleep(100);   // 1/10 sec
                Drop(p);
            }
            SetPriority(ThreadPriority.Normal);
        }

        public override Object[] Introspect()
        {
            return new Object[] {
		"discards its input",
		"IN", "input", null,
			"tune"};
        }
        public override void OpenPorts()
        {
            _inport = OpenInput("IN");
            //inport.SetType(Type.GetType("System.Object"));
            
        }

    }
}

