/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-02-2010 02:32:21pm
Filepath: scripts\_custom\engines\dawns music box\MusicBox.cs
Lines of code: 322
***********************************************************/


using System;
using System.Collections;
using Server;
using Server.Targeting;
using Server.Gumps;
using Server.ContextMenus;
using Server.Network;
using Server.Mobiles;
using Server.Multis;
using Server.Items;
using System.Collections.Generic;


namespace Server.Commands
{
	public class addallsongs
	{
		public static void Initialize() 
		{
            CommandSystem.Register("AddAllSongs", AccessLevel.GameMaster, new CommandEventHandler(AddAllSongs_OnCommand)); 
		}

        [Usage("AddAllSongs")]
        [Description("Adds all songs to the MusicBox.")]

        public static void AddAllSongs_OnCommand(CommandEventArgs e)
        {
            e.Mobile.SendMessage("Target the MusicBox.");
            e.Mobile.Target = new Server.Items.MusicBox.MusicBox.InternalTarget();
        }
	}
}


namespace Server.Items.MusicBox
{
    [Flipable(0x2AF9, 0x2AFD)]
    public class MusicBox : Item, ISecurable
    {


        public class InternalTarget : Target
        {
            public InternalTarget()
                : base(1, false, TargetFlags.None)
            {
            }

            protected override void OnTarget(Mobile from, object o)
            {
                if (o is MusicBox)
                {
                    from.SendMessage("All Tracks were added to the Music Box!");
                    ((MusicBox)o).AddAllTracks();
                }
                else
                    from.SendMessage("That is not a Music Box!");
            }
        }

        public const int SONGS = 51;

          private SecureLevel m_Level;

	  [CommandProperty( AccessLevel.GameMaster )]
	  public SecureLevel Level
	  {
		  get{ return m_Level; }
		  set{ m_Level = value;Modify(); }
	  }

        [Constructable]
        public MusicBox()
            : base(0x2AF9)
        {
            Name = "Dawn's MusicBox: Stopped";
            Weight = 3.0;
            m_HasTrack = new bool[SONGS];
            for (int x = 0; x < SONGS; x++)
            {
                m_HasTrack[x] = false;
            }
        }

        public MusicBox(Serial serial)
            : base(serial)
        {
        }

		public override void GetContextMenuEntries( Mobile from,  List<ContextMenuEntry> list )
		{
			base.GetContextMenuEntries( from, list );
                  SetSecureLevelEntry.AddTo( from, this, list );
			list.Add(new PlayListEntry(from, this));
		}

        bool playing;

                                
               public override void OnDoubleClick(Mobile from)
        {
            
            if (from.AccessLevel < AccessLevel.GameMaster)
            {
                if (playing)
                {
                    StopMusic(from);
                    Name = "Dawn's MusicBox: Stopped";
                    ItemID = 0x2AF9;
                }
                else
                {
                    PlayMusic(from);   
                    Name = "Dawn's MusicBox: Playing";
                    ItemID = 0x2AFA;
                }
            }
            else
            {
                IPooledEnumerable eable = from.Map.GetMobilesInRange(from.Location, m_Range);
                ArrayList list = new ArrayList();
                foreach (Mobile mob in eable)
                {
                    if (playing)
                        StopMusic(mob);
                    else
                        PlayMusic(mob);
                }
                eable.Free();
                if (playing)
                    Name = "Dawn's MusicBox: Stopped";
                else
                    Name = "Dawn's MusicBox: Playing";
            }
            playing = !playing;
        }

        private static int[] m_Locals = new int[] { 1075131, 1075132, 1075133, 1075134, 1075135, 1075136, 1075137, 
            1075138, 1075139, 1075140, 1075142, 1075143, 1075144, 1075145, 1075146, 1075147, 1075148, 1075149, 
            1075150, 1075151, 1075152, 1075154, 1075155, 1075156, 1075157, 1075158, 1075159, 1075160, 1075163, 
            1075164, 1075165, 1075166, 1075167, 1075168, 1075170, 1075171, 1075172, 1075173, 1075174, 1075175, 
            1075176, 1075177, 1075178, 1075179, 1075180, 1075181, 1075182, 1075183, 1075184, 1075185, 1075186
        };

        private static MusicName[] m_Songs = new MusicName[] { 
            
MusicName.OldUlt02, MusicName.Serpents, MusicName.Britain1, MusicName.Approach, MusicName.Combat2,
            MusicName.Britain2, MusicName.Bucsden, MusicName.Jhelom, MusicName.LBCastle, MusicName.Magincia,

MusicName.Minoc, MusicName.Create1, MusicName.Death, MusicName.DragFlit, MusicName.Forest_a,

MusicName.Ocllo, MusicName.Samlethe, MusicName.Skarabra, MusicName.Trinsic, MusicName.Vesper, 

MusicName.Wind, MusicName.Yew, MusicName.Cave01, MusicName.Dungeon9, MusicName.Sailing,

MusicName.Tavern01, MusicName.Tavern02, MusicName.Stones2, MusicName.Swamp_a, MusicName.Victory,

MusicName.Tavern03, MusicName.Tavern04, MusicName.Combat3, MusicName.Combat1, MusicName.InTown01,

MusicName.BTCastle, MusicName.Nujelm, MusicName.Dungeon2, MusicName.Cove, MusicName.Moonglow,

MusicName.TokunoDungeon, MusicName.Jungle_a, MusicName.Linelle, MusicName.Mountn_a, MusicName.OldUlt01,

MusicName.OldUlt03, MusicName.OldUlt04, MusicName.OldUlt05, MusicName.OldUlt06, MusicName.Plains_a,

MusicName.Zento


        };

