// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2009.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2009, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace PTZController
{
    public class PanelState
    {
        [XmlAttribute]
        public int OffsetX;
        [XmlAttribute]
        public int OffsetY;
        [XmlAttribute]
        public bool Visible;

        public void Update(PanelForm panel)
        {
            if (panel != null)
            {
                Visible = panel.Visible;

                OffsetX = panel.Left - panel.Owner.Left;
                OffsetY = panel.Top - panel.Owner.Top;
            }
        }

        public override string ToString()
        {
            return string.Format("({0}, {1}), {2}", OffsetX, OffsetY, Visible);
        }
    }

    public class PanelStateList
    {
        private ArrayList m_states;

        public PanelStateList()
        {
            m_states = new ArrayList();
        }

        public PanelStateList(int count)
        {
            m_states = new ArrayList();

            for (int i = 0; i < count; i++)
            {
                PanelState ps = new PanelState();

                m_states.Add(ps);
            }
        }

        public PanelStateList(Point[] locations, bool visible)
        {
            m_states = new ArrayList();

            for (int i = 0; i < locations.Length; i++)
            {
                PanelState ps = new PanelState();

                ps.OffsetX = locations[i].X;
                ps.OffsetY = locations[i].Y;
                ps.Visible = visible;

                m_states.Add(ps);
            }
        }

        public PanelState this[int index]
        {
            get { return m_states[index] as PanelState; }
            set { m_states[index] = value; }
        }

        [XmlElement("item")]
        public PanelState[] Items
        {
            get
            {
                PanelState[] items = new PanelState[m_states.Count];
                m_states.CopyTo(items);
                return items;
            }
            set
            {
                if (value == null)
                {
                    return;
                }

                PanelState[] items = (PanelState[])value;
                
                m_states.Clear();
                
                foreach (PanelState item in items)
                {
                    m_states.Add(item);
                }
            }
        }

        public int Add(PanelState item)
        {
            return m_states.Add(item);
        }

        public void Update(PanelForm[] panels)
        {
            m_states.Clear();

            for (int i = 0; i < panels.Length; i++)
            {
                PanelState ps = new PanelState();

                ps.Update(panels[i]);
                this.Add(ps);

                Debug.WriteLine(ps.ToString());
            }
        }

        public void Apply(PanelForm[] panels)
        {
            for (int i = 0; i < m_states.Count; i++)
            {
                panels[i].Left = Items[i].OffsetX;
                panels[i].Top = Items[i].OffsetY;
                panels[i].Visible = Items[i].Visible;

                Debug.WriteLine(Items[i].ToString());
            }
        }
    }
}
