using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using Seeems.Cms.Content;
using Seeems.Framework.Xml;
using Seeems.Framework.UI.Controls;
namespace Seeems.Framework.UI.Design {
  public class LimitedTextPropertyEditor: WidePropertyEditor {
    int _rows = 4;
    bool _wordWrap;
    int _maxChar = 50;
    public int Rows {
      get {
        return _rows;
      }
      set {
        _rows = value;
      }
    }
    public bool WordWrap {
      get {
        return _wordWrap;
      }
      set {
        _wordWrap = value;
      }
    }
    public int MaxChar {
      get {
        return _maxChar;
      }
    }
    public override IEnumerable GetSupportedTypes() {
      yield
      return typeof(string);
      yield
      return typeof(CData);
      yield
      return typeof(Html);
    }
    public override bool ParseValue(string value) {
      string val = Convert.ToString(this.Value);
      if (val != value) {
        if (this.PropertyType != null) {
          if (this.PropertyType == typeof(CData)) this.Value = (CData) value;
          else if (this.PropertyType == typeof(Html)) this.Value = (Html) value;
          else this.Value = value;
        } else {
          this.Value = value;
        }
        return true;
      }
      return false;
    }
    public override void RenderInput(Control container) {
      string name = this.FieldName;
      HtmlGenericControl area = new HtmlGenericControl("textarea");
      area.Attributes["id"] = name;
      area.Attributes["class"] = "input";
      area.Attributes["name"] = name;
      area.Attributes["value"] = this.GetValue();
      area.Attributes["rows"] = this.Rows.ToString();
      area.Attributes["wrap"] = _wordWrap.ToString();
      area.Attributes["onkeyup"] = JavaScriptFunctionCall;
      HtmlGenericControl entchar = new HtmlGenericControl("span");
      HtmlGenericControl maxchar = new HtmlGenericControl("label");
      entchar.Attributes["id"] = "enteredChars";
      entchar.Attributes["class"] = "span";
      entchar.Attributes["style"] = "font-size: 11px";
      entchar.InnerText = "0";
      maxchar.Attributes["class"] = "label";
      maxchar.InnerText = "/" + MaxChar.ToString();
      container.Controls.Add(area);
      container.Controls.Add(entchar);
      container.Controls.Add(maxchar);
    }
    const string JavaScriptFunctionCall = @"

      var count = 50;

      var tex = this.value;

      var len = tex.length;

      document.getElementById('enteredChars').innerHTML = len;

       if(len > count){ tex = tex.substring(0,count);

      this.value = tex;

      alert(""Reached max"");

      return false;

      this.value = count-len; }";
  }
}