        public static int[] Locals
        {
            get { return m_Locals; }
        }

        public static MusicName[] Songs
        {
            get { return m_Songs; }
        }

        private bool[] m_HasTrack;

        public bool[] HasTrack
        {
            get { return m_HasTrack; }
            set { m_HasTrack = value; Modify();}
        }

        public void AddTrack(MusicBoxTrack track)
        {
            for (int x = 0; x < MusicBox.SONGS; x++)
            {
                if (track.Song == MusicBox.Songs[x])
                {
                    m_HasTrack[x] = true;
                }
            }
			
			Modify();
        }

        public void AddAllTracks()
        {
            for (int x = 0; x < MusicBox.SONGS; x++)
            {
                m_HasTrack[x] = true;
            }
			
			Modify();
        }

        private int m_Range = 10;

        [CommandProperty(AccessLevel.GameMaster)]
        public int Range
        {
            get { return m_Range; }
            set { m_Range = value;Modify(); }
        }

        private MusicName m_Music = MusicName.Invalid;

        [CommandProperty(AccessLevel.GameMaster)]
        public MusicName Music
        {
            get { return m_Music; }
            set { m_Music = value; InvalidateProperties();Modify(); }
        }

        public virtual void PlayMusic(Mobile m)
        {
            if (m_Music != MusicName.Invalid && m.NetState != null)
                m.Send( Network.PlayMusic.GetInstance(m_Music));
        }

        public virtual void StopMusic(Mobile m)
        {
            if (m_Music != MusicName.Invalid && m.NetState != null)
                m.Send(Network.PlayMusic.InvalidInstance);
        }

        public override void Serialize(GenericWriter writer)
        {
            base.Serialize(writer);
            writer.Write((int)0); // version
            writer.WriteEncodedInt( (int) m_Level );

            writer.Write((int)m_Range);
            writer.Write((int)m_Music);
            for (int x = 0; x < SONGS; x++)
            {
                writer.Write((bool)m_HasTrack[x]);
            }
        }

        public override void Deserialize(GenericReader reader)
        {
            base.Deserialize(reader);
            int version = reader.ReadInt();
            m_Level = (SecureLevel) reader.ReadEncodedInt();
            m_Range = reader.ReadInt();
            m_Music = (MusicName)reader.ReadInt();
            m_HasTrack = new bool[SONGS];
            for (int x = 0; x < SONGS; x++)
            {
                m_HasTrack[x] = reader.ReadBool();
            }
            Name = "Dawn's MusicBox: Stopped";
        }
    }

    public class PlayListEntry : ContextMenuEntry
    {
        private Mobile m_From;
        private MusicBox m_Box;

        public PlayListEntry(Mobile from, MusicBox box)
            : base(10003, 1)
        {
            m_From = from;
            m_Box = box;
        }

        public override void OnClick()
        {
            m_From.SendGump(new PlayListGump(m_From, m_Box));
        }
    }

    public class PlayListGump : Gump
    {
        private MusicBox m_Box;
        private Mobile m_From;

        public PlayListGump(Mobile from, MusicBox box)
            : base(30, 30)
        {
            from.CloseGump(typeof(PlayListGump));
            m_Box = box;
            m_From = from;
            AddBackground(0, 0, 600, 480, 9400);
            AddPage(0);
            AddHtmlLocalized(250, 10, 180, 20, 1075131, 2124, false, false);
            int Xpos = 26, Ypos = 42;
            int z = 0;
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 17; y++)
                {
                    bool has = (box.HasTrack[z]);
                    AddSongEntry(Xpos, Ypos, MusicBox.Locals[z], has ? 177 : 547, z + 100, has);
                    Ypos += 20;
                    z++;
                }
                Xpos += 200;
                Ypos = 42;
            }
            AddLabel(250, 460, 2124, "Add Songs");
            AddButton(220, 460, 1210, 1210, 99, GumpButtonType.Reply, 0);
        }

        private void AddSongEntry(int x, int y, int local, int color, int song, bool active)
        {
            AddHtmlLocalized(x, y, 180, 20, local, color, false, false);
            if (active) AddButton(x - 20, y, 1210, 1210, song, GumpButtonType.Reply, 0);
        }

        public override void OnResponse(NetState sender, RelayInfo info)
        {
            if (info.ButtonID == 99)
            {
                m_From.Target = new InternalTarget(this);
            }
            else if (info.ButtonID > 99)
            {
                m_Box.Music = MusicBox.Songs[info.ButtonID - 100];
                m_From.SendMessage("Now active:");
                m_From.SendLocalizedMessage(MusicBox.Locals[info.ButtonID - 100]);
            }
            else
                return;
        }

        public void Target(MusicBoxTrack target)
        {
            for (int x = 0; x < MusicBox.SONGS; x++)
            {
                if (target.Song == MusicBox.Songs[x])
                {
                    if (!m_Box.HasTrack[x])
                    {
                        m_Box.HasTrack[x] = true;
                        target.Delete();
                        m_From.SendMessage("Song successfully added.");
                    }
                    else
                        m_From.SendMessage("That song is already added.");
                }
            }
        }

        public class InternalTarget : Target
        {
            private PlayListGump m_Owner;

            public InternalTarget(PlayListGump owner)
                : base(1, false, TargetFlags.None)
            {
                m_Owner = owner;
            }

            protected override void OnTarget(Mobile from, object o)
            {
                if (o is MusicBoxTrack)
                {
                    m_Owner.Target((MusicBoxTrack)o);
                }
                else
                    from.SendMessage("That is not a Song Track!");
            }
        }
    }
}