// 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.Configuration;
using System.Linq;

namespace Admin
{
    public partial class SettingsManagement : System.Web.UI.Page
    {
        private BlogNETDataContext dc;
        public BlogNETDataContext DataContext
        {
            get { return dc; }
        }

        #region PageEvents

        protected void Page_PreInit(object sender, EventArgs e)
        {
            dc = new BlogNETDataContext(ConfigurationManager.ConnectionStrings["BlogNETDB"].ConnectionString);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtTitle.Text = (string)Application[Constants.Application.WebsiteTitle] ?? "";
                txtSlogan.Text = (string)Application[Constants.Application.WebsiteSlogan] ?? "";
                txtAbout.Text = (string)Application[Constants.Application.AboutContent] ?? "";
                string pageSize = (string)Application[Constants.Application.CommentsPageSize];
                ddlCommentsPageSize.SelectedValue = String.IsNullOrEmpty(pageSize) ? Constants.Settings.CommentsPageSize.ToString() : pageSize;
            }
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            if (dc != null)
            {
                dc.Dispose();
            }
        }

        #endregion

        #region ControlEvents

        protected void btnAddQuote_Click(object sender, EventArgs e)
        {
            if (txtQuote.Text.Trim().Length <= 255)
            {
                Quote quote = new Quote { Message = txtQuote.Text.Trim(), Author = txtAuthor.Text.Trim() };

                dc.Quotes.InsertOnSubmit(quote);
                dc.SubmitChanges();

                Cache.Remove(Constants.Cache.Quotes);
                Response.Redirect(Request.Url.ToString());
            }
            else
            {
                lblError.Visible = true;
                lblError.Text = "Quote text is longer than 255 characters.";
            }
        }

        protected void btnSaveSetting_Click(object sender, EventArgs e)
        {
            #region SetTitle

            string title = txtTitle.Text.Trim();

            if (((string)Application[Constants.Application.WebsiteTitle] ?? "") != title)
            {
                var currentTitle = (from t in dc.Settings
                                    where t.Key == Constants.Application.WebsiteTitle
                                    select t).SingleOrDefault();

                if (currentTitle != null)
                {
                    currentTitle.Value = title;
                }
                else
                {
                    Setting t = new Setting { Key = Constants.Application.WebsiteTitle, Value = title };
                    dc.Settings.InsertOnSubmit(t);
                }

                dc.SubmitChanges();
                Application[Constants.Application.WebsiteTitle] = title;
            }

            #endregion

            #region SetSlogan

            string slogan = txtSlogan.Text.Trim();

            if (((string)Application[Constants.Application.WebsiteSlogan] ?? "") != slogan)
            {
                var currentSlogan = (from s in dc.Settings
                                     where s.Key == Constants.Application.WebsiteSlogan
                                     select s).SingleOrDefault();

                if (currentSlogan != null)
                {
                    currentSlogan.Value = slogan;
                }
                else
                {
                    Setting s = new Setting { Key = Constants.Application.WebsiteSlogan, Value = slogan };
                    dc.Settings.InsertOnSubmit(s);
                }

                dc.SubmitChanges();
                Application[Constants.Application.WebsiteSlogan] = slogan;
            }

            #endregion

            #region SetAbout

            string about = txtAbout.Text.Trim();

            if (((string)Application[Constants.Application.AboutContent] ?? "") != about)
            {
                var currentAbout = (from a in dc.Settings
                                    where a.Key == Constants.Application.AboutContent
                                    select a).SingleOrDefault();

                if (currentAbout != null)
                {
                    currentAbout.Value = about;
                }
                else
                {
                    Setting a = new Setting { Key = Constants.Application.AboutContent, Value = about };
                    dc.Settings.InsertOnSubmit(a);
                }

                dc.SubmitChanges();
                Application[Constants.Application.AboutContent] = about;
            }

            #endregion

            #region SetCommentsPageSize

            string commentsPageSize = ddlCommentsPageSize.SelectedValue;

            if (((string)Application[Constants.Application.CommentsPageSize] ?? "") != commentsPageSize)
            {
                var currentCommentsPageSize = (from c in dc.Settings
                                               where c.Key == Constants.Application.CommentsPageSize
                                               select c).SingleOrDefault();

                if (currentCommentsPageSize != null)
                {
                    currentCommentsPageSize.Value = commentsPageSize;
                }
                else
                {
                    Setting c = new Setting { Key = Constants.Application.CommentsPageSize, Value = commentsPageSize };
                    dc.Settings.InsertOnSubmit(c);
                }

                dc.SubmitChanges();
                Application[Constants.Application.CommentsPageSize] = commentsPageSize;
                Cache.Remove(Constants.Cache.LatestComments);
            }

            #endregion

            lbSaveSuccessful.Visible = true;
        }

        protected void gvQuotes_RowDeleted(object sender, System.Web.UI.WebControls.GridViewDeletedEventArgs e)
        {
            Cache.Remove(Constants.Cache.Quotes);
        }

        #endregion

    }
}