
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.Math.Geometry;
using FlatRedBall_XNA;
using Lidgren.Network;

public enum MessageTypes3
{
    NONE,
    WIZARD_SELECTION,
    BEGIN_GAME
}


namespace Wizard_Duel.Screens
{
    static public class GameSettings2
    {
        static public int player1wizard { get; set; }
        static public int player2wizard { get; set; }
    }

    public class WizardSelectionScreen : Screen
    {

        #region Methods
        bool isHost = NetworkScreen.isHost, isClient = NetworkScreen.isClient;
        NetServer server = NetworkScreen.server;
        NetClient client = NetworkScreen.client;
        Sprite cursor, background;
        AxisAlignedRectangle cursorrec;
        int wizardselection, wizardselection2, count = 0;
        Wizard wizard1, wizard2, wizard3, wizard4, wizard5, wizard6, wizard7, wizard8, wizard9, wizard10;

        #region Constructor and Initialize

        public WizardSelectionScreen()
            : base("NetworkScreen")
        {
            // Don't put initialization code here, do it in
            // the Initialize method below
            //   |   |   |   |   |   |   |   |   |   |   |
            //   |   |   |   |   |   |   |   |   |   |   |
            //   V   V   V   V   V   V   V   V   V   V   V

        }



        public override void Initialize(bool addToManagers)
        {



            // Set the screen up here instead of in the Constructor to avoid
            // exceptions occurring during the constructor.

            // The Screen even provides a content manager for us.  How nice!

            background = SpriteManager.AddSprite("Empty Background.jpg");
            background.PixelSize = .0173f;

            FlatRedBallServices.Game.IsMouseVisible = false;

            cursor = SpriteManager.AddSprite("cursor.png");
            cursor.PixelSize = .015f;
            cursorrec = ShapeManager.AddAxisAlignedRectangle();
            cursorrec.ScaleX = 1;
            cursorrec.ScaleY = 1;
            //cursorrec.AttachTo(cursor, false);
            cursorrec.Visible = true;

            wizard1 = new Wizard(FlatRedBallServices.GlobalContentManager, "Hovering Wizard.png", .036f);
            wizard2 = new Wizard(FlatRedBallServices.GlobalContentManager, "Green Wizard.png", .025f);
            wizard3 = new Wizard(FlatRedBallServices.GlobalContentManager, "Hooded Wizard.png", .045f);
            wizard4 = new Wizard(FlatRedBallServices.GlobalContentManager, "KH Wizard.png", .030f);
            wizard5 = new Wizard(FlatRedBallServices.GlobalContentManager, "Maple Wizard.png", .035f);
            wizard6 = new Wizard(FlatRedBallServices.GlobalContentManager, "Caped Wizard.png", .042f);
            wizard7 = new Wizard(FlatRedBallServices.GlobalContentManager, "Old Wizard.png", .048f);
            wizard8 = new Wizard(FlatRedBallServices.GlobalContentManager, "Tall Wizard.png", .037f);
            wizard9 = new Wizard(FlatRedBallServices.GlobalContentManager, "wizard.png", .020f);
            wizard10 = new Wizard(FlatRedBallServices.GlobalContentManager, "Wizard2.png", .020f);

            wizard1.X = -10;
            wizard1.Y = 10;
            wizard2.X = -5;
            wizard2.Y = 10;
            wizard3.X = 0;
            wizard3.Y = 10;
            wizard4.X = 5;
            wizard4.Y = 10;
            wizard5.X = 10;
            wizard5.Y = 10;
            wizard6.X = -10;
            wizard6.Y = 5;
            wizard7.X = -5;
            wizard7.Y = 5;
            wizard8.X = 0;
            wizard8.Y = 5;
            wizard9.X = 5;
            wizard9.Y = 5;
            wizard10.X = 10;
            wizard10.Y = 5;

            // Manager setup -- we use default skin and do not 
            // register manager as a component
            // AddToManagers should be called LAST in this method:


            if (addToManagers)
            {
                AddToManagers();
            }
        }

        public override void AddToManagers()
        {


        }

        #endregion

        #region Public Methods

