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

using PTZController.Core;

namespace PTZController.Logic
{
    public class CustomItem
    {
        public enum CustomCommand
        {
            Function = 0,
            HexData
        }

        public enum FunctionMode
        {
            AuxOn = 0,
            AuxOff,
            GotoPreset
        }

        /// <summary>
        /// Ctor
        /// </summary>
        public CustomItem()
        {

        }

        /// <summary>
        /// Ctor, initialize a function command
        /// </summary>
        /// <param name="name"></param>
        /// <param name="camera"></param>
        /// <param name="mode"></param>
        /// <param name="value"></param>
        public CustomItem(
            string name, 
            int camera, 
            FunctionMode mode, 
            int value, 
            string description)
        {
            this.Name = name;
            this.Command = CustomCommand.Function;
            this.Camera = camera;
            this.Mode = mode;
            this.Value = value;
            this.Description = description;
        }

        /// <summary>
        /// Copy Ctor
        /// </summary>
        /// <param name="item"></param>
        public CustomItem(CustomItem item)
        {
            this.Name = item.Name;
            this.Command = item.Command;
            this.Camera = item.Camera;
            this.Mode = item.Mode;
            this.Value = item.Value;
            this.HexValue = item.HexValue;
            this.Description = item.Description;
        }

        /// <summary>
        /// Command name
        /// </summary>
        private string m_name = "";

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

        /// <summary>
        /// Command type
        /// </summary>
        private CustomCommand m_command;

        [XmlAttribute]
        public CustomCommand Command
        {
            get { return m_command; }
            set { m_command = value; }
        }

        /// <summary>
        /// Camera index
        /// </summary>
        private int m_camera;

        [XmlAttribute]
        public int Camera
        {
            get { return m_camera; }
            set { m_camera = value; }
        }

        /// <summary>
        /// Function mode
        /// </summary>
        private FunctionMode m_mode;

        [XmlAttribute]
        public FunctionMode Mode
        {
            get { return m_mode; }
            set { m_mode = value; }
        }

        /// <summary>
        /// Function value
        /// </summary>
        private int m_value;

        [XmlAttribute]
        public int Value
        {
            get { return m_value; }
            set { m_value = value; }
        }

        /// <summary>
        /// Hex data
        /// </summary>
        private byte[] m_hexValue;

        [XmlAttribute]
        public byte[] HexValue
        {
            get { return m_hexValue; }
            set { m_hexValue = value; }
        }

        public string HexString
        {
            get
            {
                string s = "";

                if (m_hexValue == null || m_hexValue.Length == 0)
                {
                    return s;
                }

                for (int i = 0; i < m_hexValue.Length; i++)
                {
                    s += string.Format("{0:X2} ", m_hexValue[i]);
                }

                return s.TrimEnd();
            }
        }

        private string _description;

        [XmlAttribute]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
    }

    [XmlRoot("customs")]
    public class CustomItemList
    {
        private ArrayList m_items;

        public CustomItemList()
        {
            m_items = new ArrayList();
        }

        public CustomItemList(int count)
        {
            m_items = new ArrayList();

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

        public CustomItemList(int presetCount, int totalCount)
        {
            int i = 0;

            m_items = new ArrayList();

            for (; i < presetCount; i++)
            {
                int n = i + 1;

                m_items.Add(new CustomItem(
                    n.ToString(), 
                    PtzConfigure.BroadcastAddress, 
                    CustomItem.FunctionMode.GotoPreset, n,
                    string.Format("Go to address {0}, preset {1}", PtzConfigure.BroadcastAddress, n)));
            }

            for (; i < totalCount; i++)
            {
                m_items.Add(new CustomItem());
            }
        }

        public CustomItem this[int index]
        {
            get
            {
                return m_items[index] as CustomItem;
            }
            set
            {
                m_items[index] = value;
            }
        }

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

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

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

                m_items.Clear();

                foreach (CustomItem item in items)
                {
                    m_items.Add(item);
                }
            }
        }

        public int Add(CustomItem item)
        {
            return m_items.Add(item);
        }
    }
}
