﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HtmlTagContextAttribute.cs" company="Microsoft Corporation">
//   Copyright (c) 2008, 2009, 2010 All Rights Reserved, Microsoft Corporation
//
//   This source is subject to the Microsoft Permissive License.
//   Please see the License.txt file for more information.
//   All other 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.
//
// </copyright>
// <summary>
//    
// </summary>

namespace Microsoft.Exchange.Data.TextConverters
{
    using System;
    using CtsResources;

    internal struct HtmlTagContextAttribute
    {
        private HtmlTagContext tagContext;
        private int attributeIndexAndCookie;

        internal HtmlTagContextAttribute(HtmlTagContext tagContext, int attributeIndexAndCookie)
        {
            this.tagContext = tagContext;
            this.attributeIndexAndCookie = attributeIndexAndCookie;
                    // Orphaned WPL code.
#if false

#if DEBUG
            this.AssertValid();
#endif
#endif
        }
        
        public static readonly HtmlTagContextAttribute Null = new HtmlTagContextAttribute();
        
        public bool IsNull { get { return this.tagContext == null; } }
        
        public HtmlAttributeId Id { get { this.AssertValid(); return this.tagContext.GetAttributeNameIdImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie)); } }
        
        public string Name { get { this.AssertValid(); return this.tagContext.GetAttributeNameImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie)); } }
        
        public string Value { get { this.AssertValid(); return this.tagContext.GetAttributeValueImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie)); } }    

        internal HtmlAttributeParts Parts { get { this.AssertValid(); return this.tagContext.GetAttributePartsImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie)); } }

        // Orphaned WPL code.
#if false
        public int ReadValue(char[] buffer, int offset, int count)
        {
            this.AssertValid();
            return this.tagContext.ReadAttributeValueImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie), buffer, offset, count);
        }

        public void Write()
        {
            this.AssertValid();
            this.tagContext.WriteAttributeImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie), true, true);
        }
        
        public void WriteName()
        {
            this.AssertValid();
            this.tagContext.WriteAttributeImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie), true, false);
        }

        public void WriteValue()
        {
            this.AssertValid();
            this.tagContext.WriteAttributeImpl(HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie), false, true);
        }
#endif

        private void AssertValid()
        {
            if (this.tagContext == null)
            {
                throw new InvalidOperationException(TextConvertersStrings.AttributeNotInitialized);
            }

            this.tagContext.AssertAttributeValid(this.attributeIndexAndCookie);
        }
        
        public override string ToString()
        {
            
            return this.tagContext == null ? "null" : HtmlTagContext.ExtractIndex(this.attributeIndexAndCookie).ToString();
        }
    }
}