//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Discussions.Controls {

	/// <summary>
	/// Creates a list of PostIcons from the Emoticon List.
	/// </summary>
	public class PostIconRadioButtonList: RadioButtonList {

        string emoticonIDs = "";
        bool autoCreate = true;

        public PostIconRadioButtonList () { }

		protected override void CreateChildControls () {
            base.CreateChildControls();

            // If we aren't using Emoticons, return nothing.
            /* No more needed. We control this from ouside.
			if (!CSContext.Current.SiteSettings.EnableEmoticons) {
				return;
			}*/

			// If the call is a postback, don't create the items...they're most likely already there
			if(!Page.IsPostBack && AutoCreate)
				this.DataBind();
		}

        public string EmoticonIDs {
            set {
                emoticonIDs = value;
            }
        }
        
        public bool AutoCreate {
            get {
                return autoCreate;
            }
            set {
                autoCreate = value;
            }
        }

        public override void DataBind () {
            base.DataBind();

            this.Items.Clear();
            this.Items.Add( new ListItem( ResourceManager.GetString("None"), "" ));

            ArrayList al = Smilies.GetSmilies(true);
			
            const string imgFormat = "<img src=\"{0}{1}\" alt=\"{2}\" />";
            string forumHomePath = Globals.GetSiteUrls().Emoticon;

            //Smiley smiley = new Smiley();

            foreach (Smiley smiley in al) {

                if (smiley != null)
                    this.Items.Add( new ListItem( string.Format(imgFormat, forumHomePath, smiley.SmileyUrl, smiley.SmileyText + " [" + smiley.SmileyCode + "]"), smiley.SmileyId.ToString() ));

            }
        }

        public override string SelectedValue {
            get {
                return base.SelectedValue;
            }
            set {
                if (value == "0")
                    base.SelectedValue = "";
                else
                    base.SelectedValue = value;
            }
        }
	}
}