        public override void Activity(bool firstTimeCalled)
        {
            routine_init();

            cursor.X = InputManager.Mouse.WorldXAt(0) + 2.1F;
            cursor.Y = InputManager.Mouse.WorldYAt(0) + 1.2F;
            cursorrec.X = InputManager.Mouse.WorldXAt(0);
            cursorrec.Y = InputManager.Mouse.WorldYAt(0);

            if (count == 2 && isHost == true)
            {
                
                SpriteManager.RemoveSprite(background);
                NetConnection player = server.Connections[0];
                NetOutgoingMessage om = server.CreateMessage();
                om.Write((byte)MessageTypes2.BEGIN_GAME);
                server.SendMessage(om, player, NetDeliveryMethod.ReliableUnordered);
                MoveToScreen(typeof(GameScreen).FullName);
            }

            if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
            {
                if (isHost == true)
                {
                    if (wizard1.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 1;
                        endSelection();
                    }
                    if (wizard2.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 2;
                        endSelection();
                    }
                    if (wizard3.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 3;
                        endSelection();
                    }
                    if (wizard4.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 4;
                        endSelection();
                    }
                    if (wizard5.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 5;
                        endSelection();
                    }
                    if (wizard6.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 6;
                        endSelection();
                    }
                    if (wizard7.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 7;
                        endSelection();
                    }
                    if (wizard8.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 8;
                        endSelection();
                    }
                    if (wizard9.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 9;
                        endSelection();
                    }
                    if (wizard10.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection = 10;
                        endSelection();
                    }
                }
                else
                {
                    if (wizard1.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 1;
                        endSelection2();
                    }
                    if (wizard2.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 2;
                        endSelection2();
                    }
                    if (wizard3.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 3;
                        endSelection2();
                    }
                    if (wizard4.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 4;
                        endSelection2();
                    }
                    if (wizard5.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 5;
                        endSelection2();
                    }
                    if (wizard6.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 6;
                        endSelection2();
                    }
                    if (wizard7.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 7;
                        endSelection2();
                    }
                    if (wizard8.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 8;
                        endSelection2();
                    }
                    if (wizard9.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 9;
                        endSelection2();
                    }
                    if (wizard10.Collision.CollideAgainst(cursorrec))
                    {
                        wizardselection2 = 10;
                        endSelection2();
                    }
                }
            }
        }

        private void routine_init()
        {
            if (isHost == true)
            {
                server_routine();
            }
            else if (isClient == true)
            {
                client_routine();
            }
        }

        private void endSelection()
        {
            wizard1.Destroy();
            wizard2.Destroy();
            wizard3.Destroy();
            wizard4.Destroy();
            wizard5.Destroy();
            wizard6.Destroy();
            wizard7.Destroy();
            wizard8.Destroy();
            wizard9.Destroy();
            wizard10.Destroy();

            SpriteManager.RemoveSprite(cursor);
            ShapeManager.Remove(cursorrec);

            count++;
            GameSettings2.player1wizard = wizardselection;
            NetConnection player = server.Connections[0];
            NetOutgoingMessage om = server.CreateMessage();
            om.Write((byte)MessageTypes3.WIZARD_SELECTION);
            om.Write(wizardselection);
            om.Write(count);
            server.SendMessage(om, player, NetDeliveryMethod.ReliableUnordered);
        }

        private void endSelection2()
        {
            wizard1.Destroy();
            wizard2.Destroy();
            wizard3.Destroy();
            wizard4.Destroy();
            wizard5.Destroy();
            wizard6.Destroy();
            wizard7.Destroy();
            wizard8.Destroy();
            wizard9.Destroy();
            wizard10.Destroy();

            SpriteManager.RemoveSprite(cursor);
            ShapeManager.Remove(cursorrec);

            count++;
            GameSettings2.player2wizard = wizardselection2;
            NetOutgoingMessage om = client.CreateMessage();
            om.Write((byte)MessageTypes3.WIZARD_SELECTION);
            om.Write(wizardselection2); // very inefficient to send a full Int32 (4 bytes) but we'll use this for simplicity
            om.Write(count);
            client.SendMessage(om, NetDeliveryMethod.ReliableUnordered);
        }

        public override void Destroy()
        {
            base.Destroy();
        }

        public void server_routine()
        {
            NetIncomingMessage om;
            while ((om = server.ReadMessage()) != null)
            {
                // handle incoming message
                if (om.MessageType == NetIncomingMessageType.Data)
                {
                    MessageTypes3 msgType = (MessageTypes3)om.ReadByte();
                    switch (msgType)
                    {
                        case MessageTypes3.WIZARD_SELECTION:
                            wizardselection2 = om.ReadInt32();
                            count = om.ReadInt32();

                            GameSettings2.player2wizard = wizardselection2;
                            

                            
                            break;
                        
                    }
                }
            }
        }

        public void client_routine()
        {
            NetIncomingMessage om;
            while ((om = client.ReadMessage()) != null)
            {
                if (om.MessageType == NetIncomingMessageType.Data)
                {
                    MessageTypes3 msgType = (MessageTypes3)om.ReadByte();
                    switch (msgType)
                    {
                        case MessageTypes3.WIZARD_SELECTION:
                            wizardselection = om.ReadInt32();
                            count = om.ReadInt32();
                            GameSettings2.player1wizard = wizardselection;
                            break;
                        case MessageTypes3.BEGIN_GAME:
                            SpriteManager.RemoveSprite(background);
                            MoveToScreen(typeof(GameScreen).FullName);
                            break;
                    }
                }
            }
        }
        #endregion
        #endregion
    }
}