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

namespace Spt.Share.Data
{
    /// <summary>
    /// Enumeration for representing a span's unit
    /// </summary>
    public enum SpanUnit
    {
        Invalid = -1,
        Second = 0,
        Minute,
        Hour
    }

    /// <summary>
    /// Class for representing a time span for tour between presets 
    /// </summary>
    public class StaySpan : ICloneable
    {
        public const string DefaultText = "Default";
        public const int MinuteFactor = 60;
        public const int HourFactor = 60;

        /// <summary>
        /// Time value
        /// </summary>
        private int _value;

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }

        /// <summary>
        /// Unit
        /// </summary>
        private SpanUnit _unit;

        public SpanUnit Unit
        {
            get { return _unit; }
            set { _unit = value; }
        }

        /// <summary>
        /// Seconds of span value
        /// </summary>
        public int Seconds
        {
            get
            {
                if (Value <= 0)
                {
                    return -1;
                }

                int factor = 1;

                switch (Unit)
                {
                    case SpanUnit.Second:
                        break;
                    case SpanUnit.Minute:
                        factor = MinuteFactor;
                        break;
                    case SpanUnit.Hour:
                        factor = HourFactor;
                        break;
                }

                return factor * Value;
            }
        }

        public StaySpan()
        {
            this.Value = 1;
            this.Unit = SpanUnit.Second;
        }

        public StaySpan(int value, SpanUnit unit)
        {
            this.Value = value;
            this.Unit = unit;
        }

        public override string ToString()
        {
            if (Value > 0)
            {
                return string.Format("{0} {1}", Value, Unit);
            }
            else
            {
                return DefaultText;
            }
        }

        public static bool operator == (StaySpan sp1, StaySpan sp2)
        {
            return sp1.Equals(sp2);
        }

        public static bool operator !=(StaySpan sp1, StaySpan sp2)
        {
            return !sp1.Equals(sp2);
        }

        public override bool Equals(object obj)
        {
            StaySpan sp = obj as StaySpan;

            return (this.Value == sp.Value) && (this.Unit == sp.Unit);
        }

        public override int GetHashCode()
        {
            return (this.Value + ((int)this.Unit << 16));
        }

        public Object Clone()
        {
            return new StaySpan(this.Value, this.Unit);
        }

    }
}
