//---------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="GameFrame.cs" company="DarkWynter Studios">
//     Copyright (C)2007 DarkWynter Studios.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using DW.UserInterface_2D;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using DWGame.Globals;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using DarkWynter.Engine.Menus;

namespace DW.Stream
{
    /// <summary>
    /// Window - the container class for panes
    /// Global space coordinator
    /// </summary>
    public class Window
    {
        public static MenuSystem menuSystem;
        List<Pane> panes;
        

        // Physical representation of the frame - game/HUD dividors
        Texture2D horizontalBar, verticalBar;


        public List<string> hudLocations;



        /// <summary>
        /// Window is the container class for all panes
        /// Constructs the 'view' panes and resizes dynamically
        /// </summary>
        public Window()
        {
            panes = new List<Pane>();
        }

        /// <summary>
        /// Gets the location of the Menu System (MS) and HUD xml files
        /// Can run 4 completely seperate HUDs if so desired
        /// </summary>
        /// <param name="configFile">XML file holding location of MS and HUD</param>
        public void Load(string configFile)
        {
            horizontalBar = Renderer.content.Load<Texture2D>("_textures/hud/horbar");
            verticalBar = Renderer.content.Load<Texture2D>("_textures/hud/vertbar");
            
            hudLocations = new List<string>();
            try
            {
                XmlDocument reader = new XmlDocument();
                reader.Load(configFile);

                XmlNodeList allNodes = reader.ChildNodes;
                foreach (XmlNode node in allNodes)
                {
                    if (node.Name == "GameConfig")
                    {
                        foreach (XmlNode thisNode in node)
                        {
                            if (thisNode.Name == "MenuSystem")
                            {
                                menuSystem = new MenuSystem(thisNode.Attributes["location"].Value);
                            }
                            if (thisNode.Name == "HUD")
                            {
                                hudLocations.Add(thisNode.Attributes["location"].Value);
                            }
                        }
                    }
                }
            }

            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error reading xml");
                throw e;
            }
        }
        /// <summary>
        /// For each player, in split screen, add new pane
        /// If not split screen, use a single pane
        /// </summary>
        public void addPane()
        {
            if (Statics.PLAYERCOUNT < Statics.PLAYERCOUNTMAX)
            {
                // Incr. Active Players
                Statics.PLAYERCOUNT++; 

                // Create new Player
                List<Rectangle> rects = Renderer.GetRectangles(Statics.PLAYERCOUNT);
                for (int i = 0; i < rects.Count; i++)
                {
                    panes.Add(new Pane(hudLocations[i], rects[i]));
                }
                Resize();
            }
        }
        /// <summary>
        /// Remove a pane from the panes list - in case a player drops
        /// </summary>
        public void removePane()
        {
            if (panes.Count > 0)
            {
                panes.RemoveAt(panes.Count - 1);
                Resize();
            }
        }

        /// <summary>
        /// Keep same size window
        /// </summary>
        public void Resize()
        {
            //Renderer.Dimensions.BACKGROUND = new Rectangle(0, 0, backgrnd.Width, backgrnd.Height);
            Resize(Renderer.Dimensions.CLIENTBOUNDS);
            
        }
        /// <summary>
        /// Handles Resize window and sub-components
        /// </summary>
        /// <param name="clientBounds">Window/Game bounds</param>
        public void Resize(Rectangle clientBounds)
        {
            // Quad Ordering (aka - panes.Count)
            //------------------
            // Quad 1 | Quad 2  |
            // Quad 3 | Quad 4  |
            // -----------------
            if (panes == null)
                return;

            // Get Sizes
            List<Rectangle> rects = Renderer.GetRectangles(Statics.PLAYERCOUNT);
            for (int i = 0; i < Statics.PLAYERCOUNT; i++)
            {
                // Pass them along
                panes[i].Resize(rects[i]);
            }
        }
        /// <summary>
        /// Updates the Window
        /// </summary>
        /// <param name="gametime">GameTime</param>
        public void Update(Stopwatch gameTimer, GameTime gametime)
        {
            if (Statics.engineState == Enums_Engine.EngineState.MENU_MODE)
            {
                menuSystem.Update();

            }

            else if (Statics.engineState == Enums_Engine.EngineState.GAME_MODE)
            {
                for (int i = 0; i < panes.Count; i++)
                    panes[i].Update(gameTimer, gametime);
            }
        }

        /// <summary>
        /// Handles the draw for the panes - each pane draws itself on the screen
        /// </summary>
        /// <param name="viewportMain">Global viewport</param>
        public void Draw(Viewport viewportMain)
        {
            // Draw Menus
            if (Statics.engineState == Enums_Engine.EngineState.MENU_MODE)
                menuSystem.Draw(Renderer.spriteBatch);

            // .. or Draw Game
            else if (Statics.viewMode == Enums_Engine.ViewMode.SPLIT_SCREEN)
            {
                // Draw Window Panes
                for (int x = 0; x < panes.Count; x++)
                    panes[x].Draw();

                // Draw Vertical Bar
                if (Statics.PLAYERCOUNT > 1)
                {
                    Renderer.spriteBatch.Draw(
                        verticalBar,
                        new Rectangle(
                            (viewportMain.Width / 2) - (verticalBar.Width / 2),     0, 
                            verticalBar.Width,      verticalBar.Height),
                        Color.Green);
                }

                // Draw Horizontal Bar
                if (Statics.PLAYERCOUNT > 2)
                {
                    Renderer.spriteBatch.Draw(
                        horizontalBar,
                        new Rectangle(
                            0,  (viewportMain.Height / 2) - (horizontalBar.Height / 2),
                            horizontalBar.Width,    horizontalBar.Height),
                        Color.Pink);
                }
            }
            else if (Statics.viewMode == Enums_Engine.ViewMode.SINGLE_SCREEN)
            {
                
                panes[0].Draw();
            }
        }
    }
}