﻿namespace ExpressionFramework.WebUI
{
    using System;
    using System.Linq.Expressions;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using System.Web.UI;

    public class TextAreaHtmlInput<TModel, TProperty> : TextHtmlInput<TModel, TProperty>, ITextAreaHtmlInput
    {
        #region Constants

        public const string RowsAttributeName = "rows";

        public const string ColumnsAttributeName = "cols";

        #endregion

        #region Constructors

        public TextAreaHtmlInput(Expression<Func<TModel, TProperty>> modelProperty, HtmlComponentContext componentContext)
            : base(modelProperty, componentContext)
        {
            this.AddReservedHtmlAttributes(RowsAttributeName, ColumnsAttributeName);
        }

        #endregion

        #region Properties

        public int? Rows
        {
            get { return this.HtmlAttributes[RowsAttributeName] as int?; }
            set
            {
                if (value.HasValue)
                {
                    this.HtmlAttributes[RowsAttributeName] = value;
                }
                else
                {
                    this.HtmlAttributes.Remove(RowsAttributeName);
                }
            }
        }

        public int? Columns
        {
            get { return this.HtmlAttributes[ColumnsAttributeName] as int?; }
            set
            {
                if (value.HasValue)
                {
                    this.HtmlAttributes[ColumnsAttributeName] = value;
                }
                else
                {
                    this.HtmlAttributes.Remove(ColumnsAttributeName);
                }
            }
        }

        #endregion

        #region Methods

        public override void WriteHtml(HtmlTextWriter htmlWriter)
        {
            var html = TextAreaExtensions.TextAreaFor(this.HtmlHelper, this.ModelProperty, this.HtmlAttributes);

            htmlWriter.Write(html);
        }

        #endregion
    }
}
