using System;
using System.Web.UI.WebControls;
using digioz;
using digioz.VideoModule;
using digioz.PictureModule;

public partial class controls_Home : System.Web.UI.UserControl
{
    public PortalSettings coPortalSettings;
    public int ciTargetPageID;
    public int ciTargetPageID2;

    protected void Page_Load(object sender, EventArgs e)
    {
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();

        // Latest News ---------------------------------------------------------

        News loNews = NewsManagement.GetLatestNews();
        string lsNewsDescription = string.Empty;

        if (loNews != null)
        {
            lblNews.Text = "<p><b>" + loNews.Subject + "</b></p>";
            lsNewsDescription = Convert.ToString(loNews.Description);
            lsNewsDescription = Server.UrlDecode(lsNewsDescription);
            lblNews.Text += "<p>" + lsNewsDescription + "</p>";
            lblNews.Text += "<center>[ <a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("News", coPortalSettings.URLEncryptionKey)) + "\">Read More</a> ]</center>";
        }
        else
        {
            lblNews.Text = "No News Available!";
        }

        // Latest Forum Posts --------------------------------------------------

        ForumPostList loForumPostList = ForumPostManagement.GetForumPostTop5List();

        if (loForumPostList.Count > 0)
        {
            lblForumTopics.Text = "<center><table style=\"width:100%;\">";
            lblForumTopics.Text += "<tr><td class=\"forummainpage\">Subject</td><td class=\"forummainpage\">Posted By</td><td class=\"forummainpage\">Post Time</td></tr>";

            foreach (ForumPost loForumPost in loForumPostList)
            {
                lblForumTopics.Text += "<tr class=\"forummainlines\"><td width=\"300\">" + "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Forum Topic View", coPortalSettings.URLEncryptionKey))
                 + "&forum=" + loForumPost.ForumID + "&topic=" + loForumPost.TopicID + "\">" + loForumPost.PostSubject + "</a></td>"
                 + "<td>" + loForumPost.PosterName + "</td><td>" + loForumPost.PostTime + "</td></tr>";
            }

            lblForumTopics.Text += "</table></center>";

            lblForumTopics.Text += "<br /><center>[ <a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Forum", coPortalSettings.URLEncryptionKey))
                                 + "\">Read More</a> ]</center>";
        }
        else
        {
            lblForumTopics.Text = "No Topics Available!";
        }

        // Latest Users --------------------------------------------------------

        UserProfileList loUserProfileList = UserProfileManagement.GetUserProfileTop5List();
        string lsUPAvatar, lsUPUsername;

        if (loUserProfileList.Count > 0)
        {
            lblUsers.Text = "<table style=\"width:100%;\"><tr>";
            foreach (UserProfile loUserProfile in loUserProfileList)
            {
                lsUPAvatar = loUserProfile.Avatar;
                lsUPUsername = loUserProfile.Username;
                if (lsUPAvatar == null || lsUPAvatar == String.Empty)
                {
                    lblUsers.Text += "<td><center><img src=\"pictures/avatar/NoAvatar.gif\" alt=\"\" /><br />";
                }
                else
                {
                    lblUsers.Text += "<td><center><img src=\"" + lsUPAvatar + "\" alt=\"\" /><br />";
                }
                lblUsers.Text += "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey))
                                 + "&username=" + Server.UrlEncode(digioz.Encryption64.Encrypt(lsUPUsername, coPortalSettings.URLEncryptionKey))
                                 + "\">" + lsUPUsername + "</a></center></td>";
            }
            lblUsers.Text += "</tr></table>";
        }
        else
        {
            lblUsers.Text = "No Users Available!";
        }

        // Latest Pictures -----------------------------------------------------
        ciTargetPageID = MenuItemManagement.GetMenuItemFromMenuName("PictureViewer").ID;
        PictureInfoList loPictureInfoList = PictureInfoManagement.GetPictureInfoTop6List();

        // Truncate long descriptions.
        foreach (PictureInfo loPictureInfo in loPictureInfoList)
        {
            if (loPictureInfo.Description.Length > 20)
            {
                loPictureInfo.Description = loPictureInfo.Description.Substring(0, 20) + "...";
            }
        }

        if (loPictureInfoList.Count > 0)
        {
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = loPictureInfoList;
            lstPictures.DataSource = ps;
            lstPictures.DataBind();

            lblPictures.Text += "<br /><center>[ <a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Pictures", coPortalSettings.URLEncryptionKey))
                                 + "\">Read More</a> ]</center>";
        }
        else
        {
            lblPictures.Text = "No Pictures Available!";
        }

        // Latest Videos -------------------------------------------------------

        ciTargetPageID2 = MenuItemManagement.GetMenuItemFromMenuName("VideoViewer").ID;
        VideoInfoList loVideoInfoList = VideoInfoManagement.GetVideoInfoTop6List();

        // Truncate long descriptions.
        foreach (VideoInfo loVideoInfo in loVideoInfoList)
        {
            if (loVideoInfo.Description.Length > 20)
            {
                loVideoInfo.Description = loVideoInfo.Description.Substring(0, 20) + "...";
            }
        }

        if (loVideoInfoList.Count > 0)
        {
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = loVideoInfoList;
            lstVideos.DataSource = ps;
            lstVideos.DataBind();

            lblVideos.Text += "<br /><center>[ <a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Videos", coPortalSettings.URLEncryptionKey))
                                 + "\">Read More</a> ]</center>";
        }
        else
        {
            lblVideos.Text = "No Videos Available!";
        }

        // Latest Comments --------------------------------------------------

        CommentEntryList loCommentEntryList = CommentEntryManagement.GetRecentCommentEntryList();
        string lsPageURL;
        int liPageID;
        digioz.MenuItem loPageDesc;

        if (loCommentEntryList.Count > 0)
        {
            lblComments.Text = "<center><table style=\"width:100%;\">";
            lblComments.Text += "<tr><td class=\"forummainpage\">Page</td><td class=\"forummainpage\">Posted By</td><td class=\"forummainpage\">Post Time</td>"
            + " </tr>";

            foreach (CommentEntry loCommentEntry in loCommentEntryList)
            {
                liPageID = loCommentEntry.PageID;
                loPageDesc = MenuItemManagement.GetMenuItemFromPageID(liPageID);
                lsPageURL = Server.UrlEncode(loPageDesc.Name);

                lblComments.Text += "<tr class=\"forummainlines\"><td>"
                    + "<a href=\"" + loCommentEntry.PostingPage.Replace("/","") + "\">" + loPageDesc.Name + " </a>"
                    + "<td>" + loCommentEntry.UserName + "</td><td>" + loCommentEntry.ComDateTime + "</td>"
                    + "</tr><tr></tr>";
            }

            lblComments.Text += "</table></center>";
        }
        else
        {
            lblComments.Text = "No Comments Available.";
        }
    }
}
