// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsAzureMmc.Model.Storage
{
    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Globalization;
    using System.Management.Automation;

    public class AzureBlob : AzureObject
    {
        private const string PATHSEPERATOR = "/";

        public AzureBlob(AzureBlobContainer container, PSObject powershellObject, Uri baseUrl)
        {
            this.Container = container;
            this.PSObject = powershellObject;
            if (powershellObject.Properties["ContentEncoding"].Value != null)
            {
                this.ContentEncoding = powershellObject.Properties["ContentEncoding"].Value.ToString();
            }

            if (powershellObject.Properties["ContentLanguage"].Value != null)
            {
                this.ContentLanguage = powershellObject.Properties["ContentLanguage"].Value.ToString();
            }

            if (powershellObject.Properties["ContentType"].Value != null)
            {
                this.ContentType = powershellObject.Properties["ContentType"].Value.ToString();
            }

            this.LastWriteTimeUtc = (DateTime)powershellObject.Properties["LastWriteTimeUtc"].Value;
            this.ObjectName = powershellObject.Properties["ObjectName"].Value.ToString();
            this.Name = this.ObjectName;
            this.Size = Convert.ToInt32(powershellObject.Properties["Size"].Value.ToString(), CultureInfo.InvariantCulture);

            if (powershellObject.Properties["MetaData"] != null)
            {
                this.Metadata = powershellObject.Properties["MetaData"].Value as NameValueCollection;
            }

            this.Path = powershellObject.Properties["Path"].Value.ToString().Replace(baseUrl.AbsoluteUri, string.Empty);

            this.Url = new Uri(powershellObject.Properties["Url"].Value.ToString());

            if (this.Url.HostNameType == UriHostNameType.Unknown)
            {
                this.Path = baseUrl + this.Url.AbsolutePath;
            }
        }

        [ReadOnly(true)]
        public string ContentEncoding { get; set; }

        [ReadOnly(true)]
        public string ContentLanguage { get; set; }

        [ReadOnly(true)]
        public string ContentType { get; set; }

        [ReadOnly(true)]
        public DateTime LastWriteTimeUtc { get; set; }

        [ReadOnly(true)]
        public string ObjectName { get; set; }

        [ReadOnly(true)]
        public int Size { get; set; }

        [ReadOnly(true)]
        public string Path { get; set; }

        [ReadOnly(true)]
        public NameValueCollection Metadata { get; set; }
        
        [Browsable(false)]
        public AzureBlobContainer Container { get; private set; }

        [Browsable(false)]
        public Uri Url { get; private set; }
    }
}