/*
 * AttributeOps.cs --
 *
 * Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: $
 */

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Eagle._Attributes;

namespace Eagle._Components.Private
{
    [Guid("a06c7262-eba4-483d-b5b8-3fba96dc4e96")]
    internal static class AttributeOps
    {
        #region Assembly Attribute Support Methods
        public static string GetAssemblyRelease(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblyReleaseAttribute), false))
                    {
                        AssemblyReleaseAttribute release =
                            (AssemblyReleaseAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblyReleaseAttribute), false)[0];

                        return release.Release;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static string GetAssemblySourceId(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblySourceIdAttribute), false))
                    {
                        AssemblySourceIdAttribute sourceId =
                            (AssemblySourceIdAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblySourceIdAttribute), false)[0];

                        return sourceId.SourceId;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static string GetAssemblySourceTimeStamp(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblySourceTimeStampAttribute), false))
                    {
                        AssemblySourceTimeStampAttribute sourceTimeStamp =
                            (AssemblySourceTimeStampAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblySourceTimeStampAttribute),
                                false)[0];

                        return sourceTimeStamp.SourceTimeStamp;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static string GetAssemblyStrongNameTag(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblyStrongNameTagAttribute), false))
                    {
                        AssemblyStrongNameTagAttribute strongNameTag =
                            (AssemblyStrongNameTagAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblyStrongNameTagAttribute),
                                false)[0];

                        return strongNameTag.StrongNameTag;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static string GetAssemblyTag(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblyTagAttribute), false))
                    {
                        AssemblyTagAttribute tag =
                            (AssemblyTagAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblyTagAttribute), false)[0];

                        return tag.Tag;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static string GetAssemblyText(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblyTextAttribute), false))
                    {
                        AssemblyTextAttribute text =
                            (AssemblyTextAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblyTextAttribute), false)[0];

                        return text.Text;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static string GetAssemblyTitle(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblyTitleAttribute), false))
                    {
                        AssemblyTitleAttribute title =
                            (AssemblyTitleAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblyTitleAttribute), false)[0];

                        return title.Title;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }

        ///////////////////////////////////////////////////////////////////////

        public static Uri GetAssemblyUri(
            Assembly assembly
            )
        {
            if (assembly != null)
            {
                try
                {
                    if (assembly.IsDefined(
                            typeof(AssemblyUriAttribute), false))
                    {
                        AssemblyUriAttribute uri =
                            (AssemblyUriAttribute)
                            assembly.GetCustomAttributes(
                                typeof(AssemblyUriAttribute), false)[0];

                        return uri.Uri;
                    }
                }
                catch
                {
                    // do nothing.
                }
            }

            return null;
        }
        #endregion
    }
}
