// Personal Webpage Engine
// Copyright(c) 2008 Mihail Kochanov
// You can contact me at StilgarAD@yahoo.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
//
// Personal Web Page Engine
//
//------------------------------------------------------------------------------

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

namespace WebControls
{
    public partial class CommentControl : System.Web.UI.UserControl
    {
        public Comment Comment { get; set; }

        #region PageEvents

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Roles.IsUserInRole(Constants.Roles.Admin))
            {
                pnlAdministration.Visible = true;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            anchor.Name = Constants.Anchors.Comment + Comment.CommentID;
            lbPostAuthor.Text = Comment.AuthorUsername;
            lbPostDate.Text = Comment.PostDate.ToTimeAdjustedString();
            ltPostText.Text = Comment.Text;
            hlIPAddress.Text = String.IsNullOrEmpty(Comment.IPAddress) ? "Unknown" : Comment.IPAddress;
            hlIPAddress.NavigateUrl = "~/Admin/IPInfo.aspx?" + Constants.QueryString.IP + "=" + Comment.IPAddress;


            if (Comment.LastEditedDate != null)
            {
                //add last editor and last edited date
                lastEdit.Visible = true;
                lbLastEditDate.Text = Comment.LastEditedDate.Value.ToTimeAdjustedString();
                lbLastEditor.Text = Comment.LastEditedBy;
            }
        }

        #endregion

        #region ControlEvents

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(DeleteCommandName))
            {
                CommandEventArgs args = new CommandEventArgs(this.DeleteCommandName, this.DeleteCommandArgument);
                OnDeleteCommand(args);
            }
            else
            {
                throw new Exception("Delete command not provided");
            }
        }

        #endregion


        #region DeleteCommand

        private string deleteCommandName;

        [DefaultValue("")]
        public string DeleteCommandName
        {
            get
            {
                return deleteCommandName == null ? String.Empty : deleteCommandName;
            }
            set
            {
                deleteCommandName = value;
            }

        }

        public string deleteCommandArgument;

        [DefaultValue("")]
        public string DeleteCommandArgument
        {
            get
            {
                return deleteCommandArgument == null ? String.Empty : deleteCommandArgument;
            }
            set
            {
                deleteCommandArgument = value;
            }
        }

        protected static readonly object EventCommand = new object();

        public event CommandEventHandler Command
        {
            add
            {
                Events.AddHandler(EventCommand, value);
            }
            remove
            {
                Events.RemoveHandler(EventCommand, value);
            }
        }

        protected virtual void OnDeleteCommand(CommandEventArgs ce)
        {
            CommandEventHandler ceh = this.Events[EventCommand] as CommandEventHandler;
            if (ceh != null)
            {
                ceh(this, ce);
            }

            //This is the trick to cause Command event to bubble up
            base.RaiseBubbleEvent(this, ce);
        }


        #endregion

    }
}