// 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.Collections;
using System.Xml.Serialization;
using System.Collections.Generic;
using PTZController.Core;

namespace PTZController.Logic
{
    public class CameraItem
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public CameraItem()
        {

        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="address"></param>
        public CameraItem(int address)
        {
            this.Address = address;
            this.Name = string.Format("Cam {0:D3}", address);
            this.m_presets = new PresetItemList(PtzConfigure.PresetCount);
        }

        public override string ToString()
        {
            return string.Format("{0:D3}. {1}", Address, Name);
        }

        /// <summary>
        /// Camera address
        /// </summary>
        private int m_address;

        public int Address
        {
            get { return m_address; }
            set { m_address = value; }
        }

        /// <summary>
        /// Camera name
        /// </summary>
        private string m_name;

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        /// <summary>
        /// Preset items
        /// </summary>
        private PresetItemList m_presets;

        public PresetItemList Presets
        {
            get { return m_presets; }
            set { m_presets = value; }
        }
    }

    public class CameraItemList
    {
        private ArrayList m_states;

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

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

            for (int i = 0; i < count; i++)
            {
                m_states.Add(new CameraItem(i));
            }
        }

        public CameraItem this[int index]
        {
            get
            {
                return m_states[index] as CameraItem;
            }
        }

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

                CameraItem[] items = (CameraItem[])value;

                m_states.Clear();

                foreach (CameraItem item in items)
                {
                    m_states.Add(item);
                }
            }
        }

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

        public int Length
        {
            get { return m_states.Count; }
        }
    }
}
