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

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls {

    // *********************************************************************
    //  StyleSkin
    //
    /// <summary>
    /// Encapsulated rendering of style based on the selected skin.
    /// </summary>
    // ********************************************************************/ 
    public class CurrentTime : Literal {

		public virtual String Format {
			get {
				Object state = ViewState["Format"];
				if ( state != null ) {
					return (String)state;
				}
				return CommunityServer.Components.ResourceManager.GetString("Utility_CurrentTime_format");
			}
			set {
				ViewState["Format"] = value;
			}
		}

		public virtual String DateFormat {
			get {
				Object state = ViewState["DateFormat"];
				if ( state != null ) {
					return (String)state;
				}
				return CommunityServer.Components.ResourceManager.GetString("Utility_CurrentTime_dateFormat");
			}
			set {
				ViewState["DateFormat"] = value;
			}
		}
		
		protected override void Render (HtmlTextWriter writer) {
			CSContext cntx = CSContext.Current;
			if (!cntx.SiteSettings.EnableCurrentTime)
                return;

			DateTime time;

            // Set the time to the user's time
            //
            if (HttpContext.Current.Request.IsAuthenticated) {
                time = DateTime.Now.AddHours(cntx.User.Profile.Timezone - cntx.SiteSettings.TimezoneOffset);
                writer.Write( Format, time.ToString(DateFormat), cntx.User.Profile.Timezone.ToString() );
            } else {
                time = DateTime.Now;
                writer.Write( Format, time.ToString(DateFormat), cntx.SiteSettings.TimezoneOffset.ToString() );
            }


        }

    }
}
